From 8b441cc86af151eac8b9790558a1ae4fe6ca6f8a Mon Sep 17 00:00:00 2001 From: KAWAKAMI Moeki <72325947+moekiorg@users.noreply.github.com> Date: Thu, 19 Dec 2024 04:28:17 +0900 Subject: [PATCH] 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 <72325947+kawakamimoeki@users.noreply.github.com> Co-authored-by: Joel Hawksley Co-authored-by: Joel Hawksley --- docs/CHANGELOG.md | 4 ++++ docs/index.md | 1 + lib/rails/generators/component/component_generator.rb | 3 ++- .../generators/component/templates/component.rb.tt | 2 +- .../test/generators/component_generator_test.rb | 10 ++++++++++ 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9760606aa..43e298e83 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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* diff --git a/docs/index.md b/docs/index.md index 605ea5c16..843311796 100644 --- a/docs/index.md +++ b/docs/index.md @@ -230,6 +230,7 @@ ViewComponent is built by over a hundred members of the community, including: tkowalewski chloe-meister zaratan +kawakamimoeki ## Who uses ViewComponent? diff --git a/lib/rails/generators/component/component_generator.rb b/lib/rails/generators/component/component_generator.rb index 4077c78f6..d5e4b8b3c 100644 --- a/lib/rails/generators/component/component_generator.rb +++ b/lib/rails/generators/component/component_generator.rb @@ -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 diff --git a/lib/rails/generators/component/templates/component.rb.tt b/lib/rails/generators/component/templates/component.rb.tt index 997e5d692..0d70706cf 100644 --- a/lib/rails/generators/component/templates/component.rb.tt +++ b/lib/rails/generators/component/templates/component.rb.tt @@ -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 %> diff --git a/test/test_engine/test/generators/component_generator_test.rb b/test/test_engine/test/generators/component_generator_test.rb index 63f0307ba..177c62ea1 100644 --- a/test/test_engine/test/generators/component_generator_test.rb +++ b/test/test_engine/test/generators/component_generator_test.rb @@ -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