Add parent option (#1073)
This commit is contained in:
Родитель
69b11f59f8
Коммит
3080670f0e
|
@ -7,6 +7,11 @@ title: Changelog
|
|||
|
||||
## main
|
||||
|
||||
* Add `--parent` generator option to specify the parent class.
|
||||
* Add config option `config.view_component.component_parent_class` to change it project-wide.
|
||||
|
||||
*Hans Lemuet*
|
||||
|
||||
* Update docs to add example for using Devise helpers in tests.
|
||||
|
||||
*Matthew Rider*
|
||||
|
|
|
@ -74,6 +74,14 @@ _Will be removed in v3.0.0._
|
|||
|
||||
## Configuration
|
||||
|
||||
### #component_parent_class
|
||||
|
||||
Parent class for generated components
|
||||
|
||||
config.view_component.component_parent_class = "MyBaseComponent"
|
||||
|
||||
Defaults to "ApplicationComponent" if defined, "ViewComponent::Base" otherwise.
|
||||
|
||||
### #default_preview_layout
|
||||
|
||||
Set a custom default layout used for preview index and individual previews:
|
||||
|
|
|
@ -105,6 +105,22 @@ bin/rails generate component Example title --inline
|
|||
invoke erb
|
||||
```
|
||||
|
||||
### Specify the parent class
|
||||
|
||||
By default, `ApplicationComponent` is used if defined, `ViewComponent::Base` otherwise.
|
||||
|
||||
```console
|
||||
bin/rails generate component Example title content --parent MyBaseComponent
|
||||
|
||||
create app/components/example_component.rb
|
||||
invoke test_unit
|
||||
create test/components/example_component_test.rb
|
||||
invoke erb
|
||||
create app/components/example_component.html.erb
|
||||
```
|
||||
|
||||
To always use your own parent class, set `config.view_component.component_parent_class = "MyBaseComponent"`.
|
||||
|
||||
### Skip collision check
|
||||
|
||||
The generator prevents naming collisions with existing components. To skip this check and force the generator to run, use the `--skip-collision-check` or `--force` option.
|
||||
|
|
|
@ -12,6 +12,7 @@ module Rails
|
|||
argument :attributes, type: :array, default: [], banner: "attribute"
|
||||
check_class_collision suffix: "Component"
|
||||
class_option :inline, type: :boolean, default: false
|
||||
class_option :parent, type: :string, desc: "The parent class for the generated component"
|
||||
class_option :stimulus, type: :boolean, default: ViewComponent::Base.generate_stimulus_controller
|
||||
class_option :sidecar, type: :boolean, default: false
|
||||
|
||||
|
@ -32,7 +33,13 @@ module Rails
|
|||
private
|
||||
|
||||
def parent_class
|
||||
defined?(ApplicationComponent) ? "ApplicationComponent" : "ViewComponent::Base"
|
||||
if options[:parent]
|
||||
options[:parent]
|
||||
elsif defined?(ApplicationComponent)
|
||||
ApplicationComponent
|
||||
else
|
||||
ViewComponent::Base.component_parent_class
|
||||
end
|
||||
end
|
||||
|
||||
def initialize_signature
|
||||
|
|
|
@ -281,6 +281,15 @@ module ViewComponent
|
|||
# Defaults to "app/components".
|
||||
mattr_accessor :view_component_path, instance_writer: false, default: "app/components"
|
||||
|
||||
# Parent class for generated components
|
||||
#
|
||||
# config.view_component.component_parent_class = "MyBaseComponent"
|
||||
#
|
||||
# Defaults to "ApplicationComponent" if defined, "ViewComponent::Base" otherwise.
|
||||
mattr_accessor :component_parent_class,
|
||||
instance_writer: false,
|
||||
default: "ViewComponent::Base"
|
||||
|
||||
class << self
|
||||
# @private
|
||||
attr_accessor :source_location, :virtual_path
|
||||
|
|
|
@ -17,7 +17,7 @@ class ComponentGeneratorTest < Rails::Generators::TestCase
|
|||
run_generator
|
||||
|
||||
assert_file "app/components/user_component.rb" do |component|
|
||||
assert_match(/class UserComponent < /, component)
|
||||
assert_match(/class UserComponent < ViewComponent::Base/, component)
|
||||
assert_no_match(/def initialize/, component)
|
||||
end
|
||||
end
|
||||
|
@ -83,6 +83,14 @@ class ComponentGeneratorTest < Rails::Generators::TestCase
|
|||
assert_no_file "component.html.erb"
|
||||
end
|
||||
|
||||
def test_component_with_parent
|
||||
run_generator %w[user --parent MyBaseComponent]
|
||||
|
||||
assert_file "app/components/user_component.rb" do |component|
|
||||
assert_match(/class UserComponent < MyBaseComponent/, component)
|
||||
end
|
||||
end
|
||||
|
||||
def test_component_with_namespace
|
||||
run_generator %w[admins/user]
|
||||
|
||||
|
@ -136,6 +144,26 @@ class ComponentGeneratorTest < Rails::Generators::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_generating_components_with_application_component_class
|
||||
with_application_component_class do
|
||||
run_generator %w[user]
|
||||
|
||||
assert_file "app/components/user_component.rb" do |component|
|
||||
assert_match(/class UserComponent < ApplicationComponent/, component)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_generating_components_with_custom_component_parent_class
|
||||
with_custom_component_parent_class("MyBaseComponent") do
|
||||
run_generator %w[user]
|
||||
|
||||
assert_file "app/components/user_component.rb" do |component|
|
||||
assert_match(/class UserComponent < MyBaseComponent/, component)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_component_with_stimulus
|
||||
run_generator %w[user --stimulus]
|
||||
|
||||
|
|
|
@ -60,6 +60,21 @@ ensure
|
|||
ViewComponent::Base.view_component_path = old_value
|
||||
end
|
||||
|
||||
def with_custom_component_parent_class(new_value)
|
||||
old_value = ViewComponent::Base.component_parent_class
|
||||
ViewComponent::Base.component_parent_class = new_value
|
||||
yield
|
||||
ensure
|
||||
ViewComponent::Base.component_parent_class = old_value
|
||||
end
|
||||
|
||||
def with_application_component_class
|
||||
Object.const_set("ApplicationComponent", Class.new(Object))
|
||||
yield
|
||||
ensure
|
||||
Object.send(:remove_const, :ApplicationComponent)
|
||||
end
|
||||
|
||||
def with_new_cache
|
||||
begin
|
||||
old_cache = ViewComponent::CompileCache.cache
|
||||
|
|
Загрузка…
Ссылка в новой задаче