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 Sep 26, 2022
1 parent 4199e17 commit 70e966f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 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 @@ -127,8 +125,23 @@ def show_view_config

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
6 changes: 6 additions & 0 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def default_values
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 All @@ -144,6 +146,8 @@ def default_values
document_presenter_class: nil,
document_component: Blacklight::DocumentComponent,
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 @@ -520,6 +524,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 @@ -529,6 +534,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 70e966f

Please sign in to comment.