rails-service-patterns

ag0os's avatarfrom ag0os

Rails service object patterns and business logic organization. Automatically invoked when working with service objects, extracting business logic, implementing command/query patterns, or organizing app/services. Triggers on "service object", "service", "business logic", "workflow", "orchestration", "command pattern", "query object", "form object", "interactor", "result object".

⭐2stars🔀0forks📁View on GitHub🕐Updated Jan 8, 2026

When & Why to Use This Skill

This Claude skill assists Ruby on Rails developers in implementing robust service object patterns and organizing complex business logic. It streamlines the process of extracting logic from bloated controllers and models into maintainable, testable units, following industry best practices like the command/query pattern and the single responsibility principle to improve overall application architecture.

Use Cases

  • Refactoring 'Fat Models' or 'Fat Controllers' by extracting complex business logic into dedicated, reusable service objects.
  • Orchestrating multi-step business workflows, such as order processing or user onboarding, with standardized success and failure handling using Result Objects.
  • Implementing specialized architectural patterns like Form Objects for complex data validation and Query Objects for isolating sophisticated ActiveRecord queries.
  • Improving codebase testability and flexibility by applying dependency injection and isolating side effects within focused service classes.
  • Standardizing the organization of the app/services directory to ensure a consistent and scalable structure for growing Rails applications.
namerails-service-patterns
descriptionRails service object patterns and business logic organization. Automatically invoked when working with service objects, extracting business logic, implementing command/query patterns, or organizing app/services. Triggers on "service object", "service", "business logic", "workflow", "orchestration", "command pattern", "query object", "form object", "interactor", "result object".
allowed-toolsRead, Grep, Glob

Rails Service Object Patterns

Patterns for extracting and organizing business logic in Rails applications.

When This Skill Applies

  • Extracting complex logic from controllers/models into service objects
  • Implementing command/query separation patterns
  • Handling multi-step business processes
  • Designing result objects and error handling
  • Organizing the app/services directory

Core Principles

Single Responsibility

Each service should do one thing well:

  • Name services with verb + noun: CreateOrder, SendEmail, ProcessPayment
  • Keep services focused and composable
  • One public method (typically call or perform)

Dependency Injection

Make services testable and flexible:

class NotificationService
  def initialize(mailer: UserMailer, sms_client: TwilioClient.new)
    @mailer = mailer
    @sms_client = sms_client
  end
end

Service Patterns

See patterns.md for detailed implementations of:

  • Basic Service Pattern
  • Result Object Pattern
  • Form Objects
  • Query Objects
  • Policy Objects

Quick Reference

Pattern When to Use
Basic Service Simple operations with transaction handling
Result Object When callers need success/failure + data/error
Form Object Complex forms spanning multiple models
Query Object Complex ActiveRecord queries
Policy Object Authorization logic (Pundit-style)

Testing Services

RSpec.describe CreateOrder do
  let(:user) { create(:user) }
  let(:service) { described_class.new(user, cart_items) }

  describe '#call' do
    it 'creates an order' do
      expect { service.call }.to change { Order.count }.by(1)
    end

    context 'when payment fails' do
      before { allow(PaymentProcessor).to receive(:charge).and_raise(PaymentError) }

      it 'rolls back the transaction' do
        expect { service.call }.not_to change { Order.count }
      end
    end
  end
end
  • patterns.md - Detailed service patterns with examples