Rails Generators
Operandi includes Rails generators to help you quickly set up and create services in your Rails application. These generators follow Rails conventions and integrate seamlessly with your Rails workflow.
Install Generator
The install generator sets up Operandi in your Rails application by creating the base ApplicationService class and configuration files.
Usage
bin/rails generate operandi:installWhat It Creates
The install generator creates the following files:
app/services/application_service.rb- Base service class for your applicationclass ApplicationService < Operandi::Base # Add common arguments, callbacks, or helpers shared across all services. # # Example: Add a context argument for the current user # arg :current_user, type: User, optional: true, context: true endconfig/initializers/operandi.rb- Configuration file (unless--skip-initializeris used) This file contains the global configuration for Operandi in your Rails application.spec/services/application_service_spec.rb- RSpec test file (if RSpec is detected and--skip-specis not used)
Options
--skip-initializer- Skip creating the initializer file--skip-spec- Skip creating the spec file
Examples
Service Generator
The service generator creates a new service class that inherits from ApplicationService. It supports namespaced services and can pre-populate arguments, steps, and outputs.
Usage
What It Creates
The service generator creates:
Service file -
app/services/{name}.rbSpec file -
spec/services/{name}_spec.rb(if RSpec is detected and--skip-specis not used)
Options
--args- List of arguments for the service (space-separated)--steps- List of steps for the service (space-separated)--outputs- List of outputs for the service (space-separated)--skip-spec- Skip creating the spec file--parent- Parent class (default:ApplicationService)
Examples
Basic Service
Create a simple service without any predefined structure:
This creates:
Service with Arguments, Steps, and Outputs
Create a fully structured service:
This creates:
Namespaced Service
Create a service within a namespace:
This creates:
Custom Parent Class
Create a service that inherits from a custom parent class:
RSpec Integration
Both generators automatically detect if RSpec is installed in your Rails application by checking for the presence of the spec/ directory. If RSpec is detected, the generators will create corresponding spec files with basic test structure.
Example Spec File
You can skip spec file generation with the --skip-spec option:
Best Practices
Run the install generator first - Always run
operandi:installbefore creating individual services to set up the baseApplicationServiceclass.Use namespaces - Organize related services under namespaces (e.g.,
User::Create,Payment::Process) to keep your services organized.Start with structure - Use
--args,--steps, and--outputsoptions to create a skeleton for your service, then fill in the implementation.Keep it simple - Don't over-specify. If you're not sure about the exact steps, create a basic service and add them as you develop.
Follow conventions - Use descriptive names for services that indicate the action being performed (e.g.,
CreateOrder,User::Authenticate,Payment::Refund).
Next Steps
After generating your services, learn more about:
Last updated