Skip to content

Commit

Permalink
Add parent option (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spone authored Sep 17, 2021
1 parent 69b11f5 commit 3080670
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
8 changes: 8 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions docs/guide/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
9 changes: 8 additions & 1 deletion lib/rails/generators/component/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ComponentGenerator < Rails::Generators::NamedBase
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

Expand All @@ -32,7 +33,13 @@ def create_component_file
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
Expand Down
9 changes: 9 additions & 0 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ def content_evaluated?
# 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
Expand Down
30 changes: 29 additions & 1 deletion test/generators/component_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_component
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
Expand Down Expand Up @@ -83,6 +83,14 @@ def test_component_with_inline
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]

Expand Down Expand Up @@ -136,6 +144,26 @@ def test_generating_components_with_custom_component_path
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]

Expand Down
15 changes: 15 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ def with_custom_component_path(new_value)
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
Expand Down

0 comments on commit 3080670

Please sign in to comment.