Skip to content

Commit

Permalink
Move link_rel_alternates to the DocumentPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jul 31, 2015
1 parent 7d919a0 commit 08cc8a4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 13 deletions.
13 changes: 1 addition & 12 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,7 @@ def render_page_title
# @option options [Array<String>] :exclude array of format shortnames to not include in the output
def render_link_rel_alternates(document=@document, options = {})
return if document.nil?

options = { unique: false, exclude: [] }.merge(options)

seen = Set.new

safe_join(document.export_formats.map do |format, spec|
next if options[:exclude].include?(format) || (options[:unique] && seen.include?(spec[:content_type]))

seen.add(spec[:content_type])

tag(:link, rel: "alternate", title: format, type: spec[:content_type], href: polymorphic_url(document, format: format))
end.compact, "\n")
presenter(document).link_rel_alternates(options)
end

##
Expand Down
23 changes: 23 additions & 0 deletions lib/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ def document_heading
end
end

##
# Create <link rel="alternate"> links from a documents dynamically
# provided export formats. Returns empty string if no links available.
#
# @params [SolrDocument] document
# @params [Hash] options
# @option options [Boolean] :unique ensures only one link is output for every
# content type, e.g. as required by atom
# @option options [Array<String>] :exclude array of format shortnames to not include in the output
def link_rel_alternates(options = {})
options = { unique: false, exclude: [] }.merge(options)

seen = Set.new

safe_join(@document.export_formats.map do |format, spec|
next if options[:exclude].include?(format) || (options[:unique] && seen.include?(spec[:content_type]))

seen.add(spec[:content_type])

tag(:link, rel: "alternate", title: format, type: spec[:content_type], href: @controller.polymorphic_url(@document, format: format))
end.compact, "\n")
end

##
# Get the document's "title" to display in the <title> element.
# (by default, use the #document_heading)
Expand Down
76 changes: 75 additions & 1 deletion spec/lib/document_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
let(:request_context) { double(:add_facet_params => '') }
let(:config) { Blacklight::Configuration.new }

subject { Blacklight::DocumentPresenter.new(document, request_context, config) }
subject { presenter }
let(:presenter) { Blacklight::DocumentPresenter.new(document, request_context, config) }

let(:document) do
SolrDocument.new(id: 1,
Expand All @@ -15,6 +16,79 @@
'mnbv' => 'document mnbv value')
end

describe "link_rel_alternates" do
before do
class MockDocument
include Blacklight::Solr::Document
end

module MockExtension
def self.extended(document)
document.will_export_as(:weird, "application/weird")
document.will_export_as(:weirder, "application/weirder")
document.will_export_as(:weird_dup, "application/weird")
end
def export_as_weird ; "weird" ; end
def export_as_weirder ; "weirder" ; end
def export_as_weird_dup ; "weird_dup" ; end
end

MockDocument.use_extension(MockExtension)

def mock_document_app_helper_url *args
solr_document_url(*args)
end

allow(request_context).to receive(:polymorphic_url) do |_, opts|
"url.#{opts[:format]}"
end
end

let(:document) { MockDocument.new(id: "MOCK_ID1") }

context "with no arguments" do
subject { presenter.link_rel_alternates }

it "generates <link rel=alternate> tags" do
tmp_value = Capybara.ignore_hidden_elements
Capybara.ignore_hidden_elements = false
document.export_formats.each_pair do |format, spec|
expect(subject).to have_selector("link[href$='.#{ format }']") do |matches|
expect(matches).to have(1).match
tag = matches[0]
expect(tag.attributes["rel"].value).to eq "alternate"
expect(tag.attributes["title"].value).to eq format.to_s
expect(tag.attributes["href"].value).to eq mock_document_app_helper_url(document, format: format)
end
end
Capybara.ignore_hidden_elements = tmp_value
end

it { is_expected.to be_html_safe }
end

context "with unique: true" do
subject { presenter.link_rel_alternates(unique: true) }

it "respects unique: true" do
tmp_value = Capybara.ignore_hidden_elements
Capybara.ignore_hidden_elements = false
expect(subject).to have_selector("link[type='application/weird']", count: 1)
Capybara.ignore_hidden_elements = tmp_value
end
end

context "with exclude" do
subject { presenter.link_rel_alternates(unique: true) }
it "excludes formats from :exclude" do
tmp_value = Capybara.ignore_hidden_elements
Capybara.ignore_hidden_elements = false
expect(subject).to_not have_selector("link[href$='.weird_dup']")
Capybara.ignore_hidden_elements = tmp_value
end
end
end

describe "render_index_field_value" do
let(:config) do
Blacklight::Configuration.new.configure do |config|
Expand Down

0 comments on commit 08cc8a4

Please sign in to comment.