Extract constant to configure valid mime types

This way it'll be possible to overwrite the valid mime types in a custom
model.
This commit is contained in:
Javi Martín
2022-08-03 15:49:22 +02:00
parent 00ea1bf719
commit 11bed74678
2 changed files with 17 additions and 1 deletions

View File

@@ -11,10 +11,12 @@ class SiteCustomization::Image < ApplicationRecord
"logo_email" => [400, 80] "logo_email" => [400, 80]
}.freeze }.freeze
VALID_MIME_TYPES = %w[image/jpeg image/png].freeze
has_attachment :image has_attachment :image
validates :name, presence: true, uniqueness: true, inclusion: { in: ->(*) { VALID_IMAGES.keys }} validates :name, presence: true, uniqueness: true, inclusion: { in: ->(*) { VALID_IMAGES.keys }}
validates :image, file_content_type: { allow: ["image/png", "image/jpeg"], if: -> { image.attached? }} validates :image, file_content_type: { allow: ->(*) { VALID_MIME_TYPES }, if: -> { image.attached? }}
validate :check_image validate :check_image
def self.all_images def self.all_images

View File

@@ -44,4 +44,18 @@ describe SiteCustomization::Image do
expect(map).not_to be_valid expect(map).not_to be_valid
end end
end end
it "dynamically validates the valid mime types" do
stub_const("#{SiteCustomization::Image}::VALID_MIME_TYPES", ["image/gif"])
gif = build(:site_customization_image,
name: "logo_header",
image: fixture_file_upload("logo_header.gif"))
expect(gif).to be_valid
png = build(:site_customization_image,
name: "logo_header",
image: fixture_file_upload("logo_header.png"))
expect(png).not_to be_valid
end
end end