Skip to content

Commit

Permalink
Add a component parameter to the FacetComponent, allowing callers to …
Browse files Browse the repository at this point in the history
…override the configured value
  • Loading branch information
cbeer committed Jul 26, 2022
1 parent 70f89c0 commit b4845cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
11 changes: 5 additions & 6 deletions app/components/blacklight/facet_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class FacetComponent < ViewComponent::Base
# @param [Blacklight::Solr::Response::Facets::FacetField] display_facet
# @param [Blacklight::Configuration] blacklight_config
# @param [Boolean] layout
# rubocop:disable Metrics/CyclomaticComplexity
def initialize(display_facet_or_field_config: nil, display_facet: nil, field_config: nil, response: nil, blacklight_config: nil, **component_args)
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ParameterLists
def initialize(display_facet_or_field_config: nil, display_facet: nil, field_config: nil, response: nil, blacklight_config: nil, component: nil, **component_args)
if display_facet_or_field_config.is_a? Blacklight::FacetFieldPresenter
@facet_field_presenter = display_facet_or_field_config
@field_config = @facet_field_presenter.facet_field
Expand All @@ -30,9 +30,10 @@ def initialize(display_facet_or_field_config: nil, display_facet: nil, field_con
raise ArgumentError, 'You must provide one of display_facet or field_config' unless @field_config
end

@component = component || (@field_config.component == true ? Blacklight::FacetFieldListComponent : @field_config.component)
@component_args = component_args
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ParameterLists

def render?
helpers.should_render_field?(@field_config, @display_facet)
Expand All @@ -41,10 +42,8 @@ def render?
def call
return render_partial if @field_config.partial

component = @field_config.component == true ? Blacklight::FacetFieldListComponent : @field_config.component

render(
component.new(
@component.new(
facet_field: @facet_field_presenter || helpers.facet_field_presenter(@field_config, @display_facet),
**@component_args
)
Expand Down
19 changes: 19 additions & 0 deletions spec/components/blacklight/facet_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@
expect(rendered).to have_selector 'ul.facet-values'
end

context 'with a provided component' do
let(:component_kwargs) { { field_config: facet_config, display_facet: display_facet, component: component_class } }
let(:component_class) do
Class.new(Blacklight::FacetFieldListComponent) do
def self.name
'CustomFacetComponent'
end

def call
'Custom facet rendering'
end
end
end

it 'renders the provided component' do
expect(rendered).to have_content 'Custom facet rendering'
end
end

context 'with a facet configured to use a partial' do
let(:facet_config) do
Blacklight::Configuration::FacetField.new(key: 'field', partial: 'catalog/facet_partial').normalize!
Expand Down

0 comments on commit b4845cb

Please sign in to comment.