Skip to content

Commit

Permalink
Merge pull request #2991 from projectblacklight/backport-2884
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne authored Feb 6, 2023
2 parents 6381931 + 6013132 commit 504184c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/views/catalog/_show_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<%= render 'show_tools' %>
<%= render 'show_tools', document: document %>
<%= render(Blacklight::Document::MoreLikeThisComponent.new(document: document)) %>
7 changes: 4 additions & 3 deletions app/views/catalog/_show_tools.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<% if show_doc_actions? %>
<% document = @document if local_assigns[:document].nil? %>
<% if show_doc_actions? document %>
<div class="card show-tools">
<div class="card-header">
<h2 class="mb-0 h6"><%= t('blacklight.tools.title') %></h2>
</div>
<% if render_show_doc_actions_method_from_blacklight? %>
<%= render(Blacklight::Document::ActionsComponent.new(document: @document, tag: 'ul', classes: 'list-group list-group-flush', wrapping_tag: 'li', wrapping_classes: 'list-group-item', actions: document_actions(@document), url_opts: Blacklight::Parameters.sanitize(params.to_unsafe_h))) %>
<%= render(Blacklight::Document::ActionsComponent.new(document: document, tag: 'ul', classes: 'list-group list-group-flush', wrapping_tag: 'li', wrapping_classes: 'list-group-item', actions: document_actions(document), url_opts: Blacklight::Parameters.sanitize(params.to_unsafe_h))) %>
<% else %>
<% Deprecation.warn(self, '#render_show_doc_actions is deprecated; use ActionComponents instead') %>
<ul class="list-group list-group-flush">
<%= render_show_doc_actions @document do |config, inner| %>
<%= render_show_doc_actions document do |config, inner| %>
<li class="list-group-item <%= config.key %>">
<%= inner %>
</li>
Expand Down
34 changes: 24 additions & 10 deletions spec/views/catalog/_show_tools.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,81 @@

before do
assign :response, instance_double(Blacklight::Solr::Response, params: {})
assign :document, document
allow(view).to receive(:blacklight_config).and_return blacklight_config
allow(view).to receive(:has_user_authentication_provider?).and_return false
end

context 'without passing in a document local explicitly' do
before do
assign :document, document
end

let(:document_actions) { blacklight_config.show.document_actions }

it 'defaults to the @document and renders the action' do
allow(view).to receive(:some_action_solr_document_path).with(document, any_args).and_return 'x'
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action'
render 'catalog/show_tools'
expect(rendered).to have_link "Some action", href: "x"
end
end

describe "document actions" do
let(:document_actions) { blacklight_config.show.document_actions }

it "renders a document action" do
allow(view).to receive(:some_action_solr_document_path).with(document, any_args).and_return 'x'
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action'
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).to have_link "Some action", href: "x"
end

it "uses the provided label" do
allow(view).to receive(:some_action_solr_document_path).and_return "x"
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, label: "Some label", partial: 'document_action'
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).to have_selector '.some_action', text: "Some label"
end

it "evaluates a document action's if configurations" do
allow(view).to receive(:some_action_solr_document_path).and_return "x"
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, if: false, partial: 'document_action'
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).not_to have_selector '.some_action', text: "Some action"
end

it "evaluates a document action's if configuration with a proc" do
allow(view).to receive(:some_action_solr_document_path).and_return "x"
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', if: proc { |_config, doc| doc.id == "xyz" }
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).not_to have_selector '.some_action', text: "Some action"
end

it "evaluates a document action's unless configurations" do
allow(view).to receive(:some_action_solr_document_path).and_return "x"
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', unless: true
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).not_to have_selector '.some_action', text: "Some action"
end

it "allows the tool to have a custom id" do
allow(view).to receive(:some_action_solr_document_path).and_return "x"
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', id: "some_action"
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).to have_selector '#some_action', text: "Some action"
end

it "defaults to modal behavior" do
allow(view).to receive(:some_action_solr_document_path).and_return "x"
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action'
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).to have_selector '.some_action > a[data-blacklight-modal="trigger"]', text: "Some action"
end

it "allows configuration to opt out of modal behavior" do
allow(view).to receive(:some_action_solr_document_path).and_return "x"
document_actions[:some_action] = Blacklight::Configuration::ToolConfig.new key: :some_action, name: :some_action, partial: 'document_action', modal: false
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document
expect(rendered).not_to have_selector '.some_action > a[data-blacklight-modal="trigger"]', text: "Some action"
end

Expand All @@ -76,7 +90,7 @@
end

it 'does not display the tools' do
render partial: 'catalog/show_tools'
render 'catalog/show_tools', document: document

expect(rendered).to be_blank
end
Expand Down

0 comments on commit 504184c

Please sign in to comment.