Class: Blacklight::FacetComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/blacklight/facet_component.rb

Overview

Renders a single section for facet limit with a specified solr field used for faceting. This renders no UI of it’s own, but renders the component that is configured for the facet.

Instance Method Summary collapse

Constructor Details

#initialize(display_facet_or_field_config: nil, display_facet: nil, field_config: nil, response: nil, blacklight_config: nil, component: nil, **component_args) ⇒ FacetComponent

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ParameterLists

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/components/blacklight/facet_component.rb', line 15

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
    @display_facet = @facet_field_presenter.display_facet
  elsif display_facet_or_field_config.is_a?(Blacklight::Configuration::Field) || field_config
    @facet_field_presenter = nil # we need the view context to generate this
    @field_config = display_facet_or_field_config || field_config
    @display_facet = display_facet ||
                     response&.aggregations&.fetch(@field_config.field) { Blacklight::Solr::Response::Facets::NullFacetField.new(@field_config.field, response: response) }
  elsif (display_facet || display_facet_or_field_config).respond_to?(:name)
    @facet_field_presenter = nil # we need the view context to generate this
    @display_facet = display_facet || display_facet_or_field_config
    @field_config = field_config || blacklight_config&.facet_configuration_for_field(@display_facet.name)
  else
    raise ArgumentError, 'You must provide one of display_facet or field_config' unless @field_config
  end

  @component = component || @field_config.component
  @component_args = component_args
end

Instance Method Details

#callObject



42
43
44
45
46
47
48
49
50
51
# File 'app/components/blacklight/facet_component.rb', line 42

def call
  return render_partial if @field_config.partial

  render(
    @component.new(
      facet_field: @facet_field_presenter || helpers.facet_field_presenter(@field_config, @display_facet),
      **@component_args
    )
  )
end

#render?Boolean

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/ParameterLists

Returns:

  • (Boolean)


38
39
40
# File 'app/components/blacklight/facet_component.rb', line 38

def render?
  helpers.should_render_field?(@field_config, @display_facet)
end

#render_partialObject



53
54
55
# File 'app/components/blacklight/facet_component.rb', line 53

def render_partial
  render(@field_config.partial, locals: { field_name: @field_config.field, facet_field: @field_config, display_facet: @display_facet }.merge(@component_args))
end