/ruby-rails:rails-generators runs instead · from ruby-rails
Creates Ruby on Rails generators and application templates to automate repetitive coding and enforce architectural patterns. Use when building custom generators, scaffolds, or whole-app setup.
This skill never runs in Claude Code
It ships in ruby-rails at skills/, but /ruby-rails:rails-generators (a command, or a skill Claude Code finds first) has the same name: typing it, or Claude choosing it, runs that one instead.
<quick_start> <basic_generator> Create a simple service object generator:
# lib/generators/service/service_generator.rb
module Generators
class ServiceGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
def create_service_file
template 'service.rb.tt', "app/services/#{file_name}_service.rb"
end
def create_service_test
template 'service_test.rb.tt', "test/services/#{file_name}_service_test.rb"
end
end
end
Template file (templates/service.rb.tt):
class <%= class_name %>Service
def initialize
end
def call
# Implementation goes here
end
end
Invoke with: rails generate service payment_processor
</basic_generator>
<usage_pattern>
Generator location: lib/generators/[name]/[name]_generator.rb
Template location: lib/generators/[name]/templates/
Test location: test/generators/[name]_generator_test.rb
</usage_pattern>
</quick_start>
<rails_8_updates>
Rails 8 introduced the authentication generator (rails generate authentication) which demonstrates modern generator patterns including ActionCable integration, controller concerns, mailer generation, and comprehensive view scaffolding. Study Rails 8 built-in generators for current best practices.
</rails_8_updates>
Rails::Generators::Base: Simple generators without required argumentsRails::Generators::NamedBase: Generators requiring a name argument (provides name, class_name, file_name, plural_name)class ServiceGenerator < Rails::Generators::NamedBase
# Automatically provides: name, class_name, file_name, plural_name
end
</step_1>
<step_2> Define source root and options:
source_root File.expand_path('templates', __dir__)
class_option :namespace, type: :string, default: nil, desc: "Namespace for the service"
class_option :skip_tests, type: :boolean, default: false, desc: "Skip test files"
Access options with: options[:namespace]
</step_2>
<step_3> Add public methods (executed in definition order):
def create_service_file
template 'service.rb.tt', service_file_path
end
def create_test_file
return if options[:skip_tests]
template 'service_test.rb.tt', test_file_path
end
private
def service_file_path
if options[:namespace]
"app/services/#{options[:namespace]}/#{file_name}_service.rb"
else
"app/services/#{file_name}_service.rb"
end
end
</step_3>
<step_4>
Create ERB templates (.tt extension):
<% if options[:namespace] -%>
module <%= options[:namespace].camelize %>
class <%= class_name %>Service
def call
# Implementation
end
end
end
<% else -%>
class <%= class_name %>Service
def call
# Implementation
end
end
<% end -%>
Important: Use <%% to output literal <% in generated files. See references/templates.md for template patterns.
</step_4>
<step_5> Test the generator (see Testing section):
rails generate service payment_processor --namespace=billing
rails generate service notifier --skip-tests
</step_5>
<common_patterns> <model_generator> Custom model with associations and scopes:
class CustomModelGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
class_option :parent, type: :string, desc: "Parent model for belongs_to"
class_option :scope, type: :array, desc: "Scopes to generate"
def create_migration
migration_template 'migration.rb.tt', "db/migrate/create_#{table_name}.rb"
end
def create_model_file
template 'model.rb.tt', "app/models/#{file_name}.rb"
end
end
See references/model-generator.md for complete example with templates. </model_generator>
<service_object_generator> Service object with result object pattern:
class ServiceGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
class_option :result_object, type: :boolean, default: true
class_option :pattern, type: :string, default: 'simple',
desc: "Service pattern (simple, command, interactor)"
def create_service
template 'service.rb.tt', "app/services/#{file_name}_service.rb"
end
def create_result_object
return unless options[:result_object]
template 'result.rb.tt', "app/services/#{file_name}_result.rb"
end
end
See references/service-generator.md for complete example with all three patterns. </service_object_generator>
<api_controller_generator> API controller with serializer:
class ApiControllerGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
class_option :serializer, type: :string, default: 'active_model_serializers'
class_option :actions, type: :array, default: %w[index show create update destroy]
def create_controller
template 'controller.rb.tt',
"app/controllers/api/v1/#{file_name.pluralize}_controller.rb"
end
def create_serializer
template "serializer_#{options[:serializer]}.rb.tt",
"app/serializers/#{file_name}_serializer.rb"
end
def add_routes
route "namespace :api do\n namespace :v1 do\n resources :#{file_name.pluralize}\n end\n end"
end
end
See references/api-generator.md for complete example with templates. </api_controller_generator>
<full_stack_scaffold> Complete feature scaffold using generator composition:
class FeatureGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
class_option :api, type: :boolean, default: false
def create_model
invoke 'model', [name], migration: true
end
def create_controller
if options[:api]
invoke 'api_controller', [name]
else
invoke 'controller', [name], actions: %w[index show new create edit update destroy]
end
end
def create_views
return if options[:api]
%w[index show new edit _form].each do |view|
template "views/#{view}.html.erb.tt",
"app/views/#{file_name.pluralize}/#{view}.html.erb"
end
end
end
See references/scaffold-generator.md for complete example. </full_stack_scaffold> </common_patterns>
<advanced_features> Generator hooks enable modular composition and test framework integration:
class ServiceGenerator < Rails::Generators::NamedBase
hook_for :test_framework, as: :service
end
This automatically invokes test_unit:service or rspec:service based on configuration. See references/hooks.md for hook patterns, responders, and fallback configuration.
<generator_composition> Invoke other generators:
def create_dependencies
invoke 'model', [name], migration: true
invoke 'service', ["#{name}_processor"]
invoke 'mailer', [name] if options[:mailer]
end
</generator_composition>
**Namespace generators** for organization:# lib/generators/admin/resource/resource_generator.rb
module Admin
module Generators
class ResourceGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
def create_admin_resource
template 'resource.rb.tt', "app/admin/#{file_name}.rb"
end
end
end
end
Invoke with: rails generate admin:resource User
See references/namespacing.md for search paths, engine generators, and gem packaging.
<file_manipulation> Thor::Actions methods available in generators:
# Create files
create_file 'config/settings.yml', yaml_content
template 'config.rb.tt', 'config/settings.rb'
# Modify existing files
insert_into_file 'config/routes.rb', route_content, after: "Rails.application.routes.draw do\n"
gsub_file 'config/application.rb', /old_value/, 'new_value'
# Rails-specific helpers
initializer 'service_config.rb', config_content
route "namespace :api do\n resources :users\n end"
See references/file-actions.md for complete reference. </file_manipulation> </advanced_features>
<application_templates>
Application templates configure a whole app (gems, auth, config, feature sets) rather than generating one component. They use the same Thor action DSL but are invoked with rails new myapp -m template.rb or bin/rails app:template LOCATION=template.rb:
# multitenant_template.rb
def source_paths
[__dir__]
end
# Guard against double application
if File.exist?('app/models/organization.rb')
say '❌ Template already applied.', :red
exit 1
end
gem 'devise', comment: 'Authentication'
gem 'acts_as_tenant', comment: 'Row-level multi-tenancy'
run 'bundle install'
# Always use the gems' own install generators — never hand-write their config
generate 'devise:install'
generate 'devise', 'User'
rails_command 'db:migrate'
run 'bundle exec rspec' # fail loudly at apply time, not first boot
Key rules: guard against re-runs (injections corrupt on second apply), prefer gem install generators over hand-rolled setup, prefer inject_into_file on stable anchors over brittle gsub_file regexes, and end by running the app's test suite.
after_bundle, composing templates with apply, and verification workflowrequire 'test_helper'
require 'generators/service/service_generator'
class ServiceGeneratorTest < Rails::Generators::TestCase
tests ServiceGenerator
destination File.expand_path('../tmp', __dir__)
setup :prepare_destination
test "generates service file" do
run_generator ["payment_processor"]
assert_file "app/services/payment_processor_service.rb" do |content|
assert_match(/class PaymentProcessorService/, content)
assert_match(/def call/, content)
end
end
test "skips tests when flag provided" do
run_generator ["payment", "--skip-tests"]
assert_file "app/services/payment_service.rb"
assert_no_file "test/services/payment_service_test.rb"
end
end
Key assertions: assert_file, assert_no_file, assert_migration, assert_class_method, assert_instance_method
source_root points to templates directory.tt extension)<common_issues>
Missing source_root: Add source_root File.expand_path('templates', __dir__) to generator class.
Incorrect template syntax: Use <%% for literal ERB in generated files: <%%= @user.name %> generates <%= @user.name %>.
Option not recognized: Define with class_option :namespace, type: :string and access with options[:namespace].
Method order issues: Public methods execute in definition order. Place dependent steps after their prerequisites. </common_issues>
<reference_guides> Detailed references:
rails new -m / app:template<success_criteria>
A well-built Rails generator has clear inheritance, properly configured source_root, well-defined class_option declarations, public methods in logical order, correct ERB templates, comprehensive tests, and consistent Rails naming conventions.
</success_criteria>
Sources:
references/api-generator.md (opens in a new tab)references/application-templates.md (opens in a new tab)references/examples.md (opens in a new tab)references/file-actions.md (opens in a new tab)references/hooks.md (opens in a new tab)references/model-generator.md (opens in a new tab)references/multitenant-template.md (opens in a new tab)references/namespacing.md (opens in a new tab)references/scaffold-generator.md (opens in a new tab)references/service-generator.md (opens in a new tab)references/templates.md (opens in a new tab)references/testing-rails.md (opens in a new tab)references/testing-rspec.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub el-feo/ai-context --plugin ruby-rails13 skills in this plugin
/brakemanStatic analysis security vulnerability scanner for Ruby on Rails applications. Use when analyzing Rails code for security issues, running security audits, reviewing code for vulnerabilities, setting up security scanning in CI/CD, managing security warnings, or investigating specific vulnerability types (SQL injection, XSS, command injection, etc.). Also use when configuring Brakeman, reducing false positives, or integrating with automated workflows./cucumber-gherkinBDD testing with Cucumber and Gherkin for Ruby and Rails applications. Use when writing feature files (.feature), step definitions, hooks, or implementing Behaviour-Driven Development in Ruby/Rails projects. Covers Gherkin keywords (Feature, Scenario, Given/When/Then, Background, Scenario Outline, Rule), Ruby step definition patterns, Cucumber Expressions, hooks (Before/After/BeforeAll/AfterAll), tags, data tables, doc strings, World modules, and Capybara integration. Triggers on cucumber, gherkin, BDD, feature files, step definitions, acceptance testing, executable specifications./design-patterns-rubyComprehensive Ruby implementations of Gang of Four (GoF) design patterns. Use when implementing object-oriented design solutions, refactoring to reduce coupling, solving architecture problems, or when user mentions specific patterns (Factory, Singleton, Observer, Strategy, etc.) in Ruby context./postgresql-rails-analyzerComprehensive PostgreSQL configuration and usage analysis for Rails applications. Use when Claude Code needs to analyze a Rails codebase for database performance issues, optimization opportunities, or best practice violations. Detects N+1 queries, missing indexes, suboptimal database configurations, anti-patterns, and provides actionable recommendations. Ideal for performance audits, optimization tasks, or when users ask to "analyze the database", "check for N+1 queries", "optimize PostgreSQL", "review database performance", or "suggest database improvements"./railsComprehensive Ruby on Rails v8.1 development guide with detailed documentation for Active Record, controllers, views, routing, testing, jobs, mailers, and more. Use when working on Rails applications, building Rails features, debugging Rails code, writing migrations, setting up associations, configuring Rails apps, or answering questions about Rails best practices and patterns./review-ruby-codeComprehensive Ruby and Rails code review using Sandi Metz rules and SOLID principles. Analyzes changed files in current branch vs base branch, runs rubycritic and simplecov, identifies OOP violations, Rails anti-patterns, security issues, code smells, and test coverage gaps. Outputs REVIEW.md with VSCode-compatible file links. Use when reviewing Ruby/Rails code, conducting code reviews, checking for design issues, pull request review, code quality analysis, or when user mentions Sandi Metz, POODR, 99 Bottles, SOLID, Law of Demeter, or "Tell Don't Ask".Other plugins' skills for the same job