Skip to content

Commit

Permalink
Allow blacklight configuration to provide multiple title field option…
Browse files Browse the repository at this point in the history
…s, and have
  • Loading branch information
cbeer committed Jan 26, 2015
1 parent 072e64f commit 4e5531b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/helpers/blacklight/configuration_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down
18 changes: 16 additions & 2 deletions lib/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand All @@ -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
Expand Down
44 changes: 44 additions & 0 deletions spec/lib/document_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4e5531b

Please sign in to comment.