Skip to content

Commit 8dcb7cf

Browse files
authored
Merge pull request #3608 from projectblacklight/simplify-facet-search
Simplify FacetSuggestInput by only passing the presenter argument
2 parents 5117011 + 300f250 commit 8dcb7cf

File tree

9 files changed

+62
-48
lines changed

9 files changed

+62
-48
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<label for="facet_suggest_<%= facet.key %>">
2-
<%= I18n.t('blacklight.search.facets.suggest.label', field_label: presenter&.label) %>
1+
<label for="facet_suggest_<%= key %>">
2+
<%= I18n.t('blacklight.search.facets.suggest.label', field_label: label) %>
33
</label>
4-
<%= text_field_tag "facet_suggest_#{facet.key}",
4+
<%= text_field_tag "facet_suggest_#{key}",
55
nil,
66
class: "facet-suggest form-control mb-3",
77
data: {
8-
facet_field: facet.key,
9-
facet_search_context: presenter.view_context.search_facet_path(id: facet.key)
8+
facet_field: key,
9+
facet_search_context: helpers.search_facet_path(id: key)
1010
},
1111
placeholder: I18n.t('blacklight.search.form.search.placeholder')
1212
%>

app/components/blacklight/search/facet_suggest_input.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
module Blacklight
44
module Search
55
class FacetSuggestInput < Blacklight::Component
6-
def initialize(facet:, presenter:)
7-
@facet = facet
6+
def initialize(presenter:)
87
@presenter = presenter
98
end
109

1110
private
1211

13-
attr_accessor :facet, :presenter
12+
attr_accessor :presenter
13+
14+
delegate :suggest, :key, :label, to: :presenter
1415

1516
def render?
16-
facet&.suggest != false
17+
# Draw if suggest is true or not present
18+
suggest != false
1719
end
1820
end
1921
end

app/controllers/concerns/blacklight/catalog.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module Blacklight::Catalog
1515
included do
1616
if respond_to? :helper_method
1717
helper_method :sms_mappings, :has_search_parameters?
18-
helper_method :search_facet_path
1918
end
2019

2120
record_search_parameters
@@ -138,15 +137,6 @@ def has_search_parameters?
138137
params[:search_field].present? || search_state.has_constraints?
139138
end
140139

141-
def search_facet_path(options = {})
142-
opts = search_state
143-
.to_h
144-
.merge(action: "facet", only_path: true)
145-
.merge(options)
146-
.except(:page)
147-
url_for opts
148-
end
149-
150140
private
151141

152142
#

app/helpers/blacklight/facets_helper_behavior.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ def facet_field_presenter(facet_config, display_facet)
77
facet_config.presenter.new(facet_config, display_facet, self)
88
end
99

10+
def search_facet_path(options = {})
11+
opts = search_state
12+
.to_h
13+
.merge(action: "facet", only_path: true)
14+
.merge(options)
15+
.except(:page)
16+
url_for opts
17+
end
18+
1019
private
1120

1221
def facet_value_for_facet_item item

app/presenters/blacklight/facet_field_presenter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Blacklight
44
class FacetFieldPresenter
55
attr_reader :facet_field, :display_facet, :view_context, :search_state
66

7-
delegate :key, to: :facet_field
7+
delegate :key, :suggest, to: :facet_field
88
delegate :field_name, to: :display_facet
99

1010
def initialize(facet_field, display_facet, view_context, search_state = view_context.search_state)

app/views/catalog/facet.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<% view_config = blacklight_config.view_config(document_index_view_type) %>
44

55
<div class="facet-filters card card-body bg-light p-3 mb-3 border-0">
6-
<%= render Blacklight::Search::FacetSuggestInput.new(facet: @facet, presenter: @presenter) %>
6+
<%= render Blacklight::Search::FacetSuggestInput.new(presenter: @presenter) %>
77
<%= render partial: 'facet_index_navigation' if @facet.index_range && @display_facet.index? %>
88
</div>
99

spec/components/blacklight/search/facet_suggest_input_spec.rb

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,65 @@
44

55
RSpec.describe Blacklight::Search::FacetSuggestInput, type: :component do
66
let(:facet) { Blacklight::Configuration::FacetField.new key: 'language_facet', suggest: true }
7-
let(:presenter) { instance_double(Blacklight::FacetFieldPresenter) }
8-
let(:view_context) { double(ActionView::Base) }
7+
let(:presenter) { Blacklight::FacetFieldPresenter.new(facet, nil, nil, nil) }
8+
let(:component) { described_class.new(presenter: presenter) }
99

1010
before do
11-
allow(presenter).to receive_messages(label: 'Language', view_context: view_context)
12-
allow(view_context).to receive(:search_facet_path).and_return('/catalog/facet/language_facet')
11+
allow(presenter).to receive_messages(label: 'Language')
1312
end
1413

