Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set field configuration default values in the normalize step of the configuration #2550

Merged
merged 7 commits into from
Nov 24, 2021
4 changes: 2 additions & 2 deletions app/components/blacklight/advanced_search_form_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class AdvancedSearchFormComponent < SearchBarComponent
renders_many :constraints
renders_many :search_field_controls
renders_many :search_filter_controls, (lambda do |config:, display_facet:, presenter: nil, component: nil, **kwargs|
presenter ||= (config.presenter || Blacklight::FacetFieldPresenter).new(config, display_facet, helpers)
component = component || config.advanced_search_component || Blacklight::FacetFieldCheckboxesComponent
presenter ||= config.presenter.new(config, display_facet, helpers)
component ||= config.advanced_search_component

component.new(facet_field: presenter, **kwargs)
end)
Expand Down
2 changes: 1 addition & 1 deletion app/components/blacklight/constraints_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def clause_presenters
end

def facet_item_presenter(facet_config, facet_item, facet_field)
Blacklight::FacetItemPresenter.new(facet_item, facet_config, helpers, facet_field)
facet_config.item_presenter.new(facet_item, facet_config, helpers, facet_field)
end

def inclusive_facet_item_presenter(facet_config, facet_item, facet_field)
Expand Down
8 changes: 2 additions & 6 deletions app/components/blacklight/document_metadata_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Blacklight
class DocumentMetadataComponent < ::ViewComponent::Base
renders_many :fields, (lambda do |component: nil, **kwargs|
(component || Blacklight::MetadataFieldComponent).new(**kwargs)
component.new(**kwargs)
end)
with_collection_parameter :fields

Expand All @@ -18,7 +18,7 @@ def before_render
return unless fields

@fields.each do |field|
field(component: field_component(field), field: field, show: @show, view_type: @view_type)
field(component: field.component, field: field, show: @show, view_type: @view_type)
end
end

Expand All @@ -27,9 +27,5 @@ def render?
end

attr_reader :view_type

def field_component(field)
field&.component || Blacklight::MetadataFieldComponent
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def presenters
return to_enum(:presenters) unless block_given?

@facet_field.paginator.items.each do |item|
yield Blacklight::FacetItemPresenter.new(item, @facet_field.facet_field, helpers, @facet_field.key, @facet_field.search_state)
yield @facet_field.facet_field.item_presenter.new(item, @facet_field.facet_field, helpers, @facet_field.key, @facet_field.search_state)
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions app/components/blacklight/facet_field_list_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ def render?
end

def facet_item_presenter(facet_item, deprecated_facet_config = nil, facet_field = nil)
Blacklight::FacetItemPresenter.new(facet_item, deprecated_facet_config || facet_config, helpers, facet_field || @facet_field.key)
(deprecated_facet_config || facet_config).item_presenter.new(facet_item, deprecated_facet_config || facet_config, helpers, facet_field || @facet_field.key)
end

def facet_item_component_class(deprecated_facet_config = nil)
default_component = (deprecated_facet_config || facet_config).pivot ? Blacklight::FacetItemPivotComponent : Blacklight::FacetItemComponent
(deprecated_facet_config || facet_config).fetch(:item_component, default_component)
(deprecated_facet_config || facet_config).item_component
end

def facet_config
Expand Down
2 changes: 1 addition & 1 deletion app/components/blacklight/facet_item_pivot_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def render_component(component)
end

def facet_item_presenter(facet_item)
Blacklight::FacetItemPresenter.new(facet_item, @facet_item.facet_config, helpers, @facet_item.facet_field, @facet_item.search_state)
@facet_item.facet_config.item_presenter.new(facet_item, @facet_item.facet_config, helpers, @facet_item.facet_field, @facet_item.search_state)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def facet
@response = search_service.facet_field_response(@facet.key)
@display_facet = @response.aggregations[@facet.field]

@presenter = (@facet.presenter || Blacklight::FacetFieldPresenter).new(@facet, @display_facet, view_context)
@presenter = @facet.presenter.new(@facet, @display_facet, view_context)
@pagination = @presenter.paginator
respond_to do |format|
format.html do
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/blacklight/facets_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Blacklight::FacetsHelperBehavior
delegate :facet_configuration_for_field, to: :blacklight_config

def facet_field_presenter(facet_config, display_facet)
(facet_config.presenter || Blacklight::FacetFieldPresenter).new(facet_config, display_facet, self)
facet_config.presenter.new(facet_config, display_facet, self)
end

private
Expand All @@ -18,6 +18,6 @@ def facet_value_for_facet_item item
end

def facet_item_presenter(facet_config, facet_item, facet_field)
Blacklight::FacetItemPresenter.new(facet_item, facet_config, self, facet_field)
facet_config.item_presenter.new(facet_item, facet_config, self, facet_field)
end
end
3 changes: 1 addition & 2 deletions app/presenters/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def show_view_config
private

