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:install

What It Creates

The install generator creates the following files:

  1. app/services/application_service.rb - Base service class for your application

    class 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
    end
  2. config/initializers/operandi.rb - Configuration file (unless --skip-initializer is used) This file contains the global configuration for Operandi in your Rails application.

  3. spec/services/application_service_spec.rb - RSpec test file (if RSpec is detected and --skip-spec is 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:

  1. Service file - app/services/{name}.rb

  2. Spec file - spec/services/{name}_spec.rb (if RSpec is detected and --skip-spec is 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

  1. Run the install generator first - Always run operandi:install before creating individual services to set up the base ApplicationService class.

  2. Use namespaces - Organize related services under namespaces (e.g., User::Create, Payment::Process) to keep your services organized.

  3. Start with structure - Use --args, --steps, and --outputs options to create a skeleton for your service, then fill in the implementation.

  4. 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.

  5. 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:

  • Arguments - Define and validate service inputs

  • Steps - Organize service logic into steps

  • Outputs - Define service outputs

  • Testing - Write comprehensive tests for your services

Last updated