- Home
- Skills
- Thibautbaissac
- Rails Ai Agents
- Rails Service Object
rails-service-object_skill
269
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill thibautbaissac/rails_ai_agents --skill rails-service-object- SKILL.md8.4 KB
Overview
This skill generates Rails service objects that follow the single-responsibility principle and include comprehensive RSpec specs. It scaffolds a consistent service interface (single #call method) and a reusable Result object so services return a predictable success/failure contract. Use it to extract business logic from controllers, jobs, or background workers while keeping code testable and maintainable.
How this skill works
The skill creates a namespaced service class with an initializer that accepts injected dependencies and a single public #call method that returns a Result object. It also generates focused RSpec examples (red/green workflow) that drive implementation, plus helper files like a shared Result class and optional base ApplicationService. Error handling, transactions, and dependency mocking are illustrated in the generated code and tests.
When to use it
- Extract complex business logic from controllers or models
- Implement operations that coordinate multiple models or external APIs
- Create interactors or POROs with clear input/output contracts
- Encapsulate side effects (email, payment, inventory) for testability
- Prefer models for simple CRUD; skip service for trivial single-model validation
Best practices
- Name services VerbNounService and place under app/services/[namespace]
- Expose a single public #call and always return a Result object
- Inject dependencies via the constructor to allow easy mocking
- Write the service spec first (RED), implement, then iterate on error cases
- Keep services focused; avoid accumulating unrelated responsibilities
Example use cases
- Orders::CreateService: validate inventory, create order, charge payment, send confirmation
- Users::RegisterService: create user, send welcome email, schedule background tasks
- Payments::ChargeService: call external gateway, update records, handle retries
- Jobs invoking services to run idempotent business flows and log failures
- Shared operations used by multiple controllers or rake tasks
FAQ
Use a service when logic spans models, external systems, or controllers; keep simple CRUD and single-model validation in the model.
How should errors be surfaced from a service?
Return a Result object with success? false, an error message, and a programmatic code symbol rather than raising uncontrolled exceptions.