From 2ad9ac8c92e8c1d6e41d81ad27e66231422bfabc Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 12 May 2020 16:11:44 -0500 Subject: [PATCH] Attachment variants can be requested by name --- app/models/campaign.rb | 4 ++ .../concerns/image/attachment_extensions.rb | 40 +++++++++++++++++++ app/models/event.rb | 4 ++ app/models/image_attachment.rb | 3 ++ app/models/nonprofit.rb | 9 +++++ app/models/profile.rb | 2 + 6 files changed, 62 insertions(+) create mode 100644 app/models/concerns/image/attachment_extensions.rb diff --git a/app/models/campaign.rb b/app/models/campaign.rb index a2441b52..1ca69f57 100644 --- a/app/models/campaign.rb +++ b/app/models/campaign.rb @@ -2,6 +2,7 @@ # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later class Campaign < ApplicationRecord + include Image::AttachmentExtensions # :name, # :tagline, # :slug, # str: url name @@ -54,6 +55,9 @@ class Campaign < ApplicationRecord has_one_attached :background_image has_one_attached :banner_image + has_one_attached_with_sizes(:main_image, {normal: [524, 360], thumb: [180,150]}) + has_one_attached_with_sizes(:background_image, {normal: [1000, 600]}) + has_many :donations has_many :charges, through: :donations has_many :payments, through: :donations, source: 'payment' diff --git a/app/models/concerns/image/attachment_extensions.rb b/app/models/concerns/image/attachment_extensions.rb new file mode 100644 index 00000000..a2df9fed --- /dev/null +++ b/app/models/concerns/image/attachment_extensions.rb @@ -0,0 +1,40 @@ +module Image::AttachmentExtensions + extend ActiveSupport::Concern + class_methods do + def has_one_attached_with_sizes(attribute, sizes) + if sizes.nil? || !sizes.is_a?(Hash) || !sizes.any? + raise ArgumentError, "You must pass a valid hash of sizes" + end + attribute = attribute.to_s + + # clean up sizes + sizes.keys.each do |key| + value = sizes[key] + if value.is_a?(Numeric) + sizes[key] = [value, value] + elsif value.is_a?(Array) && value.count == 1 && value.all?{|i| i.is_a?(Numeric)} + sizes[key]= [value[0], value[0]] + elsif value.is_a?(Array) && value.count == 2 && value.all?{|i| i.is_a?(Numeric)} + sizes[key] = [value[0], value[1]] + else + raise ArgumentError, "#{value.to_s} was not a valid size." + end + end + + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{attribute}_by_size(size) + case (size) + #{sizes.map do |k,v| + <<-INNER + when :#{k.to_sym} + return #{attribute}.variant(resize: "#{v[0]}x#{v[1]}") + INNER + end.join("\n")} + else + raise ArgumentError, ":" + size.to_s + " is not a valid size. Valid sizes are: #{sizes.keys.map{|i| ":" + i.to_s}.join(', ')}" + end + end + RUBY + end + end +end \ No newline at end of file diff --git a/app/models/event.rb b/app/models/event.rb index 6b8298a5..6953997b 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -2,6 +2,7 @@ # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later class Event < ApplicationRecord + include Image::AttachmentExtensions # :deleted, #bool for soft-delete # :name, # str # :tagline, # str @@ -63,6 +64,9 @@ class Event < ApplicationRecord accepts_nested_attributes_for :ticket_levels, allow_destroy: true has_one_attached :main_image has_one_attached :background_image + + has_one_attached_with_sizes :main_image, {normal: 400, thumb: 100} + has_one_attached_with_sizes :background_image, {normal: [1000, 600]} scope :not_deleted, -> { where(deleted: [nil, false]) } diff --git a/app/models/image_attachment.rb b/app/models/image_attachment.rb index a3173494..23e89904 100644 --- a/app/models/image_attachment.rb +++ b/app/models/image_attachment.rb @@ -2,10 +2,13 @@ # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later class ImageAttachment < ApplicationRecord + include Image::AttachmentExtensions # :parent_id, # :file has_one_attached :file + has_one_attached_with_sizes :file, {large: [600, 400], medium: [400, 266], small: [400,266], thumb_explore: [200,133]} + # not sure if poly parent is used on this model, as all values are nil in db belongs_to :parent, polymorphic: true end diff --git a/app/models/nonprofit.rb b/app/models/nonprofit.rb index 19557ad0..d85b58e7 100755 --- a/app/models/nonprofit.rb +++ b/app/models/nonprofit.rb @@ -5,6 +5,7 @@ class Nonprofit < ApplicationRecord attr_accessor :register_np_only, :user_id, :user Categories = ['Public Benefit', 'Human Services', 'Education', 'Civic Duty', 'Human Rights', 'Animals', 'Environment', 'Health', 'Arts, Culture, Humanities', 'International', 'Children', 'Religion', 'LGBTQ', "Women's Rights", 'Disaster Relief', 'Veterans'].freeze + include Image::AttachmentExtensions # :name, # str # :stripe_account_id, # str # :summary, # text: paragraph-sized organization summary @@ -103,6 +104,14 @@ class Nonprofit < ApplicationRecord has_one_attached :background_image has_one_attached :logo + # way too wordy + has_one_attached_with_sizes(:logo, {small: 30, normal: 100, large: 180}) + has_one_attached_with_sizes(:background_image, {normal: [1000,600]}) + has_one_attached_with_sizes(:main_image, {nonprofit_carousel: [590, 338], thumb: [188, 120], thumb_explore: [100, 100]}) + has_one_attached_with_sizes(:second_image, {nonprofit_carousel: [590, 338], thumb: [188, 120], thumb_explore: [100, 100]}) + has_one_attached_with_sizes(:third_image, {nonprofit_carousel: [590, 338], thumb: [188, 120], thumb_explore: [100, 100]}) + + serialize :achievements, Array serialize :categories, Array diff --git a/app/models/profile.rb b/app/models/profile.rb index b0e564e1..a8ab05b8 100755 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -2,6 +2,7 @@ # License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later class Profile < ApplicationRecord + include Image::AttachmentExtensions # :registered, # bool # :mini_bio, # :first_name, # str @@ -26,6 +27,7 @@ class Profile < ApplicationRecord serialize :privacy_settings, Array has_one_attached :picture + has_one_attached_with_sizes(:picture, {normal: 150, medium:100, tiny: 50}) belongs_to :user has_many :activities # Activities this profile has created