This skill helps you organize Rails business logic using service objects, command/query patterns, and result objects for cleaner, testable apps.
npx playbooks add skill ag0os/rails-dev-plugin --skill rails-service-patterns
Analyze and recommend patterns for extracting and organizing business logic in Rails applications.
This skill forks on Axis A — logic placement. Before recommending where logic goes:
rails-stack-profiles (Axis A only — native or extracted).native → [extraction.native.md](extraction.native.md)extracted → [extraction.extracted.md](extraction.extracted.md)If the axis cannot be resolved, default to native and state the assumption.
Everything else in this file is invariant — it holds regardless of axis.
| Pattern | Use When | Entry Point |
|---------|----------|-------------|
| Basic Service | Single operation with transaction | CreateOrder.new(...).call |
| Result Object | Caller needs success/failure + data | Result.new(success?: true, data:) |
| Form Object | Multi-model form submissions | RegistrationForm.new(params).save |
| Query Object | Complex reusable queries | UserSearchQuery.new(scope).call |
| Policy Object | Authorization decisions | PostPolicy.new(user, post).update? |
native axisextracted axisCreateOrder, SendInvitation -- never OrderService or UserManagercall (or perform)Use Struct.new(keyword_init: true) for lightweight results. Never raise exceptions for expected failures (validation, auth, payment decline).
class AuthenticateUser
Result = Struct.new(:success?, :user, :error, keyword_init: true)
def initialize(email:, password:)
@email = email
@password = password
end
def call
user = User.find_by(email: @email)
if user&.authenticate(@password)
Result.new(success?: true, user: user)
else
Result.new(success?: false, error: "Invalid credentials")
end
end
end
See [patterns.md](patterns.md) for the enhanced monad-like ServiceResult with on_success/on_failure chaining.
| Anti-Pattern | Problem | Fix |
|-------------|---------|-----|
| God service (100+ lines) | Does too much | Split into composable services |
| Raising exceptions for flow control | Expensive, hard to handle | Use Result objects |
| Deep service-calls-service chains | Hidden coupling | Orchestrate from controller or coordinator |
| self.call class method pattern | No instance state, limits DI | Use instance methods with constructor DI |
| No return value | Caller can't react to failures | Always return Result or meaningful value |
| Service modifying passed-in objects | Surprising side effects | Return new objects or be explicit |
| VerbNoun naming violation (UserService) | Unclear responsibility, attracts god service | One service = one operation = one verb |
When analyzing or creating services, provide:
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.