Attachment variants can be requested by name
This commit is contained in:
parent
3c78022724
commit
2ad9ac8c92
6 changed files with 62 additions and 0 deletions
|
@ -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'
|
||||
|
|
40
app/models/concerns/image/attachment_extensions.rb
Normal file
40
app/models/concerns/image/attachment_extensions.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
@ -64,6 +65,9 @@ class Event < ApplicationRecord
|
|||
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]) }
|
||||
scope :deleted, -> { where(deleted: true) }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue