houdini/app/models/concerns/image/attachment_extensions.rb
2020-05-27 12:32:31 -05:00

40 lines
No EOL
1.6 KiB
Ruby

module Image::AttachmentExtensions
extend ActiveSupport::Concern
class_methods do
def has_one_attached_with_sizes(attribute_name, sizes)
if sizes.nil? || !sizes.is_a?(Hash) || !sizes.any?
raise ArgumentError, "You must pass a valid hash of sizes"
end
attribute = attribute_name.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