From d63c4431bfe2b849ecbc612c0c3c325ed2c6b340 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Mon, 26 Sep 2022 12:11:16 -0700 Subject: [PATCH] Refactor DocumentPresenter#fields to use generic logic applicable for different types of field displays --- .../blacklight/document_presenter.rb | 23 +++++++++++++++---- app/presenters/blacklight/index_presenter.rb | 11 --------- app/presenters/blacklight/show_presenter.rb | 9 -------- lib/blacklight/configuration.rb | 22 +++++++++++------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/app/presenters/blacklight/document_presenter.rb b/app/presenters/blacklight/document_presenter.rb index 3df8f5d18b..2914e8b2cb 100644 --- a/app/presenters/blacklight/document_presenter.rb +++ b/app/presenters/blacklight/document_presenter.rb @@ -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 ## @@ -132,8 +130,23 @@ def inspect private + # @return [Hash] + 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 diff --git a/app/presenters/blacklight/index_presenter.rb b/app/presenters/blacklight/index_presenter.rb index 570273e6bb..7f3a280dbf 100644 --- a/app/presenters/blacklight/index_presenter.rb +++ b/app/presenters/blacklight/index_presenter.rb @@ -5,16 +5,5 @@ class IndexPresenter < DocumentPresenter def view_config configuration.view_config(view_context.document_index_view_type) end - - private - - # @return [Hash] 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 diff --git a/app/presenters/blacklight/show_presenter.rb b/app/presenters/blacklight/show_presenter.rb index a57cca6141..815d34ceec 100644 --- a/app/presenters/blacklight/show_presenter.rb +++ b/app/presenters/blacklight/show_presenter.rb @@ -4,15 +4,6 @@ module Blacklight class ShowPresenter < DocumentPresenter private - # @return [Hash] - 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 diff --git a/lib/blacklight/configuration.rb b/lib/blacklight/configuration.rb index be2bda0bfd..7d0757bbed 100644 --- a/lib/blacklight/configuration.rb +++ b/lib/blacklight/configuration.rb @@ -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), @@ -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. @@ -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 @@ -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) @@ -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)