agentleFS
ThibautBaissacThibautBaissac/rails_ai_agents/.claude/skills/extraction-timing/SKILL.md

extraction-timing

Skill665 starsChanged 6 months agoMITView on GitHub

Guides decisions about when and how to extract code into services, queries, concerns, form objects, or other patterns. Use when deciding whether to extract code, choosing between patterns (service vs concern vs query), evaluating if a base class or abstraction is needed, or when user mentions refactoring, extraction, code organization, or "where should this go." WHEN NOT: Implementing a specific pattern already decided on (use specialist agents like service-agent, query-agent, or model-agent), writing tests (use rspec-agent), or architecture-level design (use rails-architecture).

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Rule: If the behavior is a *property* of the model, use a concern.

    line 78
  2. Distinctive:

    Before extracting, verify you're not creating:

    line 101

≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

You are an expert in Rails code organization and extraction decisions. Help decide when to extract, what pattern to use, and when to keep it simple.

Core Philosophy: Skinny Everything

The 2025 Rails consensus has evolved beyond "Fat Models" to Skinny Everything:

  • Controllers: orchestrate (delegate to services, render responses) -- max ~10 lines per action
  • Models: persist (validations, associations, scopes, simple predicates) -- max ~100 lines
  • Services: contain business logic (multi-step operations, external calls, orchestration)
  • Views: display markup with zero logic

Extraction Thresholds

SignalAction
Controller action exceeds ~10 lines of business logicExtract to service object
Model exceeds ~100 linesExtract business logic to services, complex queries to query objects
Query joins multiple tables or has conditional clausesExtract to query object
Form touches multiple models or has custom validationExtract to form object
Display formatting logic in modelExtract to presenter
UI element reused across 2+ viewsExtract to ViewComponent
Shared behavior across 2+ models (narrow, simple)Extract to concern
5+ concrete implementations with identical structureExtract base class
One-off operationDon't extract. Inline is fine.

Decision Tree: "Where Should This Code Go?"

Is it a database query?
  ├── Simple (one table, one condition) → Model scope
  └── Complex (joins, conditionals, reused) → Query object

Is it business logic?
  ├── Simple CRUD on one model → Controller inline (or model method)
  ├── Multi-step operation → Service object
  ├── Involves external API → Service object
  └── Spans multiple models → Service object with transaction

Is it shared behavior?
  ├── Property of the model (soft-delete, slugs, search) → Concern
  └── Operation on the model (checkout, import, sync) → Service object

Is it display logic?
  ├── Formatting one model's data → Presenter (SimpleDelegator)
  ├── Reusable UI element → ViewComponent
  └── Simple helper method → Keep in helper (use sparingly)

Is it validation?
  ├── Single model, standard rules → Model validation
  ├── Multi-model form → Form object
  └── Business rule (not data integrity) → Service validation

Settled Debates

Concerns vs Service Objects

ConcernsService Objects
Use forSimple shared model propertiesMulti-step business operations
ExamplesSoftDeletable, Searchable, SluggableCreateOrder, ProcessRefund, ImportCsv
Max size~30 linesNo hard limit (but SRP applies)
Test viaIncluding model's specsIsolated unit specs

Rule: If the behavior is a property of the model, use a concern. If it's an operation on the model, use a service.

STI vs Polymorphic Associations

STIPolymorphic
Use whenSubclasses share >80% of columnsTypes have unique attributes
TableOne shared table with type columnSeparate tables per type
Avoid when>20% columns are NULL for some subtypesTypes are fundamentally similar

Callbacks vs Explicit Calls

Rule: Callbacks for data normalization only. Everything else is explicit.

Acceptable CallbacksMust Be Explicit (in services)
before_validation :strip_whitespaceSending emails
before_save :downcase_emailEnqueuing background jobs
before_destroy :check_dependenciesCalling external APIs
after_initialize :set_defaultsCreating related records with business logic

Anti-Pattern Checklist

Before extracting, verify you're not creating:

  • A service that wraps a single model.update! call (Service Graveyard)
  • A base class for only 2 services (Premature Abstraction)
  • A concern with multiple responsibilities (Kitchen Sink Concern)
  • A helper that should be a presenter or component
  • An abstraction for a hypothetical future need (YAGNI violation)

Reference

See @docs/rails-development-principles.md for the complete development principles guide including SOLID, testing strategy, security, and performance.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.