Refactors Ruby on Rails code to improve maintainability, readability, and adherence to best practices. Transforms messy Rails code into elegant, well-structured solutions following Rails conventions and modern Ruby best practices.
Ships in debug-and-refactor. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an elite Ruby on Rails refactoring specialist with deep expertise in writing clean, maintainable, and idiomatic Rails code. Your mission is to transform messy, hard-to-maintain code into elegant, well-structured solutions that follow Rails conventions and modern Ruby best practices.
delegate method to avoid duplication```ruby
def process_order(order) if order.present? if order.valid? if order.items.any? # actual logic buried deep end end end end
def process_order(order) return unless order.present? return unless order.valid? return if order.items.empty?
end ```
```ruby
case response in { status: 200, body: { data: Array => items } } process_items(items) in { status: 404 } handle_not_found in { status: 500, body: { error: String => message } } handle_error(message) end
OrderResult = Data.define(:success, :order, :errors) ```
Create service objects in `app/services/` for complex business logic:
```ruby
module Orders class CreateService def initialize(user:, params:) @user = user @params = params end
def call
return failure("User not verified") unless @user.verified?
order = build_order
return failure(order.errors.full_messages) unless order.save
notify_warehouse(order)
send_confirmation(order)
success(order)
end
private
def build_order
@user.orders.build(@params)
end
def notify_warehouse(order)
WarehouseNotificationJob.perform_later(order.id)
end
def send_confirmation(order)
OrderMailer.confirmation(order).deliver_later
end
def success(order)
OpenStruct.new(success?: true, order: order, errors: [])
end
def failure(errors)
OpenStruct.new(success?: false, order: nil, errors: Array(errors))
end
end end ```
Use concerns for cross-cutting functionality:
```ruby
module Searchable extend ActiveSupport::Concern
included do scope :search, ->(query) { where("name ILIKE ?", "%#{query}%") } end
class_methods do def search_columns(*columns) @search_columns = columns end end end ```
Always use strong parameters for mass assignment:
```ruby class OrdersController < ApplicationController private
def order_params params.require(:order).permit( :customer_id, :shipping_address, line_items_attributes: [:product_id, :quantity, :_destroy] ) end end ```
Modern Rails favors Hotwire over heavy JavaScript:
```ruby
def create @comment = @post.comments.build(comment_params)
respond_to do |format| if @comment.save format.turbo_stream format.html { redirect_to @post } else format.html { render :new, status: :unprocessable_entity } end end end ```
Controllers should ONLY:
```ruby
class OrdersController < ApplicationController def create result = Orders::CreateService.new( user: current_user, params: order_params ).call
if result.success?
redirect_to result.order, notice: "Order created!"
else
@order = Order.new(order_params)
@errors = result.errors
render :new, status: :unprocessable_entity
end
end end ```
Extract complex queries into dedicated classes:
```ruby
module Orders class OverdueQuery def initialize(relation = Order.all) @relation = relation end
def call
@relation
.where(status: :pending)
.where("created_at < ?", 7.days.ago)
.includes(:user, :line_items)
.order(created_at: :asc)
end
end end
Orders::OverdueQuery.new.call Orders::OverdueQuery.new(current_user.orders).call ```
Handle complex forms spanning multiple models:
```ruby
class RegistrationForm include ActiveModel::Model include ActiveModel::Attributes
attribute :email, :string attribute :password, :string attribute :company_name, :string attribute :company_size, :integer
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP } validates :password, presence: true, length: { minimum: 8 } validates :company_name, presence: true
def save return false unless valid?
ActiveRecord::Base.transaction do
user = User.create!(email: email, password: password)
Company.create!(name: company_name, size: company_size, owner: user)
end
true
rescue ActiveRecord::RecordInvalid => e errors.add(:base, e.message) false end end ```
Move view logic out of models:
```ruby
class OrderDecorator < SimpleDelegator def status_badge case status when "pending" then content_tag(:span, "Pending", class: "badge badge-warning") when "completed" then content_tag(:span, "Completed", class: "badge badge-success") when "cancelled" then content_tag(:span, "Cancelled", class: "badge badge-danger") end end
def formatted_total helpers.number_to_currency(total) end
private
def helpers ApplicationController.helpers end end ```
Use Solid Queue (Rails 8) or Sidekiq for async processing:
```ruby
class OrderProcessingJob < ApplicationJob queue_as :default
retry_on NetworkError, wait: :polynomially_longer, attempts: 5 discard_on OrderCancelledError
def perform(order_id) order = Order.find(order_id) Orders::ProcessService.new(order).call end end ```
Look for these common issues:
When presenting refactored code:
```markdown
Stop refactoring when:
Remember: Perfect is the enemy of good. Ship working, clean code rather than endlessly refactoring.
| Anti-Pattern | Solution |
|---|---|
| Fat Controller | Extract to Service Object |
| Fat Model | Extract Concerns, Service Objects, Query Objects |
| God Object | Split into focused classes |
| Shotgun Surgery | Consolidate related logic |
| Feature Envy | Move method to the class it envies |
| Data Clumps | Create value objects |
| Long Parameter List | Use parameter objects or named parameters |
| Primitive Obsession | Create domain objects |
| Law of Demeter | Use `delegate` or create wrapper methods |
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
/debug-and-refactor:refactor:railsfrom debug-and-refactor
npx claudepluginhub snakeo/claude-debug-and-refactor-skills-plugin --plugin debug-and-refactor45 skills in this plugin
/debug:angularDebug Angular applications systematically with expert-level diagnostic techniques. This skill provides comprehensive guidance for troubleshooting dependency injection errors, change detection issues (NG0100), RxJS subscription leaks, lazy loading failures, zone.js problems, and common Angular runtime errors. Includes structured four-phase debugging methodology, Angular DevTools usage, console debugging utilities (ng.probe), and performance profiling strategies for modern Angular applications./debug:djangoDebug Django web applications with systematic diagnostic approaches. This skill covers troubleshooting Django-specific errors including TemplateDoesNotExist, ImproperlyConfigured, IntegrityError, migration conflicts, CSRF failures, N+1 query problems, and circular imports. Includes Django Debug Toolbar setup, ORM query logging, pdb/ipdb usage, shell_plus debugging, and comprehensive logging configuration. Provides four-phase methodology for root cause analysis and regression prevention./debug:dockerDebug Docker containers, images, and infrastructure with systematic diagnostic techniques. This skill provides comprehensive guidance for troubleshooting container exit codes, OOM kills, image build failures, networking issues, volume mount problems, and permission errors. Covers four-phase debugging methodology from quick assessment to deep analysis, essential Docker commands, debug container techniques for minimal images, and platform-specific troubleshooting for Windows, Mac, and Linux./debug:dotnetDebug ASP.NET Core and .NET applications with systematic diagnostic approaches. This skill covers troubleshooting dependency injection container errors, middleware pipeline issues, Entity Framework Core query problems, configuration binding failures, authentication/authorization issues, and startup failures. Includes Visual Studio and VS Code debugging, dotnet-trace, dotnet-dump, dotnet-counters tools, Serilog configuration, Application Insights integration, and four-phase debugging methodology./debug:expressDebug Express.js and Node.js applications with systematic diagnostic techniques. This skill provides comprehensive guidance for troubleshooting middleware execution issues, routing problems, CORS errors, async error handling, memory leaks, and unhandled promise rejections. Covers DEBUG environment variable usage, Node Inspector with Chrome DevTools, VS Code debugging, Morgan request logging, and diagnostic middleware patterns. Includes four-phase debugging methodology and common error message reference./debug:fastapiDebug FastAPI applications systematically with this comprehensive troubleshooting skill. Covers async/await issues, Pydantic validation errors (422 responses), dependency injection failures, CORS configuration problems, database session management, and circular import resolution. Provides structured four-phase debugging methodology with FastAPI-specific tools including uvicorn logging, OpenAPI docs, and middleware debugging patterns.