Skip to content

Commit 1a0b276

Browse files
committed
fixup: make it possible to have a blank config
1 parent 33b5684 commit 1a0b276

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

lib/view_component/base.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ class << self
2222
# Returns the current config.
2323
#
2424
# @return [ActiveSupport::OrderedOptions]
25-
def config
26-
ViewComponent::Config.current
27-
end
25+
26+
# @!attribute current
27+
# @return [ViewComponent::Config]
28+
# Returns the current ViewComponent::Config. This is persisted against this
29+
# class so that config options remain accessible before the rest of
30+
# ViewComponent has loaded. Defaults to an instance of ViewComponent::Config
31+
# with all other documented defaults set.
32+
class_attribute :config, default: ViewComponent::Config.default, instance_predicate: false
2833
end
2934

3035
include ViewComponent::InlineTemplate

lib/view_component/config.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
module ViewComponent
66
class Config
7+
class Layer < ActiveSupport::OrderedOptions; end
78
class << self
89
# `new` without any arguments initializes the default configuration, but
910
# it's important to differentiate in case that's no longer the case in
1011
# future.
11-
alias_method :default, :new
12-
13-
def defaults
14-
ActiveSupport::OrderedOptions.new.merge!({
15-
generate: default_generate_options,
12+
def default
13+
Layer[
14+
generate: Layer[preview_path: ""],
1615
preview_controller: "ViewComponentsController",
1716
preview_route: "/rails/view_components",
1817
show_previews_source: false,
@@ -26,7 +25,15 @@ def defaults
2625
test_controller: "ApplicationController",
2726
default_preview_layout: nil,
2827
capture_compatibility_patch_enabled: false
29-
})
28+
]
29+
end
30+
31+
def defaults
32+
default
33+
end
34+
35+
def blank
36+
Layer[default.deep_transform_values { nil }]
3037
end
3138

3239
# @!attribute generate
@@ -182,14 +189,14 @@ def default_generate_options
182189

183190
# @!attribute current
184191
# @return [ViewComponent::Config]
185-
# Returns the current ViewComponent::Config. This is persisted against this
192+
# Returns the global ViewComponent::Config. This is persisted against this
186193
# class so that config options remain accessible before the rest of
187194
# ViewComponent has loaded. Defaults to an instance of ViewComponent::Config
188195
# with all other documented defaults set.
189-
class_attribute :current, default: defaults, instance_predicate: false
196+
class_attribute :global, default: defaults, instance_predicate: false
190197

191198
def initialize
192-
@config = self.class.defaults
199+
@config = self.class.blank
193200
end
194201

195202
delegate_missing_to :config

lib/view_component/engine.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
module ViewComponent
88
class Engine < Rails::Engine # :nodoc:
9-
config.view_component = ViewComponent::Config.current
9+
config.view_component = ["ApplicationComponent", "ViewComponent::Base"].map do |k|
10+
k.constantize
11+
rescue NameError
12+
nil
13+
end.compact.first.config
1014

1115
rake_tasks do
1216
load "view_component/rails/tasks/view_component.rake"

0 commit comments

Comments
 (0)