Skip to content

Commit

Permalink
Deprecate SearchHistoryConstraintsHelperBehavior methods in favor of …
Browse files Browse the repository at this point in the history
…using the ConstraintsComponent for greater consistency
  • Loading branch information
cbeer committed Feb 24, 2022
1 parent d167e9c commit 3dadf51
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@
# Includes methods for rendering more textually on Search History page
# (render_search_to_s(_*))
module Blacklight::SearchHistoryConstraintsHelperBehavior
extend Deprecation
self.deprecation_horizon = '8.0'

# Simpler textual version of constraints, used on Search History page.
# Theoretically can may be DRY'd up with results page render_constraints,
# maybe even using the very same HTML with different CSS?
# But too tricky for now, too many changes to existing CSS. TODO.
def render_search_to_s(params)
render_search_to_s_q(params) +
render_search_to_s_filters(params)
return render(Blacklight::ConstraintsComponent.for_search_history(search_state: convert_to_search_state(params))) unless overridden_search_history_constraints_helper_methods?

Deprecation.warn(Blacklight::SearchHistoryConstraintsHelperBehavior, 'Calling out to potentially overridden helpers for backwards compatibility.')

Deprecation.silence(Blacklight::SearchHistoryConstraintsHelperBehavior) do
render_search_to_s_q(params) +
render_search_to_s_filters(params)
end
end
deprecation_deprecate render_search_to_s: 'Use Blacklight::ConstraintsComponent.for_search_history instead'

##
# Render the search query constraint
def render_search_to_s_q(params)
Deprecation.warn(Blacklight::SearchHistoryConstraintsHelperBehavior, '#render_search_to_s_q is deprecated without replacement')
return "".html_safe if params['q'].blank?

label = label_for_search_field(params[:search_field]) unless default_search_field?(params[:search_field])
Expand All @@ -28,6 +39,7 @@ def render_search_to_s_q(params)
##
# Render the search facet constraints
def render_search_to_s_filters(params)
Deprecation.warn(Blacklight::SearchHistoryConstraintsHelperBehavior, '#render_search_to_s_filters is deprecated without replacement')
return "".html_safe unless params[:f]

safe_join(params[:f].collect do |facet_field, value_list|
Expand All @@ -43,13 +55,15 @@ def render_search_to_s_filters(params)
# 'and'. Pass in option :escape_value => false to pass in pre-rendered
# html for value. key with escape_key if needed.
def render_search_to_s_element(key, value, _options = {})
Deprecation.warn(Blacklight::SearchHistoryConstraintsHelperBehavior, '#render_search_to_s_element is deprecated without replacement')
tag.span(render_filter_name(key) + tag.span(value, class: 'filter-values'),
class: 'constraint')
end

##
# Render the name of the facet
def render_filter_name name
Deprecation.warn(Blacklight::SearchHistoryConstraintsHelperBehavior, '#render_filter_name is deprecated without replacement')
return "".html_safe if name.blank?

tag.span(t('blacklight.search.filters.label', label: name),
Expand All @@ -59,11 +73,25 @@ def render_filter_name name
##
# Render the value of the facet
def render_filter_value value, key = nil
Deprecation.warn(Blacklight::SearchHistoryConstraintsHelperBehavior, '#render_filter_value is deprecated without replacement')
display_value = value
Deprecation.silence(Blacklight::FacetsHelperBehavior) do
display_value = facet_display_value(key, value) if key
end
tag.span(h(display_value),
class: 'filter-value')
end

private

# Check if the downstream application has overridden these methods
# @deprecated
# @private
def overridden_search_history_constraints_helper_methods?
method(:render_search_to_s_q).owner != Blacklight::FacetsHelperBehavior ||
method(:render_search_to_s_filters).owner != Blacklight::FacetsHelperBehavior ||
method(:render_search_to_s_element).owner != Blacklight::FacetsHelperBehavior ||
method(:render_filter_name).owner != Blacklight::FacetsHelperBehavior ||
method(:render_filter_value).owner != Blacklight::FacetsHelperBehavior
end
end
4 changes: 3 additions & 1 deletion app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ def link_back_to_catalog(opts = { label: nil })

# Search History and Saved Searches display
def link_to_previous_search(params)
link_to(render_search_to_s(params), search_action_path(params))
Deprecation.silence(Blacklight::SearchHistoryConstraintsHelperBehavior) do
link_to(render_search_to_s(params), search_action_path(params))
end
end

# Get url parameters to a search within a grouped result set
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Blacklight::SearchHistoryConstraintsHelperBehavior do
around { |test| Deprecation.silence(described_class) { test.call } }

before(:all) do
@config = Blacklight::Configuration.new do |config|
config.add_search_field 'default_search_field', label: 'Default'
Expand Down
1 change: 1 addition & 0 deletions spec/views/catalog/index.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

describe "with search parameters" do
before do
allow(controller).to receive(:search_state_class).and_return(Blacklight::SearchState)
allow(view).to receive(:has_search_parameters?).and_return(true)
stub_template "catalog/_results_pagination.html.erb" => ""
stub_template "catalog/_search_header.html.erb" => "header_content"
Expand Down

0 comments on commit 3dadf51

Please sign in to comment.