rails:refactor

Extract Rails business logic into service objects, concerns, and query objects.

1|Updated Apr 2, 2026
One-click install
npx skills add https://github.com/fabn/claude-plugins --skill rails-refactor
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails:refactor
Source: https://github.com/fabn/claude-plugins/tree/main/plugins/rails/skills/refactor
Command: npx skills add https://github.com/fabn/claude-plugins --skill rails-refactor

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Identifies fat controllers, god models, repeated query chains, complex form handling, and display logic in Ruby on Rails projects and provides structured extraction patterns to reduce complexity, improve testability, and enforce single responsibility.

Core Features & Use Cases

  • Service Extraction: Move controller and model business logic into focused service objects that follow project initialization and call conventions.
  • Concern Extraction: Isolate cohesive model behavior into ActiveSupport::Concern modules for clarity and reuse.
  • Query/Form/Presenter Patterns: Replace repeated query chains with query objects, encapsulate multi-model form handling into form objects, and move display logic into DelegateClass wrappers or presenters.
  • Test & Verification Guidance: Create or update specs, run affected test suites, and lint new code to ensure behavior is preserved.
  • Use Case: Convert a 45-line OrdersController#create that handles validation, payment, and notifications into a slim controller and an OrderCreationService with accompanying specs.

Quick Start

Ask to refactor OrdersController#create by extracting an OrderCreationService, list files to create and modify, and provide the before/after code changes.

Frequently Asked Questions about rails:refactor

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor fat Ruby on Rails controllers into service objects?

Refactoring fat Ruby on Rails controllers involves extracting business logic into focused service objects. This reduces controller complexity by moving validation, payment, and notification processes into separate, testable classes following project initialization and call conventions.

When should I extract concerns from Rails models over 200 lines?

Extract concerns from Rails models over 200 lines when you need to isolate cohesive behavior into ActiveSupport::Concern modules. This separation enforces single responsibility, improves clarity, and enhances reuse across your application without cluttering the main model definition.

What is the best way to handle repeated query chains in Ruby on Rails?

The best way to handle repeated query chains in Ruby on Rails is extracting them into query objects. This pattern replaces complex, duplicated ActiveRecord queries with focused, testable classes that encapsulate specific data retrieval logic.

How do I manage multi-model forms in Rails using form objects?

Manage multi-model forms in Rails by encapsulating the complex form handling logic into form objects. This pattern separates form validation and submission processing from controllers, creating a dedicated, testable abstraction for multi-model interactions.

Can I move Rails display logic into DelegateClass wrappers?

Yes, you can move Rails display logic into DelegateClass wrappers or presenters. This approach extracts view-specific formatting and presentation code from models and controllers, enforcing single responsibility and keeping business logic separated from display concerns.

Do I need to update tests when extracting service objects from Rails controllers?

Yes, you need to update or create specs when extracting service objects from Rails controllers. Running affected test suites and linting new code ensures the extracted abstractions preserve original behavior and maintain testability throughout the refactoring process.