mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
We introduced a list of formats we support and forgot to add webp. Now I added that as allowed format again and modified the error message. I removed the first sentence from the error message because it's very similar to the default error which is shown as well.
44 lines
1.1 KiB
Ruby
44 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Spree
|
|
class Image < Asset
|
|
SIZES = {
|
|
mini: { resize_to_fill: [48, 48] },
|
|
small: { resize_to_fill: [227, 227] },
|
|
product: { resize_to_limit: [240, 240] },
|
|
large: { resize_to_limit: [600, 600] },
|
|
}.freeze
|
|
|
|
has_one_attached :attachment
|
|
|
|
validates :attachment, attached: true, content_type: %r{\Aimage/(png|jpeg|gif|jpg|svg\+xml|webp)\Z}
|
|
validate :no_attachment_errors
|
|
|
|
def variant(name)
|
|
if attachment.variable?
|
|
attachment.variant(SIZES[name])
|
|
else
|
|
attachment
|
|
end
|
|
end
|
|
|
|
def url(size)
|
|
return unless attachment.attached?
|
|
|
|
Rails.application.routes.url_helpers.url_for(variant(size))
|
|
end
|
|
|
|
# if there are errors from the plugin, then add a more meaningful message
|
|
def no_attachment_errors
|
|
return if errors[:attachment].empty?
|
|
|
|
if errors.all? { |e| e.type == :content_type_invalid }
|
|
attachment.errors.clear
|
|
errors.add :base, I18n.t('spree.admin.products.image_upload_error')
|
|
end
|
|
|
|
false
|
|
end
|
|
end
|
|
end
|