Skip to content

Commit

Permalink
Show a dash '-' with an empty association
Browse files Browse the repository at this point in the history
Closes #2681
  • Loading branch information
mshibuya committed Oct 9, 2021
1 parent 798ab1b commit 7580f33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rails_admin/config/fields/association.rb
Expand Up @@ -18,7 +18,7 @@ def association
wording = associated.send(amc.object_label_method)
can_see = !am.embedded? && (show_action = v.action(:show, am, associated))
can_see ? v.link_to(wording, v.url_for(action: show_action.action_name, model_name: am.to_param, id: associated.id), class: 'pjax') : ERB::Util.html_escape(wording)
end.to_sentence.html_safe
end.to_sentence.html_safe.presence || '-'
end

# Accessor whether association is visible or not. By default
Expand Down
13 changes: 12 additions & 1 deletion spec/rails_admin/config/fields/association_spec.rb
Expand Up @@ -4,15 +4,26 @@
describe '#pretty_value' do
let(:player) { FactoryBot.create(:player, name: '<br />', team: FactoryBot.create(:team)) }
let(:field) { RailsAdmin.config('Team').fields.detect { |f| f.name == :players } }
let(:view) { ActionView::Base.empty.tap { |d| allow(d).to receive(:action).and_return(nil) } }
let(:view) { ActionView::Base.empty }
subject { field.with(object: player.team, view: view).pretty_value }

context 'when the link is disabled' do
let(:view) { ActionView::Base.empty.tap { |d| allow(d).to receive(:action).and_return(nil) } }

it 'does not expose non-HTML-escaped string' do
is_expected.to be_html_safe
is_expected.to eq '&lt;br /&gt;'
end
end

context 'when the value is empty' do
let(:team) { FactoryBot.build :team }
subject { field.with(object: team, view: view).pretty_value }

it "returns '-' to show emptiness" do
is_expected.to eq '-'
end
end
end

describe '#removable?', active_record: true do
Expand Down

0 comments on commit 7580f33

Please sign in to comment.