def field_presenter(field_config, options = {})
presenter_class = field_config.presenter || Blacklight::FieldPresenter
presenter_class.new(view_context, document, field_config, options.merge(field_presenter_options))
field_config.presenter.new(view_context, document, field_config, options.merge(field_presenter_options))
end

def field_presenter_options
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/blacklight/index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def fields
end

def field_config(field)
configuration.index_fields.fetch(field) { Configuration::NullField.new(field) }
configuration.index_fields.fetch(field) { Configuration::NullDisplayField.new(field) }
end
end
end
2 changes: 1 addition & 1 deletion app/presenters/blacklight/show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def fields
end

def field_config(field)
configuration.show_fields.fetch(field) { Configuration::NullField.new(field) }
configuration.show_fields.fetch(field) { Configuration::NullDisplayField.new(field) }
end

def field_presenter_options
Expand Down
4 changes: 4 additions & 0 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ class Configuration < OpenStructWithHashAccess
autoload :Fields
autoload :Field
autoload :NullField
autoload :NullDisplayField
autoload :SearchField
autoload :FacetField
autoload :SortField
autoload :DisplayField
autoload :IndexField
autoload :ShowField
end

include Fields
Expand Down
12 changes: 6 additions & 6 deletions lib/blacklight/configuration/display_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

module Blacklight
class Configuration::DisplayField < Blacklight::Configuration::Field
def initialize(*args, **kwargs, &block)
super

self.presenter ||= Blacklight::FieldPresenter
self.component ||= Blacklight::MetadataFieldComponent
end
##
# The following is a non-exhaustive list of display field config parameters that are used
# by Blacklight directly. Application-specific code or plugins may add or replace
Expand Down Expand Up @@ -38,11 +44,5 @@ class Configuration::DisplayField < Blacklight::Configuration::Field
# @return [String]
# @!attribute separator_options
# @return [Hash]

# @param [Blacklight::Configuration] _blacklight_config
def normalize! _blacklight_config = nil
super
self.presenter ||= Blacklight::FieldPresenter
end
end
end
7 changes: 7 additions & 0 deletions lib/blacklight/configuration/facet_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ class Configuration::FacetField < Blacklight::Configuration::Field
# Rendering:
# @!attribute presenter
# @return [Blacklight::FacetFieldPresenter]
# @!attribute item_presenter
# @return [Blacklight::FacetItemPresenter]
# @!attribute component
# @return [Blacklight::FacetFieldListComponent]
# @!attribute item_component
# @return [Blacklight::FacetItemComponent]
# @!attribute partial
# @return [String] Rails view partial used to render the facet field

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def normalize! blacklight_config = nil
query.stringify_keys! if query

Expand All @@ -71,7 +74,10 @@ def normalize! blacklight_config = nil
self.if = show if self.if.nil?
self.index_range = 'A'..'Z' if index_range == true
self.presenter ||= Blacklight::FacetFieldPresenter
self.item_presenter ||= Blacklight::FacetItemPresenter
self.component = Blacklight::FacetFieldListComponent if component.nil? || component == true
self.item_component ||= pivot ? Blacklight::FacetItemPivotComponent : Blacklight::FacetItemComponent
self.advanced_search_component ||= Blacklight::FacetFieldCheckboxesComponent

super

Expand All @@ -82,5 +88,6 @@ def normalize! blacklight_config = nil

self
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
end
end
17 changes: 17 additions & 0 deletions lib/blacklight/configuration/null_display_field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Blacklight
# Returned if no config is defined for a display field in the Blacklight::Configuration
class Configuration::NullDisplayField < Blacklight::Configuration::DisplayField
def initialize(field_or_hash = nil)
case field_or_hash
when String, Symbol
super(field: field_or_hash)
else
super
end

normalize!
end
end
end
9 changes: 9 additions & 0 deletions lib/blacklight/configuration/view_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def display_label(**options)
)
end

# Translate an ordinary field into the expected DisplayField object
def title_field=(value)
if value.is_a?(Blacklight::Configuration::Field) && !value.is_a?(Blacklight::Configuration::DisplayField)
super(Blacklight::Configuration::DisplayField.new(value.to_h))
else
super
end
end

