Skip to content

Commit

Permalink
Refactor DocumentPresenter#fields to use generic logic applicable for…
Browse files Browse the repository at this point in the history
… different types of field displays
  • Loading branch information
cbeer committed Jan 11, 2023
1 parent 648fbfc commit d63c443
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
23 changes: 18 additions & 5 deletions app/presenters/blacklight/document_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ def display_type(base_name = nil, default: nil)

fields += ['format'] if fields.empty? # backwards compatibility with the old default value for display_type_field

display_type = fields.lazy.map { |field| field_presenter(field_config(field)) }.detect(&:any?)&.values
display_type ||= Array(default) if default

display_type || []
display_type = fields.lazy.map { |field| field_presenter(display_fields[field] || Configuration::NullDisplayField.new(field)) }.detect(&:any?)&.values
display_type || Array(default)
end

##
Expand Down Expand Up @@ -132,8 +130,23 @@ def inspect

private

# @return [Hash<String,Configuration::Field>]
def fields
@fields ||= Array(display_type).inject(display_fields) do |fields, display_type|
fields.merge(display_fields(configuration.for_display_type(display_type)))
end
end

def display_fields(config = configuration)
config[view_config.document_fields_key || :index_fields]
end

def field_config(field)
fields.fetch(field) { Configuration::NullDisplayField.new(field) }
end

def field_presenter(field_config, options = {})
field_config.presenter.new(view_context, document, field_config, options.merge(field_presenter_options))
field_config.presenter.new(view_context, document, field_config, field_presenter_options.merge(options))
end

def field_presenter_options
Expand Down
11 changes: 0 additions & 11 deletions app/presenters/blacklight/index_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,5 @@ class IndexPresenter < DocumentPresenter
def view_config
configuration.view_config(view_context.document_index_view_type)
end

private

# @return [Hash<String,Configuration::Field>] all the fields for this index view
def fields
configuration.index_fields_for(display_type)
end

def field_config(field)
configuration.index_fields.fetch(field) { Configuration::NullDisplayField.new(field) }
end
end
end
9 changes: 0 additions & 9 deletions app/presenters/blacklight/show_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ module Blacklight
class ShowPresenter < DocumentPresenter
private

# @return [Hash<String,Configuration::Field>]
def fields
configuration.show_fields_for(display_type)
end

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

def field_presenter_options
{ context: 'show' }
end
Expand Down
22 changes: 14 additions & 8 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def initialized_default_configuration?
title_field: nil,
# solr field to use to render format-specific partials
display_type_field: nil,
# the "field access" key to use to look up the document display fields
document_fields_key: :index_fields,
# partials to render for each document(see #render_document_partials)
partials: [],
document_actions: NestedOpenStructWithHashAccess.new(ToolConfig),
Expand Down Expand Up @@ -173,6 +175,8 @@ def initialized_default_configuration?
document_component: Blacklight::DocumentComponent,
sidebar_component: Blacklight::Document::SidebarComponent,
display_type_field: nil,
# the "field access" key to use to look up the document display fields
document_fields_key: :show_fields,
# Default route parameters for 'show' requests.
# Set this to a hash with additional arguments to merge into the route,
# or set `controller: :current` to route to the current controller.
Expand Down Expand Up @@ -296,17 +300,17 @@ def initialized_default_configuration?
# @since v8.0.0
# @return [Boolean]
property :filter_search_state_fields, default: true

# Additional Blacklight configuration setting for document-type specific
# configuration.
# @!attribute fields_for_type
# @since v8.0.0
# @return [Hash{Symbol => Blacklight::Configuration}]
# @see [#for_display_type]
property :fields_for_type, default: {}.with_indifferent_access
end
# rubocop:enable Metrics/BlockLength

# Additional Blacklight configuration setting for document-type specific
# configuration.
# @!attribute fields_for_type
# @since v8.0.0
# @return [Hash{Symbol => Blacklight::Configuration}]
# @see [#for_display_type]
property :fields_for_type, default: {}.with_indifferent_access

##
# Create collections of solr field configurations.
# This will create array-like accessor methods for
Expand Down Expand Up @@ -556,6 +560,7 @@ def for_display_type display_type, &_block
##
# Return a list of fields for the index display that should be used for the
# provided document. This respects any configuration made using for_display_type
# @deprecated
def index_fields_for(display_types)
Array(display_types).inject(index_fields) do |fields, display_type|
fields.merge(for_display_type(display_type).index_fields)
Expand All @@ -565,6 +570,7 @@ def index_fields_for(display_types)
##
# Return a list of fields for the show page that should be used for the
# provided document. This respects any configuration made using for_display_type
# @deprecated
def show_fields_for(display_types)
Array(display_types).inject(show_fields) do |fields, display_type|
fields.merge(for_display_type(display_type).show_fields)
Expand Down

0 comments on commit d63c443

Please sign in to comment.