diff --git a/lib/blacklight/configuration.rb b/lib/blacklight/configuration.rb index 6fb8a1f34..da56f2744 100644 --- a/lib/blacklight/configuration.rb +++ b/lib/blacklight/configuration.rb @@ -211,14 +211,14 @@ def initialized_default_configuration? # @!attribute action_mapping # @since v7.16.0 - # @return [Hash{Symbol => Blacklight::Configuration::ViewConfig}] + # @return [Hash{Symbol => Blacklight::Configuration::ActionConfigMapEntry}] property :action_mapping, default: NestedOpenStructWithHashAccess.new( - ViewConfig, - default: { top_level_config: :index }, - show: { top_level_config: :show }, - citation: { parent_config: :show }, - email_record: { top_level_config: :email }, - sms_record: { top_level_config: :sms } + ActionConfigMapEntry, + default: { blacklight_config_property: :index, default: [:index] }, + show: { blacklight_config_property: :show }, + citation: { parent_action_key: :show }, + email_record: { blacklight_config_property: :email }, + sms_record: { blacklight_config_property: :sms } ) # @!attribute sms @@ -528,12 +528,7 @@ def view_config(view_type = nil, action_name: :index) view_type = nil end - @view_config[[view_type, action_name]] ||= if view_type.nil? - action_config(action_name) - else - base_config = action_config(action_name) - base_config.merge(view.fetch(view_type, {})) - end + @view_config[[view_type, action_name]] ||= action_config(action_name, (view.fetch(view_type, nil) if view_type)) end # YARD will include inline disabling as docs, cannot do multiline inside @!macro. AND this must be separate from doc block. @@ -646,8 +641,8 @@ def _deep_copy(value) end end - def action_config(action, default: :index) - action_config = action_mapping[action] + def action_config(action_name, view_type_specific_config, default: :index) + action_config = action_mapping[action_name] action_config ||= action_mapping[:default] if action_config.parent_config && action_config.parent_config != :default @@ -658,9 +653,19 @@ def action_config(action, default: :index) end action_config = action_config.reverse_merge(action_mapping[:default]) if action_config != action_mapping[:default] - action_config = action_config.reverse_merge(self[action_config.top_level_config]) if action_config.top_level_config - action_config = action_config.reverse_merge(show) if default == :show && action_config.top_level_config != :show - action_config.reverse_merge(index) + view_config = if action_config.blacklight_config_property + self[action_config.blacklight_config_property] + else + self[default] + end + + view_config = Array(action_config.default - [action_config.blacklight_config_property || default]).inject(view_config) do |config, top_level_config| + config.reverse_merge(self[top_level_config]) + end + + view_config = view_config.merge(view_type_specific_config) if view_type_specific_config + + view_config end end end diff --git a/lib/blacklight/configuration/action_config_map_entry.rb b/lib/blacklight/configuration/action_config_map_entry.rb new file mode 100644 index 000000000..94955583c --- /dev/null +++ b/lib/blacklight/configuration/action_config_map_entry.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Blacklight + class Configuration::ActionConfigMapEntry < OpenStructWithHashAccess + # @!attribute parent_action_key + # Pull in the configuration for this action from another action's config + # @return [Symbol] + + def parent_config = parent_action_key + + def parent_config=(value) + self.parent_action_key = value + end + + # + # @!attribute blacklight_config_property + # Pull in the configuration for this action from a top-level config + # @return [Symbol] + + def top_level_config = blacklight_config_property + + def top_level_config=(value) + self.blacklight_config_property = value + end + + # + # @!attribute default + # Pull in additional default configuration for this action from a top-level config + # @return [Array] + end +end diff --git a/spec/models/blacklight/configuration_spec.rb b/spec/models/blacklight/configuration_spec.rb index c5f5c4104..e0dae4b44 100644 --- a/spec/models/blacklight/configuration_spec.rb +++ b/spec/models/blacklight/configuration_spec.rb @@ -670,25 +670,6 @@ expect(config.view_config(action_name: :show)).to have_attributes config.show.to_h end - it 'includes the default action mapping configuration' do - config.action_mapping.default.whatever = :some_value - - expect(config.view_config(action_name: :show)).to have_attributes whatever: :some_value - end - - it 'includes the action-specific mappings' do - config.action_mapping.foo.document_presenter_class = Blacklight::DocumentPresenter - - expect(config.view_config(action_name: :foo)).to have_attributes config.action_mapping.foo.to_h - end - - it 'allows the action mapping to specific a parent configuration with some more defaults' do - config.action_mapping.foo.parent_config = :bar - config.action_mapping.bar.whatever = :bar_value - - expect(config.view_config(action_name: :foo)).to have_attributes whatever: :bar_value - end - context 'with the :citation action' do it 'also includes the show config' do expect(config.view_config(action_name: :citation)).to have_attributes config.show.to_h