Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --skip-suffix option to component generator #2166

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

* Add `--skip-suffix` option to component generator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add `--skip-suffix` option to component generator
* Add `--skip-suffix` option to component generator.


*KAWAKAMI Moeki*

* Add FreeATS to list of companies using ViewComponent.

*Ilia Liamshin*
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/tkowalewski" alt="tkowalewski" width="32" />
<img src="https://avatars.githubusercontent.com/chloe-meister" alt="chloe-meister" width="32" />
<img src="https://avatars.githubusercontent.com/zaratan" alt="zaratan" width="32" />
<img src="https://avatars.githubusercontent.com/kawakamimoeki" alt="kawakamimoeki" width="32" />

## Who uses ViewComponent?

Expand Down
3 changes: 2 additions & 1 deletion lib/rails/generators/component/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class ComponentGenerator < Rails::Generators::NamedBase
class_option :sidecar, type: :boolean, default: false
class_option :stimulus, type: :boolean,
default: ViewComponent::Base.config.generate.stimulus_controller
class_option :skip_suffix, type: :boolean, default: false

def create_component_file
template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
template "component.rb", File.join(component_path, class_path, "#{file_name}#{options[:skip_suffix] ? "" : "_component"}.rb")
end

hook_for :test_framework
Expand Down
2 changes: 1 addition & 1 deletion lib/rails/generators/component/templates/component.rb.tt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

<% module_namespacing do -%>
class <%= class_name %>Component < <%= parent_class %>
class <%= class_name %><%= options[:skip_suffix] ? "" : "Component" %> < <%= parent_class %>
<%- if initialize_signature -%>
def initialize(<%= initialize_signature %>)
<%= initialize_body %>
Expand Down
10 changes: 10 additions & 0 deletions test/test_engine/test/generators/component_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ def test_component
assert_no_match(/def initialize/, component)
end
end

def test_component_without_suffix
run_generator %w[example --skip-suffix]

assert_file "app/components/test_engine/example.rb" do |component|
assert_match(/module TestEngine/, component)
assert_match(/class Example < ViewComponent::Base/, component)
assert_no_match(/def initialize/, component)
end
end
end
Loading