Skip to content

Commit

Permalink
Change to decide #thumb_method based on ActiveStorage version
Browse files Browse the repository at this point in the history
Refs. #3255
  • Loading branch information
mshibuya committed Feb 28, 2021
1 parent b552f2c commit 2dba791
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/types/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
RailsAdmin::Config::Fields::Types.register(self)

register_instance_option :thumb_method do
if Gem.loaded_specs.key?('ruby-vips')
if ::ActiveStorage::VERSION::MAJOR >= 6
{resize_to_limit: [100, 100]}
else
{resize: '100x100>'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MultipleActiveStorage < RailsAdmin::Config::Fields::Types::MultipleFileUpl

class ActiveStorageAttachment < RailsAdmin::Config::Fields::Types::MultipleFileUpload::AbstractAttachment
register_instance_option :thumb_method do
if Gem.loaded_specs.key?('ruby-vips')
if ::ActiveStorage::VERSION::MAJOR >= 6
{resize_to_limit: [100, 100]}
else
{resize: '100x100>'}
Expand Down
10 changes: 10 additions & 0 deletions spec/rails_admin/config/fields/types/active_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
end.with(object: record)
end

describe '#thumb_method' do
it 'returns corresponding value which is to be passed to image_processing(ActiveStorage >= 6.0) or mini_magick(ActiveStorage 5.2)' do
if ::ActiveStorage::VERSION::MAJOR >= 6
expect(field.thumb_method).to eq(resize_to_limit: [100, 100])
else
expect(field.thumb_method).to eq(resize: '100x100>')
end
end
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"} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@
end

describe RailsAdmin::Config::Fields::Types::MultipleActiveStorage::ActiveStorageAttachment do
describe '#thumb_method' do
let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: "test.txt", content_type: "text/plain"}] }
subject { field.attachments[0] }

it 'returns corresponding value which is to be passed to image_processing(ActiveStorage >= 6.0) or mini_magick(ActiveStorage 5.2)' do
if ::ActiveStorage::VERSION::MAJOR >= 6
expect(subject.thumb_method).to eq(resize_to_limit: [100, 100])
else
expect(subject.thumb_method).to eq(resize: '100x100>')
end
end
end

describe '#pretty_value' do
subject { field.pretty_value }

Expand Down

0 comments on commit 2dba791

Please sign in to comment.