Skip to content

Commit

Permalink
feat: Add --skip-suffix option to component generator (#2166)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people authored Dec 18, 2024
1 parent 2b46c50 commit 8b441cc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 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

* 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

0 comments on commit 8b441cc

Please sign in to comment.