Skip to content

Commit

Permalink
Merge pull request #3295 from RongRongTeng/feature/show-correct-file-…
Browse files Browse the repository at this point in the history
…name-for-multiple-attachments

Show Correct Filename For Multiple Attachments
  • Loading branch information
mshibuya authored Feb 21, 2021
2 parents b514455 + a594665 commit 0df71c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/rails_admin/config/fields/types/multiple_file_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}] }
Expand Down

0 comments on commit 0df71c9

Please sign in to comment.