1514
it 'has an input with the facet-suggest class, which the javascript needs to find it' do
16-
rendered = render_inline(described_class.new(facet: facet, presenter: presenter))
17-
expect(rendered.css("input.facet-suggest").count).to eq 1
15+
with_request_url "/catalog/facet/language_facet" do
16+
rendered = render_inline component
17+
expect(rendered.css("input.facet-suggest").count).to eq 1
18+
end
1819
end
1920

2021
it 'has an input with the data-facet-field attribute, which the javascript needs to determine the correct query' do
21-
rendered = render_inline(described_class.new(facet: facet, presenter: presenter))
22-
expect(rendered.css('input[data-facet-field="language_facet"]').count).to eq 1
22+
with_request_url "/catalog/facet/language_facet" do
23+
rendered = render_inline component
24+
expect(rendered.css('input[data-facet-field="language_facet"]').count).to eq 1
25+
end
2326
end
2427

2528
it 'has an input with the data-facet-search-context attribute, which the javascript needs to determine the current search context' do
26-
allow(view_context).to receive(:search_facet_path).and_return('/catalog/facet/language_facet?f%5Bformat%5D%5B%5D=Book&facet.prefix=R&facet.sort=index&q=tibet&search_field=all_fields')
27-
rendered = render_inline(described_class.new(facet: facet, presenter: presenter))
28-
expect(rendered.css('input[data-facet-search-context="/catalog/facet/language_facet?f%5Bformat%5D%5B%5D=Book&facet.prefix=R&facet.sort=index&q=tibet&search_field=all_fields"]').count).to eq 1
29+
with_request_url "/catalog/facet/language_facet?f%5Bformat%5D%5B%5D=Book&facet.prefix=R&facet.sort=index&q=tibet&search_field=all_fields" do
30+
rendered = render_inline component
31+
expect(rendered.css('input[data-facet-search-context="/catalog/facet/language_facet.html?f%5Bformat%5D%5B%5D=Book&facet.prefix=R&facet.sort=index&q=tibet&search_field=all_fields"]').count).to eq 1
32+
end
2933
end
3034

3135
it 'has a visible label that is associated with the input' do
32-
rendered = render_inline(described_class.new(facet: facet, presenter: presenter))
33-
label = rendered.css('label').first
34-
expect(label.text.strip).to eq 'Filter Language'
36+
with_request_url "/catalog/facet/language_facet" do
37+
rendered = render_inline component
38+
label = rendered.css('label').first
39+
expect(label.text.strip).to eq 'Filter Language'
3540

36-
id_in_label_for = label.attribute('for').text
37-
expect(id_in_label_for).to eq('facet_suggest_language_facet')
41+
id_in_label_for = label.attribute('for').text
42+
expect(id_in_label_for).to eq('facet_suggest_language_facet')
3843

39-
expect(rendered.css('input').first.attribute('id').text).to eq id_in_label_for
44+
expect(rendered.css('input').first.attribute('id').text).to eq id_in_label_for
45+
end
4046
end
4147

4248
context 'when the facet is explicitly configured to suggest: false' do
4349
let(:facet) { Blacklight::Configuration::FacetField.new key: 'language_facet', suggest: false }
4450

4551
it 'does not display' do
46-
expect(render_inline(described_class.new(facet: facet, presenter: presenter)).to_s).to eq ''
52+
with_request_url "/catalog/facet/language_facet" do
53+
expect(render_inline(component).to_s).to eq ''
54+
end
4755
end
4856
end
4957

5058
context 'when the facet is not explicitly configured with a suggest key' do
5159
let(:facet) { Blacklight::Configuration::FacetField.new key: 'language_facet' }
5260

5361
it 'displays' do
54-
rendered = render_inline(described_class.new(facet: facet, presenter: presenter))
55-
expect(rendered.css("input.facet-suggest").count).to eq 1
62+
with_request_url "/catalog/facet/language_facet" do
63+
rendered = render_inline component
64+
expect(rendered.css("input.facet-suggest").count).to eq 1
65+
end
5666
end
5767
end
5868
end

spec/controllers/catalog_controller_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,6 @@ def export_as_mock
785785
end
786786
end
787787

788-
describe "search_facet_path" do
789-
it "is the same as the catalog path" do
790-
get :index, params: { page: 1 }
791-
expect(controller.send(:search_facet_path, id: "some_facet", page: 5)).to eq facet_catalog_path(id: "some_facet")
792-
end
793-
end
794-
795788
describe "page_links" do
796789
it "has prev/next docs and result set data for non-empty result sets", :integration do
797790
get :page_links, params: { f: { "format" => 'Book' }, counter: 2 }

spec/helpers/blacklight/facets_helper_behavior_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@
2626
expect(presenter).to be_a SomePresenter
2727
end
2828
end
29+
30+
describe "#search_facet_path" do
31+
before do
32+
params[:controller] = 'catalog'
33+
end
34+
35+
it "is the same as the catalog path" do
36+
expect(helper.search_facet_path(id: "some_facet", page: 5)).to eq facet_catalog_path(id: "some_facet")
37+
end
38+
end
2939
end

0 commit comments

Comments
 (0)