Skip to content

Commit

Permalink
Merge 633b8ce into 2b7b5fa
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 31, 2018
2 parents 2b7b5fa + 633b8ce commit e74d7e1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
21 changes: 14 additions & 7 deletions app/helpers/blacklight/url_helper_behavior.rb
Expand Up @@ -19,14 +19,21 @@ def url_for_document(doc, options = {})
# @example With the default document link field
# link_to_document(doc, counter: 3) #=> "<a href=\"catalog/123\" data-tracker-href=\"/catalog/123/track?counter=3&search_id=999\">My Title</a>
def link_to_document(doc, field_or_opts = nil, opts = { counter: nil })
if field_or_opts.is_a? Hash
opts = field_or_opts
else
field = field_or_opts
end
label = case field_or_opts
when NilClass
index_presenter(doc).label document_show_link_field(doc), opts
when Hash
opts = field_or_opts
index_presenter(doc).label document_show_link_field(doc), opts
when Proc, Symbol
Deprecation.warn(self, "passing a #{field_or_opts.class} to link_to_document is deprecated and will be removed in Blacklight 8")
Deprecation.silence(Blacklight::IndexPresenter) do
index_presenter(doc).label field_or_opts, opts
end
else # String
field_or_opts
end

field ||= document_show_link_field(doc)
label = index_presenter(doc).label field, opts
link_to label, url_for_document(doc), document_link_params(doc, opts)
end

Expand Down
4 changes: 4 additions & 0 deletions app/presenters/blacklight/index_presenter.rb
Expand Up @@ -29,8 +29,12 @@ def label(field_or_string_or_proc, opts = {})
config = field_config(field_or_string_or_proc)
document[field_or_string_or_proc]
when Proc
Deprecation.warn(self, "calling IndexPresenter.label with a Proc is deprecated. " \
"First argument must be a symbol. This will be removed in Blacklight 8")
field_or_string_or_proc.call(document, opts)
when String
Deprecation.warn(self, "calling IndexPresenter.label with a String is deprecated. " \
"First argument must be a symbol. This will be removed in Blacklight 8")
field_or_string_or_proc
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_index_header.html.erb
Expand Up @@ -15,7 +15,7 @@
<%= t('blacklight.search.documents.counter', counter: counter) %>
</span>
<% end %>
<%= link_to_document document, document_show_link_field(document), counter: counter %>
<%= link_to_document document, counter: counter %>
</h3>
<%= document_actions %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/catalog/_show_more_like_this.html.erb
@@ -1,3 +1,3 @@
<li class="more_like_this_document">
<span class="mlt_title"><%= link_to_document document, document_show_link_field(document) %></span>
<span class="mlt_title"><%= link_to_document document %></span>
</li>
6 changes: 5 additions & 1 deletion spec/helpers/blacklight/url_helper_behavior_spec.rb
Expand Up @@ -204,6 +204,7 @@
end

it "consists of the document title wrapped in a <a>" do
expect(Deprecation).to receive(:warn)
expect(helper.link_to_document(document, :title_tsim)).to have_selector("a", text: '654321', count: 1)
end

Expand All @@ -212,17 +213,20 @@
end

it "accepts and returns a Proc" do
expect(Deprecation).to receive(:warn).twice
expect(helper.link_to_document(document, proc { |doc, _opts| doc[:id] + ": " + doc.first(:title_tsim) })).to have_selector("a", text: '123456: 654321', count: 1)
end

context 'when label is missing' do
let(:data) { { 'id' => id } }

it "returns id" do
expect(Deprecation).to receive(:warn)
expect(helper.link_to_document(document, :title_tsim)).to have_selector("a", text: '123456', count: 1)
end

it "is html safe" do
expect(Deprecation).to receive(:warn)
expect(helper.link_to_document(document, :title_tsim)).to be_html_safe
end

Expand All @@ -245,7 +249,7 @@

it "converts the counter parameter into a data- attribute" do
allow(helper).to receive(:track_test_path).with(hash_including(id: have_attributes(id: '123456'), counter: 5)).and_return('tracking url')

expect(Deprecation).to receive(:warn)
expect(helper.link_to_document(document, :title_tsim, counter: 5)).to include 'data-context-href="tracking url"'
end

Expand Down

0 comments on commit e74d7e1

Please sign in to comment.