CtrlK
BlogDocsLog inGet started
Tessl Logo

igmarin/rails-agent-skills

Curated library of AI agent skills for Ruby on Rails development. Covers code review, architecture, security, testing (RSpec), engines, service objects, DDD patterns, and workflow automation.

95

2.21x
Quality

97%

Does it follow best practices?

Impact

91%

2.21x

Average score across 3 eval scenarios

SecuritybySnyk

Passed

No known issues

Overview
Quality
Evals
Security
Files

SKILL.mdruby-service-objects/

name:
ruby-service-objects
description:
Use when creating or refactoring Ruby service classes in Rails. Covers the .call pattern, module namespacing, YARD documentation, standardized responses, orchestrator delegation, transaction wrapping, and error handling conventions.

Ruby Service Objects

HARD-GATE: Tests Gate Implementation

EVERY service object MUST have its test written and validated BEFORE implementation.
  1. Write the spec for .call (with contexts for success, error, edge cases)
  2. Run the spec — verify it fails because the service does not exist yet
  3. ONLY THEN write the service implementation
See rspec-best-practices for the full gate cycle.

Quick Reference

ConventionRule
Entry point.call class method delegating to new.call
Response format{ success: true/false, response: { ... } }
File locationapp/services/module_name/service_name.rb
Pragmafrozen_string_literal: true in every file
DocsYARD on every public method (see yard-documentation)
ValidationRaise early on invalid input
ErrorsRescue, log, return error hash — don't leak exceptions
TransactionsWrap multi-step DB operations

Core Patterns

1. The .call Pattern

module AnimalTransfers
  class TransferService
    attr_reader :source_shelter_id, :target_shelter_id

    def self.call(params)
      new(params).call
    end

    def initialize(params)
      @source_shelter_id = params.dig(:source_shelter, :shelter_id)
      @target_shelter_id = params.dig(:target_shelter, :shelter_id)
    end

    def call
      validate_shelters!
      result = process_data
      build_success_response(result)
    rescue ActiveRecord::RecordInvalid => e
      log_error('Validation Error', e)
      build_error_response(e.message, [])
    rescue StandardError => e
      log_error('Processing Error', e, include_backtrace: true)
      build_error_response(e.message, [])
    end
  end
end

2. Standardized Response Format

# Success
{ success: true, response: { successful_items: [...] } }

# Error
{ success: false, response: { error: { message: '...', failed_items: [...] } } }

# Partial success
{
  success: true,
  response: {
    successful_transfers: ['TAG001'],
    error: { message: 'Some animals were not found...', failed_transfers: ['TAG002'] }
  }
}

3. Class-only Services (Static Methods)

When no instance state is needed:

class ShelterValidator
  def self.validate_source_shelter!(shelter_id)
    shelter = Shelter.find_by(id: shelter_id)
    raise ArgumentError, 'Source shelter not found' unless shelter
    shelter
  end
end

Additional Patterns

Constants for configuration

MISSING_CONFIGURATION_ERROR = 'Missing required configuration'
DEFAULT_TIMEOUT = 30

SQL sanitization

def self.find(tag_number:)
  query = ActiveRecord::Base.sanitize_sql(['SELECT * FROM table WHERE tag_number = ?;', tag_number])
  fetcher.execute_query(query)
end

Checklist for New Service Objects

  • Module namespace matches directory structure
  • Constants defined for error messages and defaults
  • Graceful handling for non-critical failures
  • SQL sanitization for any dynamic queries
  • README.md documenting the module

Pitfalls

ProblemCorrect approach
Returning raw exceptions instead of error hashCallers should get { success: false, ... }, not unhandled exceptions
Skipping input validationBad input causes cryptic errors deep in the call chain
Transaction wrapping everythingOnly wrap multi-step DB operations that must be atomic
.call method longer than 20 linesExtract to sub-services — orchestrator should coordinate, not implement
Service renders HTTP responsesThat's the controller's job — service returns data only
Service modifies unrelated modelsUnclear boundary — extract a new service with a single responsibility
Duplicated validation across servicesExtract to a shared validator object

Integration

SkillWhen to chain
yard-documentationWhen writing or reviewing inline docs for classes and public methods
ruby-api-client-integrationFor external API integrations (Auth, Client, Fetcher, Builder layers)
strategy-factory-null-calculatorFor variant-based calculators (Factory + Strategy + Null Object)
rspec-service-testingFor testing service objects
rspec-best-practicesFor general RSpec structure
rails-architecture-reviewWhen service extraction is part of an architecture review

ruby-service-objects

README.md

tile.json