Skip to content

Commit

Permalink
Add mechanism for component-local inheritable config
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
boardfish committed Feb 16, 2025
1 parent 37e89f8 commit f6afae9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

module ViewComponent
class Base < ActionView::Base
# Returns the current config.
#
# @return [ActiveSupport::OrderedOptions]
class_attribute :config, default: ViewComponent::Config.defaults

class << self
delegate(*ViewComponent::Config.defaults.keys, to: :config)

# Returns the current config.
#
# @return [ActiveSupport::OrderedOptions]
def config
ViewComponent::Config.current
def configure(&block)
config.instance_eval(&block)
end
end

Expand All @@ -40,7 +42,7 @@ def config
VC_INTERNAL_DEFAULT_FORMAT = :html

# For CSRF authenticity tokens in forms
delegate :form_authenticity_token, :protect_against_forgery?, :config, to: :helpers
delegate :form_authenticity_token, :protect_against_forgery?, to: :helpers

# For Content Security Policy nonces
delegate :content_security_policy_nonce, to: :helpers
Expand Down
3 changes: 3 additions & 0 deletions lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def defaults
generate: default_generate_options,
preview_controller: "ViewComponentsController",
preview_route: "/rails/view_components",
preview: ActiveSupport::OrderedOptions.new({
paths: default_preview_paths
}),
show_previews_source: false,
instrumentation_enabled: false,
use_deprecated_instrumentation_name: true,
Expand Down
7 changes: 7 additions & 0 deletions test/sandbox/app/components/config_base_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class ConfigBaseComponent < ViewComponent::Base
configure do
preview.paths = ["expected_path"]
end
end
7 changes: 7 additions & 0 deletions test/sandbox/app/components/inherited_config_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class InheritedConfigComponent < ConfigBaseComponent
configure do
preview.paths << "another_expected_path"
end
end
14 changes: 14 additions & 0 deletions test/sandbox/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,18 @@ def test_no_method_error_does_not_reference_missing_helper
MESSAGE
assert !exception_message_regex.match?(exception.message)
end

def test_configuration_dsl
component_class = Class.new(ViewComponent::Base) do
configure do
preview.paths = ["expected_path"]
end
end

assert_equal ConfigBaseComponent.new.config.preview.paths, ["expected_path"]
end

def test_inherited_configuration
assert_equal InheritedConfigComponent.new.config.preview.paths, ["expected_path", "another_expected_path"]
end
end

0 comments on commit f6afae9

Please sign in to comment.