Skip to content

Commit 8b441cc

Browse files
moeki0joelhawksley
andauthored
feat: Add --skip-suffix option to component generator (#2166)
* feat: Add --skip-suffix option to component generator This option allows generating component files without the "_component" suffix, giving developers more flexibility in naming their components. * update changelog * update docs * Remove trailing whitespace * Update docs/CHANGELOG.md --------- Co-authored-by: KAWAKAMI Moeki <[email protected]> Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent 2b46c50 commit 8b441cc

File tree

5 files changed

+18
-2
lines changed

5 files changed

+18
-2
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 5
1010

1111
## main
1212

13+
* Add `--skip-suffix` option to component generator.
14+
15+
*KAWAKAMI Moeki*
16+
1317
* Add FreeATS to list of companies using ViewComponent.
1418

1519
*Ilia Liamshin*

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ ViewComponent is built by over a hundred members of the community, including:
230230
<img src="https://avatars.githubusercontent.com/tkowalewski" alt="tkowalewski" width="32" />
231231
<img src="https://avatars.githubusercontent.com/chloe-meister" alt="chloe-meister" width="32" />
232232
<img src="https://avatars.githubusercontent.com/zaratan" alt="zaratan" width="32" />
233+
<img src="https://avatars.githubusercontent.com/kawakamimoeki" alt="kawakamimoeki" width="32" />
233234

234235
## Who uses ViewComponent?
235236

lib/rails/generators/component/component_generator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ class ComponentGenerator < Rails::Generators::NamedBase
1919
class_option :sidecar, type: :boolean, default: false
2020
class_option :stimulus, type: :boolean,
2121
default: ViewComponent::Base.config.generate.stimulus_controller
22+
class_option :skip_suffix, type: :boolean, default: false
2223

2324
def create_component_file
24-
template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
25+
template "component.rb", File.join(component_path, class_path, "#{file_name}#{options[:skip_suffix] ? "" : "_component"}.rb")
2526
end
2627

2728
hook_for :test_framework

lib/rails/generators/component/templates/component.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
<% module_namespacing do -%>
4-
class <%= class_name %>Component < <%= parent_class %>
4+
class <%= class_name %><%= options[:skip_suffix] ? "" : "Component" %> < <%= parent_class %>
55
<%- if initialize_signature -%>
66
def initialize(<%= initialize_signature %>)
77
<%= initialize_body %>

test/test_engine/test/generators/component_generator_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ def test_component
1717
assert_no_match(/def initialize/, component)
1818
end
1919
end
20+
21+
def test_component_without_suffix
22+
run_generator %w[example --skip-suffix]
23+
24+
assert_file "app/components/test_engine/example.rb" do |component|
25+
assert_match(/module TestEngine/, component)
26+
assert_match(/class Example < ViewComponent::Base/, component)
27+
assert_no_match(/def initialize/, component)
28+
end
29+
end
2030
end

0 commit comments

Comments
 (0)