Agent skill
rails-service-patterns
Analyzes and recommends Rails service object patterns for business logic extraction including command objects, result objects, form objects, query objects, and external API integration. Use when extracting logic from controllers/models, orchestrating multi-step workflows, or organizing app/services. NOT for simple CRUD, model validations, controller routing, or background job scheduling.
Install this agent skill to your Project
npx add-skill https://github.com/ag0os/rails-dev-plugin/tree/main/skills/rails-service-patterns
SKILL.md
Rails Service Object Patterns
Analyze and recommend patterns for extracting and organizing business logic in Rails applications.
Quick Reference
| 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? |
Supporting Documentation
- patterns.md - Result objects, form objects, and profile-aware guidance
Core Principles
- VerbNoun naming:
CreateOrder,SendInvitation-- neverOrderServiceorUserManager - One public method: Expose only
call(orperform) - Explicit return values: Use Result objects, never exceptions for expected flow control
- Profile-aware extraction: See "When to Extract" below
When to Extract a Service (Profile-Dependent)
| Scenario | Omakase | Service-Oriented / API-First |
|---|---|---|
| Logic on a single model's own data | Model method or concern | Model method |
| Shared behavior across models | Concern | Concern |
| Domain logic for one model | Concern | Service object |
| Multi-model workflow with rollback | Model method + transaction | Service object |
| External API call | Model method wrapping client | Service object |
| Simple side effect (email, log) | Callback (after_commit) |
Service object |
Omakase: Only extract to a service when the workflow genuinely spans multiple unrelated models or external systems. Prefer concerns and enriched model methods.
Service-oriented / API-first: Service objects are the default extraction target for any non-trivial business logic.
Result Object Pattern
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 for the enhanced monad-like ServiceResult with on_success/on_failure chaining.
Anti-Patterns
| 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 |
Output Format
When analyzing or creating services, provide:
- Service file in
app/services/with VerbNoun naming - Result struct if callers need success/failure status
- Controller integration showing how to call and handle results
- Test outline covering happy path, failure cases, and edge cases
- Error handling strategy (Result objects for expected failures, exceptions for unexpected)
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
rails-caching-patterns
Caching patterns for Rails applications including fragment caching, low-level caching, HTTP caching, Russian doll caching, and cache invalidation strategies. Automatically invoked when working with Rails.cache, cache stores, stale?/fresh_when, fragment caching, cache keys, or performance optimization through caching. Triggers on "cache", "caching", "Rails.cache", "fragment cache", "Russian doll", "stale?", "fresh_when", "cache key", "cache store", "Redis cache", "Solid Cache", "memcached", "ETag", "cache invalidation", "cache bust". NOT for CDN configuration (use rails-devops-patterns) or database query optimization (use rails-model-patterns).
rails-graphql-patterns
Analyzes and recommends GraphQL patterns for Rails using graphql-ruby including schema design, types, resolvers, mutations, subscriptions, DataLoader, and query complexity. Use when building GraphQL APIs, defining types, writing mutations, optimizing N+1 queries, or structuring app/graphql. NOT for REST API controllers, ActiveRecord queries outside GraphQL, or Turbo Stream responses.
ruby-object-design
Automatically invoked when making decisions about Ruby code structure and organization. Triggers on "class or module", "should this be a class", "struct vs class", "PORO", "data object", "design pattern", "class vs module", "when to use class", "module vs class", "stateless class", "value object", "data container", "object factory", "extend self", "singleton class". Provides guidance on choosing the right Ruby construct (class, module, Struct, Data, Hash). NOT for code smell identification or refactoring (use ruby-refactoring) or Rails-specific framework patterns.
rails-views-patterns
Analyzes Rails view templates, partials, layouts, helpers, and form patterns for best practices. Use when reviewing ERB templates, improving view performance with fragment caching, fixing form helpers, organizing partials, adding accessibility attributes, or evaluating collection rendering. NOT for Stimulus/Turbo logic (use hotwire-patterns), controller concerns, or API-only responses.
rails-architecture-patterns
Provides architectural planning, design decisions, and coordination guidance for Rails applications. Use when planning new features, choosing between design approaches (STI vs polymorphic, service vs concern, monolith vs engine), evaluating system architecture, or deciding which domain skill or agent to delegate to. NOT for implementation details within a single domain (use the domain-specific skill instead).
rails-mailer-patterns
Action Mailer patterns for Rails applications. Automatically invoked when working with email delivery, mailer classes, email templates, mailer previews, interceptors, or delivery configuration. Triggers on "mailer", "email", "ActionMailer", "deliver_later", "deliver_now", "mail template", "email preview", "SMTP", "SendGrid", "Postmark", "notification email". NOT for push notifications, SMS, or in-app messaging.
Didn't find tool you were looking for?