From 4e5531bc0e2427b487fdb546414befcd48700531 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Mon, 26 Jan 2015 14:07:41 -0800 Subject: [PATCH] Allow blacklight configuration to provide multiple title field options, and have --- .../configuration_helper_behavior.rb | 9 +++- lib/blacklight/document_presenter.rb | 18 +++++++- spec/lib/document_presenter_spec.rb | 44 +++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/app/helpers/blacklight/configuration_helper_behavior.rb b/app/helpers/blacklight/configuration_helper_behavior.rb index 6933844476..4c37209494 100644 --- a/app/helpers/blacklight/configuration_helper_behavior.rb +++ b/app/helpers/blacklight/configuration_helper_behavior.rb @@ -138,7 +138,14 @@ def spell_check_max # Used in the document list partial (search view) for creating a link to the document show action def document_show_link_field document=nil - blacklight_config.view_config(document_index_view_type).title_field.try(:to_sym) || document.id + fields = Array(blacklight_config.view_config(document_index_view_type).title_field) + + field = fields.first if document.nil? + field ||= fields.find { |f| document.has? f } + field &&= field.try(:to_sym) + field ||= document.id + + field end ## diff --git a/lib/blacklight/document_presenter.rb b/lib/blacklight/document_presenter.rb index 134fd84a08..7106366b2f 100644 --- a/lib/blacklight/document_presenter.rb +++ b/lib/blacklight/document_presenter.rb @@ -20,7 +20,14 @@ def initialize(document, controller, configuration = controller.blacklight_confi # @param [SolrDocument] document # @return [String] def document_heading - render_field_value(@document[@configuration.view_config(:show).title_field] || @document.id) + fields = Array(@configuration.view_config(:show).title_field) + f = fields.find { |field| @document.has? field } + + if f.nil? + render_field_value(@document.id) + else + render_field_value(@document[f]) + end end ## @@ -31,7 +38,14 @@ def document_heading # @return [String] def document_show_html_title if @configuration.view_config(:show).html_title_field - render_field_value(@document[@configuration.view_config(:show).html_title_field]) + fields = Array(@configuration.view_config(:show).html_title_field) + f = fields.find { |field| @document.has? field } + + if f.nil? + render_field_value(@document.id) + else + render_field_value(@document[f]) + end else document_heading end diff --git a/spec/lib/document_presenter_spec.rb b/spec/lib/document_presenter_spec.rb index 434fe78bf7..7bd89df515 100644 --- a/spec/lib/document_presenter_spec.rb +++ b/spec/lib/document_presenter_spec.rb @@ -213,4 +213,48 @@ expect(subject.render_field_value('a', double(:separator => nil, :itemprop => 'some-prop'))).to have_selector("span[@itemprop='some-prop']", :text => "a") end end + + describe "#document_heading" do + it "should fallback to an id" do + allow(document).to receive(:id).and_return "xyz" + expect(subject.document_heading).to eq document.id + end + + it "should return the value of the field" do + config.show.title_field = :x + allow(document).to receive(:has?).with(:x).and_return(true) + allow(document).to receive(:[]).with(:x).and_return("value") + expect(subject.document_heading).to eq "value" + end + + it "should return the first present value" do + config.show.title_field = [:x, :y] + allow(document).to receive(:has?).with(:x).and_return(false) + allow(document).to receive(:has?).with(:y).and_return(true) + allow(document).to receive(:[]).with(:y).and_return("value") + expect(subject.document_heading).to eq "value" + end + end + + describe "#document_show_html_title" do + it "should fallback to an id" do + allow(document).to receive(:id).and_return "xyz" + expect(subject.document_show_html_title).to eq document.id + end + + it "should return the value of the field" do + config.show.html_title_field = :x + allow(document).to receive(:has?).with(:x).and_return(true) + allow(document).to receive(:[]).with(:x).and_return("value") + expect(subject.document_show_html_title).to eq "value" + end + + it "should return the first present value" do + config.show.html_title_field = [:x, :y] + allow(document).to receive(:has?).with(:x).and_return(false) + allow(document).to receive(:has?).with(:y).and_return(true) + allow(document).to receive(:[]).with(:y).and_return("value") + expect(subject.document_show_html_title).to eq "value" + end + end end