Skip to content

Commit 0df71c9

Browse files
authored
Merge pull request #3295 from RongRongTeng/feature/show-correct-file-name-for-multiple-attachments
Show Correct Filename For Multiple Attachments
2 parents b514455 + a594665 commit 0df71c9

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def initialize(value)
3838
image_html = v.image_tag(thumb_url, class: 'img-thumbnail')
3939
url != thumb_url ? v.link_to(image_html, url, target: '_blank', rel: 'noopener noreferrer') : image_html
4040
else
41-
v.link_to(value, url, target: '_blank', rel: 'noopener noreferrer')
41+
display_value = value.respond_to?(:filename) ? value.filename : value
42+
v.link_to(display_value, url, target: '_blank', rel: 'noopener noreferrer')
4243
end
4344
end
4445
end

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,34 @@
77
let(:field) do
88
RailsAdmin.config('FieldTest').fields.detect do |f|
99
f.name == :active_storage_assets
10-
end.with(object: record)
10+
end.with(
11+
object: record,
12+
view: ApplicationController.new.view_context,
13+
)
1114
end
1215

1316
describe RailsAdmin::Config::Fields::Types::MultipleActiveStorage::ActiveStorageAttachment do
17+
describe '#pretty_value' do
18+
subject { field.pretty_value }
19+
20+
context 'when attachment is not an image' do
21+
let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: "test.txt", content_type: "text/plain"}] }
22+
23+
it 'uses filename as link text' do
24+
expect(Nokogiri::HTML(subject).text).to eq 'test.txt'
25+
end
26+
end
27+
28+
context 'when the field is an image' do
29+
let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"}] }
30+
31+
it 'shows thumbnail image with a link' do
32+
expect(Nokogiri::HTML(subject).css('img').attribute('src').value).to match(%r{rails/active_storage/representations})
33+
expect(Nokogiri::HTML(subject).css('a').attribute('href').value).to match(%r{rails/active_storage/blobs})
34+
end
35+
end
36+
end
37+
1438
describe '#image?' do
1539
context 'when attachment is an image' do
1640
let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"}] }

0 commit comments

Comments
 (0)