Skip to content

Commit f6afae9

Browse files
committed
Add mechanism for component-local inheritable config
Preview namespace additionally added to config object in this commit to test config inheritance. Much more cleanup to be done around making as many things as possible component-local.
1 parent 37e89f8 commit f6afae9

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

lib/view_component/base.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@
1919

2020
module ViewComponent
2121
class Base < ActionView::Base
22+
# Returns the current config.
23+
#
24+
# @return [ActiveSupport::OrderedOptions]
25+
class_attribute :config, default: ViewComponent::Config.defaults
26+
2227
class << self
2328
delegate(*ViewComponent::Config.defaults.keys, to: :config)
2429

25-
# Returns the current config.
26-
#
27-
# @return [ActiveSupport::OrderedOptions]
28-
def config
29-
ViewComponent::Config.current
30+
def configure(&block)
31+
config.instance_eval(&block)
3032
end
3133
end
3234

@@ -40,7 +42,7 @@ def config
4042
VC_INTERNAL_DEFAULT_FORMAT = :html
4143

4244
# For CSRF authenticity tokens in forms
43-
delegate :form_authenticity_token, :protect_against_forgery?, :config, to: :helpers
45+
delegate :form_authenticity_token, :protect_against_forgery?, to: :helpers
4446

4547
# For Content Security Policy nonces
4648
delegate :content_security_policy_nonce, to: :helpers

lib/view_component/config.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def defaults
1515
generate: default_generate_options,
1616
preview_controller: "ViewComponentsController",
1717
preview_route: "/rails/view_components",
18+
preview: ActiveSupport::OrderedOptions.new({
19+
paths: default_preview_paths
20+
}),
1821
show_previews_source: false,
1922
instrumentation_enabled: false,
2023
use_deprecated_instrumentation_name: true,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class ConfigBaseComponent < ViewComponent::Base
4+
configure do
5+
preview.paths = ["expected_path"]
6+
end
7+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class InheritedConfigComponent < ConfigBaseComponent
4+
configure do
5+
preview.paths << "another_expected_path"
6+
end
7+
end

test/sandbox/test/base_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,18 @@ def test_no_method_error_does_not_reference_missing_helper
145145
MESSAGE
146146
assert !exception_message_regex.match?(exception.message)
147147
end
148+
149+
def test_configuration_dsl
150+
component_class = Class.new(ViewComponent::Base) do
151+
configure do
152+
preview.paths = ["expected_path"]
153+
end
154+
end
155+
156+
assert_equal ConfigBaseComponent.new.config.preview.paths, ["expected_path"]
157+
end
158+
159+
def test_inherited_configuration
160+
assert_equal InheritedConfigComponent.new.config.preview.paths, ["expected_path", "another_expected_path"]
161+
end
148162
end

0 commit comments

Comments
 (0)