Skip to content

Commit

Permalink
Refactor #render_document_heading to support any document-like object
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 26, 2015
1 parent 5f7c0ca commit 1286818
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
22 changes: 14 additions & 8 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,23 +266,29 @@ def document_show_html_title document=nil
##
# Render the document "heading" (title) in a content tag
# @overload render_document_heading(tag)
# @params [Symbol] tag
# @overload render_document_heading(document, options)
# @params [SolrDocument] document
# @params [Hash] options
# @options options [Symbol] :tag
# @overload render_document_heading(options)
# @params [Hash] options
# @options options [Symbol] :tag
def render_document_heading(*args)
options = args.extract_options!
if args.first.is_a? blacklight_config.document_model
document = args.shift
tag = options[:tag]

tag_or_document = args.first

if tag_or_document.is_a? String or tag_or_document.is_a? Symbol
Deprecation.warn(Blacklight::BlacklightHelperBehavior, "#render_document_heading with a tag argument is deprecated; pass e.g. `tag: :h4` instead")
tag = tag_or_document
document = @document
else
document = nil
tag = args.first || options[:tag]
tag = options.fetch(:tag, :h4)
document = tag_or_document || @document
end

tag ||= :h4

content_tag(tag, presenter(document).document_heading, :itemprop => "name")
content_tag(tag, presenter(document).document_heading, itemprop: "name")
end

##
Expand Down
33 changes: 33 additions & 0 deletions spec/helpers/blacklight_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,37 @@ def mock_document_app_helper_url *args
expect(helper.presenter_class).to eq Blacklight::DocumentPresenter
end
end

describe "#render_document_heading" do
before do
allow(helper).to receive(:presenter).and_return(double(document_heading: "Heading"))
end

let(:document) { double }

it "should accept no arguments and render the document heading" do
expect(helper.render_document_heading).to have_selector "h4", text: "Heading"
end

it "should accept a tag name and render the document heading" do
Deprecation.silence(Blacklight::BlacklightHelperBehavior) do
expect(helper.render_document_heading(:h3)).to have_selector "h3", text: "Heading"
expect(helper.render_document_heading("h2")).to have_selector "h2", text: "Heading"
end
end

it "should accept the tag name as an option" do
expect(helper.render_document_heading tag: "h1").to have_selector "h1", text: "Heading"
end

it "should accept an explicit document argument" do
allow(helper).to receive(:presenter).with(document).and_return(double(document_heading: "Document Heading"))
expect(helper.render_document_heading(document)).to have_selector "h4", text: "Document Heading"
end

it "should accept the document with a tag option" do
allow(helper).to receive(:presenter).with(document).and_return(double(document_heading: "Document Heading"))
expect(helper.render_document_heading(document, tag: "h3")).to have_selector "h3", text: "Document Heading"
end
end
end

0 comments on commit 1286818

Please sign in to comment.