Skip to content

Commit

Permalink
Add a second argument to #document_partial_name for the current kind …
Browse files Browse the repository at this point in the history
…of partial we're rendering

This allows downstream implementors to override the calculated
 #document_partial_name on a per-partial basis. E.g. if you had a
heterogeneous index with some records with MODS and MARC data, you could
choose the appropriate rendering format for those records independent of
the regular format.
  • Loading branch information
cbeer committed Mar 12, 2014
1 parent 8cf317b commit b3301be
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Expand Up @@ -428,19 +428,25 @@ def document_index_path_templates

##
# Return a normalized partial name for rendering a single document
#
#
# @param [SolrDocument]
# @param [Symbol] base name for the partial
# @return [String]
def document_partial_name(document)
display_type = document[blacklight_config.view_config(:show).display_type_field]
def document_partial_name(document, base_name = nil)
view_config = blacklight_config.view_config(:show)

display_type = if base_name and view_config.has_key? :"#{base_name}_display_type_field"
document[view_config[:"#{base_name}_display_type_field"]]
end

display_type ||= document[view_config.display_type_field]

return 'default' unless display_type
display_type = display_type.join(" ") if display_type.respond_to?(:join)
display_type ||= 'default'

# .to_s is necessary otherwise the default return value is not always a string
# using "_" as sep. to more closely follow the views file naming conventions
# parameterize uses "-" as the default sep. which throws errors
"#{display_type.gsub("-"," ")}".parameterize("_").to_s
Array(display_type).join(" ").gsub("-","_").parameterize("_")
end

##
Expand All @@ -466,7 +472,7 @@ def render_document_partials(doc, partials = [], locals ={})
# @param [String] base name for the partial
# @param [Hash] locales to pass through to the partials
def render_document_partial(doc, base_name, locals = {})
format = document_partial_name(doc)
format = document_partial_name(doc, base_name)

document_partial_path_templates.each do |str|
# XXX rather than handling this logic through exceptions, maybe there's a Rails internals method
Expand Down
47 changes: 47 additions & 0 deletions spec/helpers/blacklight_helper_spec.rb
Expand Up @@ -445,4 +445,51 @@ def mock_document_app_helper_url *args
expect(helper.should_show_spellcheck_suggestions? response).to be_false
end
end

describe "#document_partial_name" do
let(:blacklight_config) { Blacklight::Configuration.new }

before do
helper.stub(blacklight_config: blacklight_config)
end

context "with a solr document with empty fields" do
let(:document) { SolrDocument.new }
it "should be the default value" do
expect(helper.document_partial_name(document)).to eq 'default'
end
end

context "with a solr document with the display type field set" do
let(:document) { SolrDocument.new 'my_field' => 'xyz'}

before do
blacklight_config.show.display_type_field = 'my_field'
end

it "should use the value in the configured display type field" do
expect(helper.document_partial_name(document)).to eq 'xyz'
end

it "should use the value in the configured display type field if the action-specific field is empty" do
expect(helper.document_partial_name(document, :some_action)).to eq 'xyz'
end
end

context "with a solr doucment with an action-specific field set" do

let(:document) { SolrDocument.new 'my_field' => 'xyz', 'other_field' => 'abc' }

before do
blacklight_config.show.media_display_type_field = 'my_field'
blacklight_config.show.metadata_display_type_field = 'other_field'
end

it "should use the value in the action-specific fields" do
expect(helper.document_partial_name(document, :media)).to eq 'xyz'
expect(helper.document_partial_name(document, :metadata)).to eq 'abc'
end

end
end
end

0 comments on commit b3301be

Please sign in to comment.