Skip to content

Commit

Permalink
Initialize ViewComponent::Config with defaults before framework load
Browse files Browse the repository at this point in the history
This commit introduces `ViewComponent::Config.current`, which should not be used directly by consumers of ViewComponent, but is used as the common thread from which `ViewComponent::Base` and `Rails.application.config.view_component` will load their config once each is ready. It'll make config available from the very start with the intent of not relying upon the framework to find these options - we can manually set anything reliant on Action View, for example, within app/engine config.
  • Loading branch information
boardfish committed Jul 19, 2023
1 parent 0dd2cf9 commit 1b1ebc4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Initialize ViewComponent::Config with defaults before framework load.

*Simon Fish*

* Add Skroutz to users list.

*Chris Nitsas*
Expand Down
6 changes: 4 additions & 2 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class << self
#
# @return [ActiveSupport::OrderedOptions]
def config
@config ||= ViewComponent::Config.defaults
ViewComponent::Config.current
end

# Replaces the entire config. You shouldn't need to use this directly
# unless you're building a `ViewComponent::Config` elsewhere.
attr_writer :config
def config=(*args)
ViewComponent::Config.current = *args
end
end

include ViewComponent::InlineTemplate
Expand Down
8 changes: 8 additions & 0 deletions lib/view_component/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ def default_generate_options
end
end

# @!attribute current
# @return [ViewComponent::Config]
# Returns the current ViewComponent::Config. This is persisted against this
# class so that config options remain accessible before the rest of
# ViewComponent has loaded. Defaults to an instance of ViewComponent::Config
# with all other documented defaults set.
class_attribute :current, default: defaults, instance_predicate: false

def initialize
@config = self.class.defaults
end
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module ViewComponent
class Engine < Rails::Engine # :nodoc:
config.view_component = ViewComponent::Base.config
config.view_component = ViewComponent::Config.current

rake_tasks do
load "view_component/rails/tasks/view_component.rake"
Expand Down
2 changes: 1 addition & 1 deletion test/sandbox/test/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_all_methods_are_documented
end
options_defined_on_instance = Set[*default_options, *accessors]
assert options_defined_on_instance.subset?(Set[*configuration_methods_to_document.map(&:name)]),
"Not all configuration options are documented: #{configuration_methods_to_document.map(&:name) - options_defined_on_instance.to_a}"
"Not all configuration options are documented: #{options_defined_on_instance.to_a - configuration_methods_to_document.map(&:name)}"
assert configuration_methods_to_document.map(&:docstring).all?(&:present?),
"Configuration options are missing docstrings."
end
Expand Down

0 comments on commit 1b1ebc4

Please sign in to comment.