Skip to content

Commit

Permalink
Merge pull request #3398 from codealchemy/mime-type-update
Browse files Browse the repository at this point in the history
Update `image?` check for file upload field types
  • Loading branch information
mshibuya committed Oct 2, 2021
2 parents 6c802dc + 7691afd commit 10d9c9d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/rails_admin/config/fields/types/active_storage.rb
Expand Up @@ -17,7 +17,8 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload

register_instance_option :image? do
if value
value.filename.to_s.split('.').last =~ /jpg|jpeg|png|gif|svg/i
mime_type = Mime::Type.lookup_by_extension(value.filename.extension_without_delimiter)
mime_type.to_s.match?(/^image/)
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/rails_admin/config/fields/types/dragonfly.rb
Expand Up @@ -12,7 +12,8 @@ class Dragonfly < RailsAdmin::Config::Fields::Types::FileUpload
register_instance_option :image? do
false unless value
if abstract_model.model.new.respond_to?("#{name}_name")
bindings[:object].send("#{name}_name").to_s.split('.').last =~ /jpg|jpeg|png|gif/i
mime_type = Mime::Type.lookup_by_extension(bindings[:object].send("#{name}_name").to_s.split('.').last)
mime_type.to_s.match?(/^image/)
else
true # Dragonfly really is image oriented
end
Expand Down
3 changes: 2 additions & 1 deletion lib/rails_admin/config/fields/types/file_upload.rb
Expand Up @@ -50,7 +50,8 @@ class FileUpload < RailsAdmin::Config::Fields::Base
end

register_instance_option :image? do
(url = resource_url.to_s) && url.split('.').last =~ /jpg|jpeg|png|gif|svg/i
mime_type = Mime::Type.lookup_by_extension(resource_url.to_s.split('.').last)
mime_type.to_s.match?(/^image/)
end

register_instance_option :allowed_methods do
Expand Down
Expand Up @@ -18,7 +18,8 @@ class ActiveStorageAttachment < RailsAdmin::Config::Fields::Types::MultipleFileU

register_instance_option :image? do
if value
value.filename.to_s.split('.').last =~ /jpg|jpeg|png|gif|svg/i
mime_type = Mime::Type.lookup_by_extension(value.filename.extension_without_delimiter)
mime_type.to_s.match?(/^image/)
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/rails_admin/config/fields/types/multiple_file_upload.rb
Expand Up @@ -45,7 +45,8 @@ def initialize(value)
end

register_instance_option :image? do
(url = resource_url.to_s) && url.split('.').last =~ /jpg|jpeg|png|gif|svg/i
mime_type = Mime::Type.lookup_by_extension(resource_url.to_s.split('.').last)
mime_type.to_s.match?(/^image/)
end

def resource_url(_thumb = false)
Expand Down
17 changes: 12 additions & 5 deletions spec/rails_admin/config/fields/types/active_storage_spec.rb
Expand Up @@ -17,11 +17,18 @@
end

describe '#image?' do
context 'when attachment is an image' do
let(:record) { FactoryBot.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"} }

it 'returns true' do
expect(field.image?).to be_truthy
context 'configured Mime::Types' do
before { Mime::Type.register 'image/webp', :webp }
after { Mime::Type.unregister :webp }

%w[jpg jpeg png gif svg webp].each do |image_type_ext|
context "when attachment is a '#{image_type_ext}' file" do
let(:record) { FactoryBot.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: "test.#{image_type_ext}"} }

it 'returns true' do
expect(field.image?).to be_truthy
end
end
end
end

Expand Down

0 comments on commit 10d9c9d

Please sign in to comment.