Skip to content

Commit

Permalink
Merge pull request #3721 from pulibrary/i3703-constraints
Browse files Browse the repository at this point in the history
i3703: display constraints for new advanced search results
  • Loading branch information
christinach committed Sep 7, 2023
2 parents 04f1615 + a073e68 commit 35a2de8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
11 changes: 10 additions & 1 deletion app/components/orangelight/constraints_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def remove_guided_query_path(index)
end

def render?
super || @search_state.to_h.keys.any? { |param| param.match?(/[f|q|op][1-3]/) }
@search_state.has_constraints? || @search_state.to_h.keys.any? { |param| param.match?(/[f|q|op][1-3]/) }
end

private
Expand Down Expand Up @@ -59,4 +59,13 @@ 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

def clause_presenters
return to_enum(:clause_presenters) unless block_given?

@search_state.clause_params.each do |key, clause|
field_config = helpers.blacklight_config.search_fields[clause[:field]]
yield Orangelight::ClausePresenter.new(key, clause, field_config, helpers)
end
end
end
12 changes: 12 additions & 0 deletions app/presenters/orangelight/clause_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
module Orangelight
class ClausePresenter < Blacklight::ClausePresenter
def label
if user_parameters[:op] == 'must_not'
"NOT #{super}"
else
super
end
end
end
end
23 changes: 22 additions & 1 deletion spec/components/orangelight/constraints_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
end
let(:search_state) { Blacklight::SearchState.new(params, Blacklight::Configuration.new) }
let(:component) { described_class.new(search_state:) }
let(:rendered) { Capybara::Node::Simple.new(render_inline(component).to_s) }

describe '#remove_guided_query_path' do
it 'generates a valid path for removing guided search queries' do
Expand All @@ -23,11 +24,31 @@
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

describe 'json query constraints' do
let(:params) do
{
action: 'index', controller: 'catalog',
'clause' => {
'0' => {
field: 'all_fields', query: 'cats'
},
'1' => {
field: 'title', query: 'dogs', op: 'must_not'
}
}
}
end
it 'includes constraints for non-empty search params' do
expect(rendered).to have_selector('.applied-filter.constraint', text: 'cats')
expect(rendered).to have_selector('.applied-filter.constraint', text: 'Title: NOT dogs')
expect(rendered).to have_selector('.applied-filter.constraint', count: 2)
end
end
end
1 change: 0 additions & 1 deletion spec/features/advanced_searching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
end

it 'shows constraint-value on search results page' do
pending('Display constraint-value on search result page from built-in advanced search')
# defaults to keyword
fill_in(id: 'clause_0_query', with: 'gay')
choose(id: 'clause_2_op_must_not')
Expand Down
19 changes: 19 additions & 0 deletions spec/presenters/orangelight/clause_presenter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Orangelight::ClausePresenter do
subject(:presenter) do
described_class.new('0', params.with_indifferent_access.dig(:clause, '0'), field_config, nil, search_state)
end

let(:field_config) { Blacklight::Configuration::NullField.new key: 'some_field' }
let(:search_state) { Blacklight::SearchState.new(params.with_indifferent_access, Blacklight::Configuration.new) }
let(:params) { { clause: { '0' => { query: 'some search string', op: 'must_not' } } } }

describe '#label' do
it 'includes the NOT boolean operator if appropriate' do
expect(subject.label).to eq 'NOT some search string'
end
end
end

0 comments on commit 35a2de8

Please sign in to comment.