class Show < ViewConfig
# @!attribute route
# @return [Hash] Default route parameters for 'show' requests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let(:facet_field) do
instance_double(
Blacklight::FacetFieldPresenter,
facet_field: Blacklight::Configuration::NullField.new(key: 'field'),
facet_field: Blacklight::Configuration::NullField.new(key: 'field', item_component: Blacklight::FacetItemComponent, item_presenter: Blacklight::FacetItemPresenter),
paginator: paginator,
key: 'field',
label: 'Field',
Expand Down
12 changes: 7 additions & 5 deletions spec/components/blacklight/facet_field_list_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
instance_double(
Blacklight::FacetFieldPresenter,
paginator: paginator,
facet_field: Blacklight::Configuration::NullField.new(key: 'field'),
facet_field: facet_config,
key: 'field',
label: 'Field',
active?: false,
Expand All @@ -21,6 +21,8 @@
)
end

let(:facet_config) { Blacklight::Configuration::NullField.new(key: 'field', item_component: Blacklight::FacetItemComponent, item_presenter: Blacklight::FacetItemPresenter) }

let(:paginator) do
instance_double(Blacklight::FacetPaginator, items: [
double(label: 'x', hits: 10),
Expand All @@ -45,7 +47,7 @@
instance_double(
Blacklight::FacetFieldPresenter,
paginator: paginator,
facet_field: Blacklight::Configuration::NullField.new(key: 'field'),
facet_field: facet_config,
key: 'field',
label: 'Field',
active?: true,
Expand All @@ -65,7 +67,7 @@
instance_double(
Blacklight::FacetFieldPresenter,
paginator: paginator,
facet_field: Blacklight::Configuration::NullField.new(key: 'field'),
facet_field: facet_config,
key: 'field',
label: 'Field',
active?: false,
Expand All @@ -91,7 +93,7 @@
instance_double(
Blacklight::FacetFieldPresenter,
paginator: paginator,
facet_field: Blacklight::Configuration::NullField.new(key: 'field'),
facet_field: facet_config,
key: 'field',
label: 'Field',
active?: false,
Expand All @@ -111,7 +113,7 @@
instance_double(
Blacklight::FacetFieldPresenter,
paginator: paginator,
facet_field: Blacklight::Configuration::NullField.new(key: 'field'),
facet_field: facet_config,
key: 'field',
label: 'Field',
active?: false,
Expand Down
6 changes: 4 additions & 2 deletions spec/components/blacklight/facet_item_pivot_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
let(:facet_item) do
instance_double(
Blacklight::FacetItemPresenter,
facet_config: Blacklight::Configuration::FacetField.new(key: 'z'),
facet_config: facet_config,
facet_field: 'z',
label: 'x',
hits: 10,
Expand All @@ -25,6 +25,8 @@
)
end

let(:facet_config) { Blacklight::Configuration::NullField.new(key: 'z', item_component: Blacklight::FacetItemComponent, item_presenter: Blacklight::FacetItemPresenter) }

it 'links to the facet and shows the number of hits' do
expect(rendered).to have_selector 'li'
expect(rendered).to have_link 'x', href: '/catalog?f%5Bz%5D=x'
Expand All @@ -40,7 +42,7 @@
let(:facet_item) do
instance_double(
Blacklight::FacetItemPresenter,
facet_config: Blacklight::Configuration::FacetField.new,
facet_config: facet_config,
facet_field: 'z',
label: 'x',
hits: 10,
Expand Down
10 changes: 5 additions & 5 deletions spec/helpers/catalog_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,32 +194,32 @@ def mock_response args

it "pulls data out of a document's field" do
blacklight_config.index.display_type_field = :type
doc = { type: 'book' }
doc = { type: 'book' }.with_indifferent_access
expect(helper.render_document_class(doc)).to eq "blacklight-book"
end

it "supports multivalued fields" do
blacklight_config.index.display_type_field = :type
doc = { type: %w[book mss] }
doc = { type: %w[book mss] }.with_indifferent_access
expect(helper.render_document_class(doc)).to eq "blacklight-book blacklight-mss"
end

it "supports empty fields" do
blacklight_config.index.display_type_field = :type
doc = { type: [] }
doc = { type: [] }.with_indifferent_access
expect(helper.render_document_class(doc)).to be_blank
end

it "supports missing fields" do
blacklight_config.index.display_type_field = :type
doc = {}
doc = {}.with_indifferent_access
expect(helper.render_document_class(doc)).to be_blank
end

it "supports view-specific field configuration" do
allow(helper).to receive(:document_index_view_type).and_return(:some_view_type)
blacklight_config.view.some_view_type(display_type_field: :other_type)
doc = { other_type: "document" }
doc = { other_type: "document" }.with_indifferent_access
expect(helper.render_document_class(doc)).to eq "blacklight-document"
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/presenters/blacklight/document_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
describe '#fields_to_render' do
subject { presenter.fields_to_render.to_a }

let(:field_config) { Blacklight::Configuration::Field.new(field: 'asdf') }
let(:field_config) { Blacklight::Configuration::DisplayField.new(field: 'asdf') }

context 'when all of the fields have values' do
before do
Expand All @@ -30,7 +30,7 @@

describe '#field_value' do
let(:field_presenter) { instance_double(Blacklight::FieldPresenter, render: 'xyz') }
let(:field_config) { Blacklight::Configuration::Field.new }
let(:field_config) { Blacklight::Configuration::DisplayField.new }
let(:options) { { a: 1 } }

it 'calls the field presenter' do
Expand Down
Loading