From 5fa250514d260b5189178bc8e5ff495fd8e782cf Mon Sep 17 00:00:00 2001 From: "Ya-Rong, Teng" Date: Sun, 2 Aug 2020 16:52:53 +0800 Subject: [PATCH 1/2] feat(types): show correct file name --- lib/rails_admin/config/fields/types/multiple_file_upload.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rails_admin/config/fields/types/multiple_file_upload.rb b/lib/rails_admin/config/fields/types/multiple_file_upload.rb index e9edb92cff..47c361489e 100644 --- a/lib/rails_admin/config/fields/types/multiple_file_upload.rb +++ b/lib/rails_admin/config/fields/types/multiple_file_upload.rb @@ -38,7 +38,8 @@ def initialize(value) image_html = v.image_tag(thumb_url, class: 'img-thumbnail') url != thumb_url ? v.link_to(image_html, url, target: '_blank', rel: 'noopener noreferrer') : image_html else - v.link_to(value, url, target: '_blank', rel: 'noopener noreferrer') + display_value = value.respond_to?(:filename) ? value.filename : value + v.link_to(display_value, url, target: '_blank', rel: 'noopener noreferrer') end end end From a594665b096bd4a2f6cc3376f6dbe1560ad588eb Mon Sep 17 00:00:00 2001 From: "Ya-Rong, Teng" Date: Sun, 2 Aug 2020 16:53:05 +0800 Subject: [PATCH 2/2] test(spec): add test examples for #pretty_value --- .../types/multiple_active_storage_spec.rb | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/spec/rails_admin/config/fields/types/multiple_active_storage_spec.rb b/spec/rails_admin/config/fields/types/multiple_active_storage_spec.rb index e24b04d7e4..0fd72d4d2a 100644 --- a/spec/rails_admin/config/fields/types/multiple_active_storage_spec.rb +++ b/spec/rails_admin/config/fields/types/multiple_active_storage_spec.rb @@ -7,10 +7,34 @@ let(:field) do RailsAdmin.config('FieldTest').fields.detect do |f| f.name == :active_storage_assets - end.with(object: record) + end.with( + object: record, + view: ApplicationController.new.view_context, + ) end describe RailsAdmin::Config::Fields::Types::MultipleActiveStorage::ActiveStorageAttachment do + describe '#pretty_value' do + subject { field.pretty_value } + + context 'when attachment is not an image' do + let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: "test.txt", content_type: "text/plain"}] } + + it 'uses filename as link text' do + expect(Nokogiri::HTML(subject).text).to eq 'test.txt' + end + end + + context 'when the field is an image' do + let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"}] } + + it 'shows thumbnail image with a link' do + expect(Nokogiri::HTML(subject).css('img').attribute('src').value).to match(%r{rails/active_storage/representations}) + expect(Nokogiri::HTML(subject).css('a').attribute('href').value).to match(%r{rails/active_storage/blobs}) + end + end + end + describe '#image?' do context 'when attachment is an image' do let(:record) { FactoryBot.create :field_test, active_storage_assets: [{io: StringIO.new('dummy'), filename: "test.jpg", content_type: "image/jpeg"}] }