Skip to content

Commit 22af61d

Browse files
committed
Remove unnecessary partial and move render conditions to component
1 parent 0de2db4 commit 22af61d

File tree

9 files changed

+78
-71
lines changed

9 files changed

+78
-71
lines changed

app/components/blacklight/facet_field_filter_component.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

app/components/blacklight/facet_field_filter_component.html.erb renamed to app/components/blacklight/facets/filter_component.html.erb

File renamed without changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module Blacklight
4+
module Facets
5+
class FilterComponent < Blacklight::Component
6+
# @params [Blacklight::FacetFieldPresenter] facet_field
7+
def initialize(facet_field:)
8+
@facet_field = facet_field
9+
end
10+
11+
def render?
12+
@facet_field.facet_field.index_range && @facet_field.display_facet.index?
13+
end
14+
15+
def prefix
16+
@facet_field.paginator.prefix
17+
end
18+
19+
def clear_facet_prefix_url
20+
@facet_field.paginator.params_for_resort_url('index', @facet_field.search_state.to_h.except(@facet_field.paginator.request_keys[:prefix]))
21+
end
22+
23+
def facet_prefix_url(letter)
24+
@facet_field.paginator.params_for_resort_url('index', @facet_field.search_state.to_h.merge(@facet_field.paginator.request_keys[:prefix] => letter))
25+
end
26+
end
27+
end
28+
end

app/controllers/concerns/blacklight/catalog.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def track
8080

8181
# displays values and pagination links for a single facet field
8282
def facet
83+
# @facet is a Blacklight::Configuration::FacetField
8384
@facet = blacklight_config.facet_fields[params[:id]]
8485
raise ActionController::RoutingError, 'Not Found' unless @facet
8586

@@ -88,8 +89,10 @@ def facet
8889
else
8990
search_service.facet_field_response(@facet.key)
9091
end
92+
# @display_facet is a Blacklight::Solr::Response::Facets::FacetField
9193
@display_facet = @response.aggregations[@facet.field]
9294

95+
# @presenter is a Blacklight::FacetFieldPresenter
9396
@presenter = @facet.presenter.new(@facet, @display_facet, view_context)
9497
@pagination = @presenter.paginator
9598
respond_to do |format|

app/views/catalog/_facet_index_navigation.html.erb

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

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

99
<div class="facet-pagination top d-flex flex-wrap w-100 justify-content-between border-bottom pb-3 mb-3">
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe Blacklight::Facets::FilterComponent, type: :component do
4+
let(:pagination) { Blacklight::Solr::FacetPaginator.new([]) }
5+
let(:facet) { Blacklight::Configuration::FacetField.new(index_range: '0'..'9', presenter: Blacklight::FacetFieldPresenter) }
6+
let(:display_facet) { instance_double(Blacklight::Solr::Response::Facets::FacetField, items: [], offset: 0, prefix: '', sort: 'index', index?: true) }
7+
let(:blacklight_config) { Blacklight::Configuration.new }
8+
9+
let(:presenter) { facet.presenter.new(facet, display_facet, vc_test_controller.view_context) }
10+
11+
before do
12+
with_request_url "/catalog/facet/language" do
13+
render_inline(described_class.new(facet_field: presenter))
14+
end
15+
end
16+
17+
it 'renders the facet index navigation range' do
18+
expect(page).to have_css '.pagination'
19+
expect(page).to have_link '0', href: '/catalog/facet/language.html?facet.prefix=0&facet.sort=index'
20+
expect(page).to have_link '1'
21+
expect(page).to have_link '8'
22+
expect(page).to have_link '9'
23+
end
24+
25+
it 'renders an "all" button' do
26+
expect(page).to have_css '.page-link', text: 'All'
27+
end
28+
29+
context 'with a selected index' do
30+
let(:display_facet) { instance_double(Blacklight::Solr::Response::Facets::FacetField, items: [], offset: 0, prefix: '5', sort: 'index', index?: true) }
31+
32+
it 'highlights the selected index' do
33+
expect(page).to have_css '.active', text: '5'
34+
end
35+
36+
it 'enables the clear facets button' do
37+
expect(page).to have_link 'All'
38+
end
39+
end
40+
end

spec/views/catalog/_facet_index_navigation.html.erb_spec.rb

Lines changed: 0 additions & 43 deletions
This file was deleted.

spec/views/catalog/facet.html.erb_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
let(:blacklight_config) { Blacklight::Configuration.new }
66
let(:component) { instance_double(Blacklight::FacetComponent) }
77
let(:facet_suggest_input) { instance_double(Blacklight::Search::FacetSuggestInput) }
8+
let(:facet_filter) { instance_double(Blacklight::Facets::FilterComponent) }
89

910
before do
1011
allow(Blacklight::FacetComponent).to receive(:new).and_return(component)
1112
allow(Blacklight::Search::FacetSuggestInput).to receive(:new).and_return(facet_suggest_input)
13+
allow(Blacklight::Facets::FilterComponent).to receive(:new).and_return(facet_filter)
14+
1215
allow(view).to receive(:render).and_call_original
1316
allow(view).to receive(:render).with(component)
1417
allow(view).to receive(:render).with(facet_suggest_input)
18+
allow(view).to receive(:render).with(facet_filter)
1519

1620
blacklight_config.add_facet_field 'xyz', label: "Facet title"
1721
allow(view).to receive(:blacklight_config).and_return(blacklight_config)
@@ -25,9 +29,10 @@
2529
expect(rendered).to have_css 'h1', text: "Facet title"
2630
end
2731

28-
it "renders the facet suggest input" do
32+
it "renders the facet suggest input and filters" do
2933
render
3034
expect(view).to have_received(:render).with(facet_suggest_input)
35+
expect(view).to have_received(:render).with(facet_filter)
3136
end
3237

3338
it "renders facet pagination" do

0 commit comments

Comments
 (0)