Skip to content

Commit 7754ac3

Browse files
committed
Follow-up for #3656
- Do not abbreviate 'representation' - Add specs
1 parent 4417efc commit 7754ac3

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

lib/rails_admin/config/fields/types/active_storage.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
1818
end
1919

2020
register_instance_option :image? do
21-
value && (value.representable? || mime_type(value.filename).to_s.match?(/^image/))
21+
value && (value.representable? || value.content_type.match?(/^image/))
2222
end
2323

2424
register_instance_option :eager_load do
@@ -50,9 +50,9 @@ def resource_url(thumb = false)
5050

5151
if thumb && value.representable?
5252
thumb = thumb_method if thumb == true
53-
repr = value.representation(thumb)
53+
representation = value.representation(thumb)
5454
Rails.application.routes.url_helpers.rails_blob_representation_path(
55-
repr.blob.signed_id, repr.variation.key, repr.blob.filename, only_path: true
55+
representation.blob.signed_id, representation.variation.key, representation.blob.filename, only_path: true
5656
)
5757
else
5858
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
@@ -63,10 +63,6 @@ def value
6363
attachment = super
6464
attachment if attachment&.attached?
6565
end
66-
67-
def mime_type(filename_obj)
68-
Mime::Type.lookup_by_extension(filename_obj.extension_without_delimiter)
69-
end
7066
end
7167
end
7268
end

lib/rails_admin/config/fields/types/multiple_active_storage.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,21 @@ class ActiveStorageAttachment < RailsAdmin::Config::Fields::Types::MultipleFileU
2323
end
2424

2525
register_instance_option :image? do
26-
value && (value.representable? || mime_type(value.filename).to_s.match?(/^image/))
26+
value && (value.representable? || value.content_type.match?(/^image/))
2727
end
2828

2929
def resource_url(thumb = false)
3030
return nil unless value
3131

3232
if thumb && value.representable?
33-
repr = value.representation(thumb_method)
33+
representation = value.representation(thumb_method)
3434
Rails.application.routes.url_helpers.rails_blob_representation_path(
35-
repr.blob.signed_id, repr.variation.key, repr.blob.filename, only_path: true
35+
representation.blob.signed_id, representation.variation.key, representation.blob.filename, only_path: true
3636
)
3737
else
3838
Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
3939
end
4040
end
41-
42-
def mime_type(filename_obj)
43-
Mime::Type.lookup_by_extension(filename_obj.extension_without_delimiter)
44-
end
4541
end
4642

4743
register_instance_option :attachment_class do

spec/rails_admin/config/fields/types/active_storage_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
expect(field.image?).to be_falsy
4343
end
4444
end
45+
46+
context 'when attachment is a PDF file' do
47+
let(:record) { FactoryBot.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: 'test.pdf', content_type: 'application/pdf'} }
48+
before { allow(ActiveStorage::Previewer::PopplerPDFPreviewer).to receive(:accept?).and_return(true) }
49+
50+
it 'returns true' do
51+
expect(field.image?).to be_truthy
52+
end
53+
end
4554
end
4655

4756
describe '#resource_url' do
@@ -68,6 +77,15 @@
6877
expect(field.resource_url(true)).not_to match(/representations/)
6978
end
7079
end
80+
81+
context 'when attachment is a PDF file' do
82+
let(:record) { FactoryBot.create :field_test, active_storage_asset: {io: StringIO.new('dummy'), filename: 'test.pdf', content_type: 'application/pdf'} }
83+
before { allow(ActiveStorage::Previewer::PopplerPDFPreviewer).to receive(:accept?).and_return(true) }
84+
85+
it 'returns variant\'s url' do
86+
expect(field.resource_url(true)).to match(/representations/)
87+
end
88+
end
7189
end
7290

7391
describe '#value' do

spec/rails_admin/config/fields/types/multiple_active_storage_spec.rb

+18
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@
6363
expect(field.attachments[0].image?).to be_falsy
6464
end
6565
end
66+
67+
context 'when attachment is a PDF file' do
68+
let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: 'test.pdf', content_type: 'application/pdf'}] }
69+
before { allow(ActiveStorage::Previewer::PopplerPDFPreviewer).to receive(:accept?).and_return(true) }
70+
71+
it 'returns true' do
72+
expect(field.attachments[0].image?).to be_truthy
73+
end
74+
end
6675
end
6776

6877
describe '#resource_url' do
@@ -89,6 +98,15 @@
8998
expect(field.attachments[0].resource_url(true)).not_to match(/representations/)
9099
end
91100
end
101+
102+
context 'when attachment is a PDF file' do
103+
let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: 'test.pdf', content_type: 'application/pdf'}] }
104+
before { allow(ActiveStorage::Previewer::PopplerPDFPreviewer).to receive(:accept?).and_return(true) }
105+
106+
it 'returns variant\'s url' do
107+
expect(field.attachments[0].resource_url(true)).to match(/representations/)
108+
end
109+
end
92110
end
93111
end
94112

0 commit comments

Comments
 (0)