Skip to content

Commit 963a39c

Browse files
committed
Refactor action_mapping to use its own configuration object that points at existing view configs instead of trying to mush it together with the actual view config.
1 parent 09170e0 commit 963a39c

File tree

3 files changed

+54
-37
lines changed

3 files changed

+54
-37
lines changed

lib/blacklight/configuration.rb

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ def initialized_default_configuration?
211211

212212
# @!attribute action_mapping
213213
# @since v7.16.0
214-
# @return [Hash{Symbol => Blacklight::Configuration::ViewConfig}]
214+
# @return [Hash{Symbol => Blacklight::Configuration::ActionConfigMapEntry}]
215215
property :action_mapping, default: NestedOpenStructWithHashAccess.new(
216-
ViewConfig,
217-
default: { top_level_config: :index },
218-
show: { top_level_config: :show },
219-
citation: { parent_config: :show },
220-
email_record: { top_level_config: :email },
221-
sms_record: { top_level_config: :sms }
216+
ActionConfigMapEntry,
217+
default: { blacklight_config_property: :index, default: [:index] },
218+
show: { blacklight_config_property: :show, default: [:index] },
219+
citation: { parent_action_key: :show },
220+
email_record: { blacklight_config_property: :email },
221+
sms_record: { blacklight_config_property: :sms }
222222
)
223223

224224
# @!attribute sms
@@ -528,12 +528,7 @@ def view_config(view_type = nil, action_name: :index)
528528
view_type = nil
529529
end
530530

531-
@view_config[[view_type, action_name]] ||= if view_type.nil?
532-
action_config(action_name)
533-
else
534-
base_config = action_config(action_name)
535-
base_config.merge(view.fetch(view_type, {}))
536-
end
531+
@view_config[[view_type, action_name]] ||= action_config(action_name, (view.fetch(view_type, nil) if view_type))
537532
end
538533

539534
# 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)
646641
end
647642
end
648643

649-
def action_config(action, default: :index)
650-
action_config = action_mapping[action]
644+
def action_config(action_name, view_type_specific_config, default: :index)
645+
action_config = action_mapping[action_name]
651646
action_config ||= action_mapping[:default]
652647

653648
if action_config.parent_config && action_config.parent_config != :default
@@ -658,9 +653,19 @@ def action_config(action, default: :index)
658653
end
659654
action_config = action_config.reverse_merge(action_mapping[:default]) if action_config != action_mapping[:default]
660655

661-
action_config = action_config.reverse_merge(self[action_config.top_level_config]) if action_config.top_level_config
662-
action_config = action_config.reverse_merge(show) if default == :show && action_config.top_level_config != :show
663-
action_config.reverse_merge(index)
656+
view_config = if action_config.blacklight_config_property
657+
self[action_config.blacklight_config_property]
658+
else
659+
self[default]
660+
end
661+
662+
view_config = Array(action_config.default - [action_config.blacklight_config_property || default]).inject(view_config) do |config, top_level_config|
663+
config.reverse_merge(self[top_level_config])
664+
end
665+
666+
view_config = view_config.merge(view_type_specific_config) if view_type_specific_config
667+
668+
view_config
664669
end
665670
end
666671
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
module Blacklight
4+
class Configuration::ActionConfigMapEntry < OpenStructWithHashAccess
5+
# @!attribute parent_action_key
6+
# Pull in the configuration for this action from another action's config
7+
# @return [Symbol]
8+
9+
def parent_config = parent_action_key
10+
11+
def parent_config=(value)
12+
self.parent_action_key = value
13+
end
14+
15+
#
16+
# @!attribute blacklight_config_property
17+
# Pull in the configuration for this action from a top-level config
18+
# @return [Symbol]
19+
20+
def top_level_config = blacklight_config_property
21+
22+
def top_level_config=(value)
23+
self.blacklight_config_property = value
24+
end
25+
26+
#
27+
# @!attribute default
28+
# Pull in additional default configuration for this action from a top-level config
29+
# @return [Array<Symbol>]
30+
end
31+
end

spec/models/blacklight/configuration_spec.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -670,25 +670,6 @@
670670
expect(config.view_config(action_name: :show)).to have_attributes config.show.to_h
671671
end
672672

673-
it 'includes the default action mapping configuration' do
674-
config.action_mapping.default.whatever = :some_value
675-
676-
expect(config.view_config(action_name: :show)).to have_attributes whatever: :some_value
677-
end
678-
679-
it 'includes the action-specific mappings' do
680-
config.action_mapping.foo.document_presenter_class = Blacklight::DocumentPresenter
681-
682-
expect(config.view_config(action_name: :foo)).to have_attributes config.action_mapping.foo.to_h
683-
end
684-
685-
it 'allows the action mapping to specific a parent configuration with some more defaults' do
686-
config.action_mapping.foo.parent_config = :bar
687-
config.action_mapping.bar.whatever = :bar_value
688-
689-
expect(config.view_config(action_name: :foo)).to have_attributes whatever: :bar_value
690-
end
691-
692673
context 'with the :citation action' do
693674
it 'also includes the show config' do
694675
expect(config.view_config(action_name: :citation)).to have_attributes config.show.to_h

0 commit comments

Comments
 (0)