-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate constraints to viewcomponents (#3361)
Resolves DEPRECATION WARNING: constraint_query_label is deprecated
- Loading branch information
1 parent
c3f288a
commit a1adaa6
Showing
15 changed files
with
183 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= render(@layout.new( | ||
classes: (Array(@classes) + ["filter-#{@facet_item_presenter.key.parameterize}"]).join(' '), | ||
label: @facet_item_presenter.field_label, | ||
value: constraint_value, | ||
remove_path: @facet_item_presenter.remove_href)) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class Orangelight::ConstraintComponent < Blacklight::ConstraintComponent | ||
def constraint_value | ||
if old_style_params? | ||
@facet_item_presenter.constraint_label['0'] | ||
elsif @facet_item_presenter.constraint_label.respond_to? :to_s | ||
@facet_item_presenter.constraint_label | ||
end | ||
end | ||
|
||
private | ||
|
||
def old_style_params? | ||
@facet_item_presenter.constraint_label.is_a?(ActiveSupport::HashWithIndifferentAccess) && | ||
@facet_item_presenter.constraint_label.key?('0') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<%= content_tag @tag || :div, id: @id, class: @classes do %> | ||
<% if @render_headers %> | ||
<h2 class="sr-only visually-hidden"><%= t('blacklight.search.search_constraints_header') %></h2> | ||
<% end %> | ||
|
||
<div class="button--start-over"> | ||
<%= render_start_over_link(start_over_path) %> | ||
</div> | ||
<div class="button--edit-search"> | ||
<%=link_to t('blacklight.search.edit_search'), | ||
edit_search_link, | ||
class: "catalog_editSearchLink btn btn-outline-secondary", | ||
id: "editSearchLink" %> | ||
</div> | ||
|
||
<% if @render_headers %> | ||
<span class="constraints-label sr-only visually-hidden"><%= t('blacklight.search.filters.title') %></span> | ||
<% end %> | ||
<% if query_constraints_area.present? %> | ||
<% query_constraints_area.each do |constraint| %> | ||
<%= constraint %> | ||
<% end %> | ||
<% else %> | ||
<%= query_constraints %> | ||
<% end %> | ||
<% if facet_constraints_area.present? %> | ||
<% facet_constraints_area.each do |constraint| %> | ||
<%= constraint %> | ||
<% end %> | ||
<% else %> | ||
<%= facet_constraints %> | ||
<% end %> | ||
<% additional_constraints.each do |constraints| %> | ||
<%= constraints %> | ||
<% end %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
class Orangelight::ConstraintsComponent < Blacklight::ConstraintsComponent | ||
# rubocop:disable Metrics/ParameterLists | ||
def initialize(search_state:, | ||
tag: :div, | ||
render_headers: true, | ||
id: 'appliedParams', classes: 'clearfix constraints-container', | ||
query_constraint_component: Blacklight::ConstraintLayoutComponent, | ||
query_constraint_component_options: {}, | ||
facet_constraint_component: Orangelight::ConstraintComponent, | ||
facet_constraint_component_options: {}, | ||
start_over_component: Blacklight::StartOverButtonComponent) | ||
super | ||
end | ||
# rubocop:enable Metrics/ParameterLists | ||
|
||
def query_constraints | ||
super + guided_search_constraints | ||
end | ||
|
||
def remove_guided_query_path(index) | ||
url_for @search_state.to_h.reject { |k, _v| k.match?(/[f|q|op]#{index}/) } | ||
end | ||
|
||
def render? | ||
super || @search_state.to_h.keys.any? { |param| param.match?(/[f|q|op][1-3]/) } | ||
end | ||
|
||
private | ||
|
||
def guided_search_constraints | ||
constraints_string = ''.html_safe | ||
(1..3).each do |index| | ||
constraints_string += helpers.render( | ||
@query_constraint_component.new( | ||
search_state: @search_state, | ||
value: guided_search_value(index), | ||
label: guided_search_label(index), | ||
remove_path: remove_guided_query_path(index), | ||
classes: 'query', | ||
**@query_constraint_component_options | ||
) | ||
) | ||
end | ||
constraints_string | ||
end | ||
|
||
def guided_search_value(index) | ||
params = @search_state.to_h | ||
return if params[:"q#{index}"].blank? | ||
|
||
has_operator = params[:"op#{index}"].present? | ||
prefix = has_operator ? params[:"op#{index}"].upcase + ' ' : '' | ||
prefix + params[:"q#{index}"] | ||
end | ||
|
||
def guided_search_label(index) | ||
search_field = @search_state.params[:"f#{index}"] | ||
helpers.label_for_search_field(search_field) unless helpers.default_search_field?(search_field) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Orangelight::ConstraintComponent, type: :component do | ||
let(:facet_item_presenter) { instance_double(Blacklight::FacetItemPresenter) } | ||
|
||
it "can handle the odd format[0]-style params" do | ||
allow(facet_item_presenter).to receive(:key).and_return('key') | ||
allow(facet_item_presenter).to receive(:field_label).and_return('Format') | ||
allow(facet_item_presenter).to receive(:constraint_label).and_return({ "0" => "Audio" }.with_indifferent_access) | ||
allow(facet_item_presenter).to receive(:remove_href).and_return('/bye') | ||
|
||
component = described_class.new(facet_item_presenter:) | ||
expect(render_inline(component).to_s).to include('Format: Audio') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe Orangelight::ConstraintsComponent, type: :component do | ||
let(:params) do | ||
{ | ||
action: 'index', controller: 'catalog', | ||
q1: 'dogs', f1: 'title', | ||
op2: 'not', q2: 'cats', f2: 'title', | ||
op3: '', q3: '', f3: '' | ||
} | ||
end | ||
let(:search_state) { Blacklight::SearchState.new(params, Blacklight::Configuration.new) } | ||
let(:component) { described_class.new(search_state:) } | ||
|
||
describe '#remove_guided_query_path' do | ||
it 'generates a valid path for removing guided search queries' do | ||
render_inline(component) | ||
expect(component.remove_guided_query_path(1)).to eq( | ||
'/?f2=title&f3=&op2=not&op3=&q2=cats&q3=' | ||
) | ||
end | ||
end | ||
describe 'advanced search query constraints' do | ||
let(:rendered) { Capybara::Node::Simple.new(render_inline(component).to_s) } | ||
it 'includes constraints for non-empty search params' do | ||
expect(rendered).to have_selector('.applied-filter.constraint', text: 'Title: dogs') | ||
expect(rendered).to have_selector('.applied-filter.constraint', text: 'Title: NOT cats') | ||
expect(rendered).to have_selector('.applied-filter.constraint', count: 2) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters