Skip to content

Commit

Permalink
Deprecate unnecessary conditions
Browse files Browse the repository at this point in the history
This makes the code easier to trace.
  • Loading branch information
jcoyne committed Oct 31, 2018
1 parent 721c3b3 commit 633b8ce
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
21 changes: 14 additions & 7 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ def url_for_document(doc, options = {})
# so we only need the +counter+ param here. We also need to know if we are viewing to document as part of search results.
# TODO: move this to the IndexPresenter
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
Original file line number Diff line number Diff line change
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
6 changes: 5 additions & 1 deletion spec/helpers/blacklight/url_helper_behavior_spec.rb
Original file line number Diff line number Diff line change
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 633b8ce

Please sign in to comment.