Layered Rails
Design and review Rails applications using layered architecture principles.
Quick Start
Rails applications are organized into four architecture layers with unidirectional data flow:
┌─────────────────────────────────────────┐
│ PRESENTATION LAYER │
│ Controllers, Views, Channels, Jobs* │
│ Forms, Filters, Presenters, Serializers │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ APPLICATION LAYER │
│ Services, Policies, Mailers, Deliveries │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ DOMAIN LAYER │
│ Models, Value Objects, Query Objects │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ INFRASTRUCTURE LAYER │
│ Active Record, API Clients, Storage │
└─────────────────────────────────────────┘
* Jobs are internal inbound entry points: design-wise they follow the same rules as controllers, minus authentication and user-input handling.
Core Rule: Lower layers must never depend on higher layers.
See Architecture Layers Reference for the full layer responsibilities and the Four Rules deep-dive.
What this skill is for
Use this skill when:
- Analyzing a codebase — apply the architecture analysis for a full audit, or zoom in with the service-layer audit, callback analysis, or god-object analysis.
- Reviewing code changes — run the code review workflow on a diff, file, or branch.
- Running the specification test — use the spec-test workflow on a single file or directory to evaluate whether code belongs in its current layer.
- Planning gradual adoption — generate a phased roadmap with the layerification plan workflow, focused on a goal like "introduce authorization" or "decompose god objects."
- Planning a feature — I'll apply the layered principles below to whichever code you're about to write.
- Implementing a specific pattern — authorization, notifications, view components, AI integration, etc. — see the Pattern Catalog and Topic References below.
In Claude Code with this skill installed as a plugin (/plugin install layered-rails@layered-rails-skills), each workflow above is also reachable as a slash command — see Slash Commands. Natural-language requests ("review this file with layered-rails", "run the specification test on app/models/order.rb") work in any environment that has the skill loaded.
Workflows
Reusable procedures bundled inside this skill. Read the file and apply it to the target code:
- Architecture analysis — full layered-architecture audit of a Rails codebase
- Code review — review a diff or file set for layer violations
- Specification test — evaluate whether code belongs in its current layer
- Service-layer audit — deep audit of
app/services/ and service-like classes (per-cluster proposals, contracts, layer hygiene)
- Callback analysis — score Active Record callbacks and find extraction candidates
- God-object analysis — identify oversized models and recommend decomposition
- Gradual layerification plan — incremental roadmap for adopting layered patterns
- ArchSpec setup — generate and verify a tailored
Archspec.rb enforcing the layer boundaries in CI
Core Principles
The Four Rules
- Unidirectional Data Flow - Data flows top-to-bottom only
- No Reverse Dependencies - Lower layers never depend on higher layers
- Abstraction Boundaries - Each abstraction belongs to exactly one layer
- Minimize Connections - Fewer inter-layer connections = looser coupling
Common Violations
| Violation |
Example |
Fix |
| Model uses Current |
Current.user in model |
Pass user as explicit parameter |
| Service accepts request |
param :request in service |
Extract value object from request |
| Controller has business logic |
Pricing calculations in action |
Extract to service or model |
| Anemic models |
All logic in services |
Keep domain logic in models |
| Category |
Reference |
| Layer violations (Current in models, request in services, notifications in models, business logic in controllers) |
layer-violations.md |
| Service objects (anemic models, bag of random objects, premature abstraction) |
service-objects.md |
| Callbacks (operation callbacks, skip callbacks, control flags) |
callbacks.md |
| Concerns (code-slicing, overgrown) |
concerns.md |
| Helpers (HTML construction in helpers) |
helpers.md |
| Jobs (anemic jobs) |
jobs.md |
| Testing (testing wrong layer) |
testing.md |
The Specification Test
If the specification of an object describes features beyond the primary responsibility of its abstraction layer, such features should be extracted into lower layers.
How to apply:
- List responsibilities the code handles
- Evaluate each against the layer's primary concern
- Extract misplaced responsibilities to appropriate layers
See Specification Test Reference for detailed guide.
Pattern Catalog
| Pattern |
Layer |
Use When |
Reference |
| Service Object |
Application |
Orchestrating domain operations |
service-objects.md |
| Query Object |
Domain |
Complex, reusable queries |
query-objects.md |
| Form Object |
Presentation |
Multi-model forms, complex validation |
form-objects.md |
| Filter Object |
Presentation |
Request parameter transformation |
filter-objects.md |
| Presenter |
Presentation |
View-specific logic, multiple models |
presenters.md |
| Serializer |
Presentation |
API response formatting |
serializers.md |
| Policy Object |
Application |
Authorization decisions |
policy-objects.md |
| Value Object |
Domain |
Immutable, identity-less concepts |
value-objects.md |
| Collaborator Object |
Domain |
A slice of one model's behavior in a typed delegate |
collaborator-objects.md |
| State Machine |
Domain |
States, events, transitions |
state-machines.md |
| Concern |
Domain |
Shared behavioral extraction |
concerns.md |
| Repository |
Domain |
Last resort — returning custom domain objects mapped from AR data, after AR scopes (simple) and query objects (query building) are insufficient |
repositories.md |
Pattern Selection Guide
"Where should this code go?"
| If you have... |
Consider... |
| Complex multi-model form |
Form Object |
| Request parameter filtering/transformation |
Filter Object |
| View-specific formatting |
Presenter |
| Complex database query used in multiple places |
Query Object |
| Business operation spanning multiple models |
Service Object (as waiting room) |
| Authorization rules |
Policy Object |
| Multi-channel notifications |
Delivery Object (Active Delivery) |
Remember: Services are a "waiting room" for code until proper abstractions emerge. Don't let app/services become a bag of random objects.
Refactoring Scenarios
Canonical before/after transformations for the most common layerification moves. The layerification plan workflow uses these as reference templates when proposing phases.
| Scenario |
Goal area |
Reference |
| Extract callbacks to service |
callbacks, after_create chains |
callbacks-to-service.md |
| Extract authorization to policy |
authorization, permissions |
authorization-to-policy.md |
| Extract query logic to query object |
complex scopes, reporting queries |
query-to-query-object.md |
| Extract Current from model |
Current.* in domain |
current-from-model.md |
| Decompose god object with associated objects |
god model, large User/Account |
god-object-decomposition.md |
| Replace implicit state machine |
timestamp-based status |
implicit-to-explicit-state-machine.md |
| Extract view logic to presenter |
template logic, formatting |
view-logic-to-presenter.md |
| Form object for complex input |
fat controllers, multi-model forms |
complex-input-to-form-object.md |
Slash Commands
These slash commands are available only when this skill is installed as a Claude Code plugin (/plugin install layered-rails@layered-rails-skills). When the skill is installed via skills.sh or any other path that delivers skills/layered-rails/ without the surrounding plugin, the commands won't be present — invoke the corresponding workflow directly (see Workflows) or just ask in plain language.
| Command |
Workflow |
Purpose |
/layered-rails:review |
review |
Review code changes from a layered architecture perspective |
/layered-rails:spec-test |
spec-test |
Run specification test on specific files |
/layered-rails:analyze |
analyze |
Full codebase abstraction-layer analysis |
/layered-rails:analyze-services |
analyze-services |
Audit app/services/ and service-like classes — conventions, clusters, layer hygiene, test consequences |
/layered-rails:analyze-callbacks |
analyze-callbacks |
Score model callbacks, find extraction candidates |
/layered-rails:analyze-gods |
analyze-gods |
Find god objects via churn × complexity |
/layered-rails:plan [goal] |
plan |
Plan gradual adoption of layered patterns |
/layered-rails:archspec |
archspec |
Generate and verify an Archspec.rb enforcing layer boundaries in CI |
Topic References
For deep dives on specific topics:
| Topic |
Reference |
| Authorization (RBAC, ABAC, policies) |
authorization.md |
| Notifications (multi-channel delivery) |
notifications.md |
| View Components |
view-components.md |
| AI Integration (LLM, agents, RAG, MCP) |
ai-integration.md |
| Configuration |
configuration.md |
| Callbacks (scoring, extraction) |
callbacks.md |
| Current Attributes |
current-attributes.md |
| Instrumentation (logging, metrics) |
instrumentation.md |
Gem References
For library-specific guidance:
| Gem |
Purpose |
Reference |
| action_policy |
Authorization framework |
action-policy.md |
| view_component |
Component framework |
view-component.md |
| anyway_config |
Typed configuration |
anyway-config.md |
| active_delivery |
Multi-channel notifications |
active-delivery.md |
| alba |
JSON serialization |
alba.md |
| workflow |
State machines |
workflow.md |
| rubanok |
Filter/transformation DSL |
rubanok.md |
| active_agent |
AI agent framework |
active-agent.md |
| active_job-performs |
Eliminate anemic jobs |
active-job-performs.md |
| archspec |
Enforce layer boundaries in CI (reference Archspec.rb config) |
archspec.md |
Extraction Signals
When to extract from models:
| Signal |
Metric |
Action |
| God object |
High churn × complexity |
Decompose into concerns, delegates, or separate models |
| Operation callback |
Score 1-2/5 |
Extract to service or event handler |
| Code-slicing concern |
Groups by artifact type |
Convert to behavioral concern or extract |
| Current dependency |
Model reads Current.* |
Pass as explicit parameter |
Callback Scoring:
| Type |
Score |
Keep? |
| Transformer (compute values) |
5/5 |
Yes |
| Normalizer (sanitize input) |
4/5 |
Yes |
| Utility (counter caches) |
4/5 |
Yes |
| Observer (side effects) |
2/5 |
Maybe |
| Operation (business steps) |
1/5 |
Extract |
See Extraction Signals Reference for detailed guide.
Model Organization
Recommended order within model files:
class User < ApplicationRecord
# 1. Gems/DSL extensions
has_secure_password
# 2. Associations
belongs_to :account
has_many :posts
# 3. Enums
enum :status, { pending: 0, active: 1 }
# 4. Normalization
normalizes :email, with: -> { _1.strip.downcase }
# 5. Validations
validates :email, presence: true
# 6. Scopes
scope :active, -> { where(status: :active) }
# 7. Callbacks (transformers only)
before_validation :set_defaults
# 8. Delegations
delegate :name, to: :account, prefix: true
# 9. Public methods
def full_name = "#{first_name} #{last_name}"
# 10. Private methods
private
def set_defaults
self.locale ||= I18n.default_locale
end
end
Success Checklist
Well-layered code:
Guidelines
- Use domain language - Name models after business concepts (Participant, not User; Cloud, not GeneratedImage)
- Patterns before abstractions - Let code age before extracting; premature abstraction is worse than duplication
- Services as waiting room - Don't let
app/services become permanent residence for code
- Explicit over implicit - Prefer explicit parameters over Current attributes
- Extraction thresholds - Consider extraction when methods exceed 15 lines or call external APIs
1---2name: layered-rails3description: Write, refactor, and review Rails code using layered architecture principles from "Layered Design for Ruby on Rails Applications". Use when writing or refactoring Rails code — models, controllers, services, jobs, mailers, policies, forms, query objects, presenters, view components, state machines, serializers, or AI/LLM features — to apply correct patterns and avoid layer violations; and when reviewing Rails code, PRs, or diffs for layer violations, fat controllers/models, anemic models, callback misuse, god objects, or specification-test failures. Triggers on "layered design", "architecture layers", "abstraction layer", "specification test", "layer violation", "fat controller/model", "god object", "anemic model", "extract service/callback/policy/concern", "service object", "form/policy/query/value object", "presenter", "view component", "state machine", "Active Delivery", "callback scoring", "Rails refactor/review", "Rails patterns/best practices", "archspec", "architecture linter/enforcement".4allowed-tools:5 - Grep6 - Glob7 - Read8 - Task9metadata:10 source: https://skillmd.com/skills/palkan/layered-rails11---1213# Layered Rails1415Design and review Rails applications using layered architecture principles.1617## Quick Start1819Rails applications are organized into four architecture layers with **unidirectional data flow**:2021```22┌─────────────────────────────────────────┐23│ PRESENTATION LAYER │24│ Controllers, Views, Channels, Jobs* │25│ Forms, Filters, Presenters, Serializers │26└─────────────────────────────────────────┘27 ↓28┌─────────────────────────────────────────┐29│ APPLICATION LAYER │30│ Services, Policies, Mailers, Deliveries │31└─────────────────────────────────────────┘32 ↓33┌─────────────────────────────────────────┐34│ DOMAIN LAYER │35│ Models, Value Objects, Query Objects │36└─────────────────────────────────────────┘37 ↓38┌─────────────────────────────────────────┐39│ INFRASTRUCTURE LAYER │40│ Active Record, API Clients, Storage │41└─────────────────────────────────────────┘42```4344\* Jobs are **internal inbound** entry points: design-wise they follow the same rules as controllers, minus authentication and user-input handling.4546**Core Rule:** Lower layers must never depend on higher layers.4748See [Architecture Layers Reference](references/core/architecture-layers.md) for the full layer responsibilities and the Four Rules deep-dive.4950## What this skill is for5152Use this skill when:53541. **Analyzing a codebase** — apply the [architecture analysis](workflows/analyze.md) for a full audit, or zoom in with the [service-layer audit](workflows/analyze-services.md), [callback analysis](workflows/analyze-callbacks.md), or [god-object analysis](workflows/analyze-gods.md).552. **Reviewing code changes** — run the [code review workflow](workflows/review.md) on a diff, file, or branch.563. **Running the specification test** — use the [spec-test workflow](workflows/spec-test.md) on a single file or directory to evaluate whether code belongs in its current layer.574. **Planning gradual adoption** — generate a phased roadmap with the [layerification plan workflow](workflows/plan.md), focused on a goal like "introduce authorization" or "decompose god objects."585. **Planning a feature** — I'll apply the layered principles below to whichever code you're about to write.596. **Implementing a specific pattern** — authorization, notifications, view components, AI integration, etc. — see the Pattern Catalog and Topic References below.6061In Claude Code with this skill installed as a plugin (`/plugin install layered-rails@layered-rails-skills`), each workflow above is also reachable as a slash command — see [Slash Commands](#slash-commands). Natural-language requests ("review this file with layered-rails", "run the specification test on `app/models/order.rb`") work in any environment that has the skill loaded.6263## Workflows6465Reusable procedures bundled inside this skill. Read the file and apply it to the target code:6667- [Architecture analysis](workflows/analyze.md) — full layered-architecture audit of a Rails codebase68- [Code review](workflows/review.md) — review a diff or file set for layer violations69- [Specification test](workflows/spec-test.md) — evaluate whether code belongs in its current layer70- [Service-layer audit](workflows/analyze-services.md) — deep audit of `app/services/` and service-like classes (per-cluster proposals, contracts, layer hygiene)71- [Callback analysis](workflows/analyze-callbacks.md) — score Active Record callbacks and find extraction candidates72- [God-object analysis](workflows/analyze-gods.md) — identify oversized models and recommend decomposition73- [Gradual layerification plan](workflows/plan.md) — incremental roadmap for adopting layered patterns74- [ArchSpec setup](workflows/archspec.md) — generate and verify a tailored `Archspec.rb` enforcing the layer boundaries in CI7576## Core Principles7778### The Four Rules79801. **Unidirectional Data Flow** - Data flows top-to-bottom only812. **No Reverse Dependencies** - Lower layers never depend on higher layers823. **Abstraction Boundaries** - Each abstraction belongs to exactly one layer834. **Minimize Connections** - Fewer inter-layer connections = looser coupling8485### Common Violations8687| Violation | Example | Fix |88|-----------|---------|-----|89| Model uses Current | `Current.user` in model | Pass user as explicit parameter |90| Service accepts request | `param :request` in service | Extract value object from request |91| Controller has business logic | Pricing calculations in action | Extract to service or model |92| Anemic models | All logic in services | Keep domain logic in models |9394| Category | Reference |95|----------|-----------|96| Layer violations (Current in models, request in services, notifications in models, business logic in controllers) | [layer-violations.md](references/anti-patterns/layer-violations.md) |97| Service objects (anemic models, bag of random objects, premature abstraction) | [service-objects.md](references/anti-patterns/service-objects.md) |98| Callbacks (operation callbacks, skip callbacks, control flags) | [callbacks.md](references/anti-patterns/callbacks.md) |99| Concerns (code-slicing, overgrown) | [concerns.md](references/anti-patterns/concerns.md) |100| Helpers (HTML construction in helpers) | [helpers.md](references/anti-patterns/helpers.md) |101| Jobs (anemic jobs) | [jobs.md](references/anti-patterns/jobs.md) |102| Testing (testing wrong layer) | [testing.md](references/anti-patterns/testing.md) |103104### The Specification Test105106> If the specification of an object describes features beyond the primary responsibility of its abstraction layer, such features should be extracted into lower layers.107108**How to apply:**1091. List responsibilities the code handles1102. Evaluate each against the layer's primary concern1113. Extract misplaced responsibilities to appropriate layers112113See [Specification Test Reference](references/core/specification-test.md) for detailed guide.114115## Pattern Catalog116117| Pattern | Layer | Use When | Reference |118|---------|-------|----------|-----------|119| Service Object | Application | Orchestrating domain operations | [service-objects.md](references/patterns/service-objects.md) |120| Query Object | Domain | Complex, reusable queries | [query-objects.md](references/patterns/query-objects.md) |121| Form Object | Presentation | Multi-model forms, complex validation | [form-objects.md](references/patterns/form-objects.md) |122| Filter Object | Presentation | Request parameter transformation | [filter-objects.md](references/patterns/filter-objects.md) |123| Presenter | Presentation | View-specific logic, multiple models | [presenters.md](references/patterns/presenters.md) |124| Serializer | Presentation | API response formatting | [serializers.md](references/patterns/serializers.md) |125| Policy Object | Application | Authorization decisions | [policy-objects.md](references/patterns/policy-objects.md) |126| Value Object | Domain | Immutable, identity-less concepts | [value-objects.md](references/patterns/value-objects.md) |127| Collaborator Object | Domain | A slice of one model's behavior in a typed delegate | [collaborator-objects.md](references/patterns/collaborator-objects.md) |128| State Machine | Domain | States, events, transitions | [state-machines.md](references/patterns/state-machines.md) |129| Concern | Domain | Shared behavioral extraction | [concerns.md](references/patterns/concerns.md) |130| Repository | Domain | **Last resort** — returning custom domain objects mapped from AR data, after AR scopes (simple) and query objects (query building) are insufficient | [repositories.md](references/patterns/repositories.md) |131132### Pattern Selection Guide133134**"Where should this code go?"**135136| If you have... | Consider... |137|----------------|-------------|138| Complex multi-model form | Form Object |139| Request parameter filtering/transformation | Filter Object |140| View-specific formatting | Presenter |141| Complex database query used in multiple places | Query Object |142| Business operation spanning multiple models | Service Object (as waiting room) |143| Authorization rules | Policy Object |144| Multi-channel notifications | Delivery Object (Active Delivery) |145146**Remember:** Services are a "waiting room" for code until proper abstractions emerge. Don't let `app/services` become a bag of random objects.147148## Refactoring Scenarios149150Canonical before/after transformations for the most common layerification moves. The [layerification plan workflow](workflows/plan.md) uses these as reference templates when proposing phases.151152| Scenario | Goal area | Reference |153|----------|-----------|-----------|154| Extract callbacks to service | callbacks, after_create chains | [callbacks-to-service.md](examples/callbacks-to-service.md) |155| Extract authorization to policy | authorization, permissions | [authorization-to-policy.md](examples/authorization-to-policy.md) |156| Extract query logic to query object | complex scopes, reporting queries | [query-to-query-object.md](examples/query-to-query-object.md) |157| Extract Current from model | Current.* in domain | [current-from-model.md](examples/current-from-model.md) |158| Decompose god object with associated objects | god model, large User/Account | [god-object-decomposition.md](examples/god-object-decomposition.md) |159| Replace implicit state machine | timestamp-based status | [implicit-to-explicit-state-machine.md](examples/implicit-to-explicit-state-machine.md) |160| Extract view logic to presenter | template logic, formatting | [view-logic-to-presenter.md](examples/view-logic-to-presenter.md) |161| Form object for complex input | fat controllers, multi-model forms | [complex-input-to-form-object.md](examples/complex-input-to-form-object.md) |162163## Slash Commands164165These slash commands are available **only when this skill is installed as a Claude Code plugin** (`/plugin install layered-rails@layered-rails-skills`). When the skill is installed via [skills.sh](https://skills.sh/) or any other path that delivers `skills/layered-rails/` without the surrounding plugin, the commands won't be present — invoke the corresponding workflow directly (see [Workflows](#workflows)) or just ask in plain language.166167| Command | Workflow | Purpose |168|---------|----------|---------|169| `/layered-rails:review` | [review](workflows/review.md) | Review code changes from a layered architecture perspective |170| `/layered-rails:spec-test` | [spec-test](workflows/spec-test.md) | Run specification test on specific files |171| `/layered-rails:analyze` | [analyze](workflows/analyze.md) | Full codebase abstraction-layer analysis |172| `/layered-rails:analyze-services` | [analyze-services](workflows/analyze-services.md) | Audit `app/services/` and service-like classes — conventions, clusters, layer hygiene, test consequences |173| `/layered-rails:analyze-callbacks` | [analyze-callbacks](workflows/analyze-callbacks.md) | Score model callbacks, find extraction candidates |174| `/layered-rails:analyze-gods` | [analyze-gods](workflows/analyze-gods.md) | Find god objects via churn × complexity |175| `/layered-rails:plan [goal]` | [plan](workflows/plan.md) | Plan gradual adoption of layered patterns |176| `/layered-rails:archspec` | [archspec](workflows/archspec.md) | Generate and verify an `Archspec.rb` enforcing layer boundaries in CI |177178## Topic References179180For deep dives on specific topics:181182| Topic | Reference |183|-------|-----------|184| Authorization (RBAC, ABAC, policies) | [authorization.md](references/topics/authorization.md) |185| Notifications (multi-channel delivery) | [notifications.md](references/topics/notifications.md) |186| View Components | [view-components.md](references/topics/view-components.md) |187| AI Integration (LLM, agents, RAG, MCP) | [ai-integration.md](references/topics/ai-integration.md) |188| Configuration | [configuration.md](references/topics/configuration.md) |189| Callbacks (scoring, extraction) | [callbacks.md](references/topics/callbacks.md) |190| Current Attributes | [current-attributes.md](references/topics/current-attributes.md) |191| Instrumentation (logging, metrics) | [instrumentation.md](references/topics/instrumentation.md) |192193## Gem References194195For library-specific guidance:196197| Gem | Purpose | Reference |198|-----|---------|-----------|199| action_policy | Authorization framework | [action-policy.md](references/gems/action-policy.md) |200| view_component | Component framework | [view-component.md](references/gems/view-component.md) |201| anyway_config | Typed configuration | [anyway-config.md](references/gems/anyway-config.md) |202| active_delivery | Multi-channel notifications | [active-delivery.md](references/gems/active-delivery.md) |203| alba | JSON serialization | [alba.md](references/gems/alba.md) |204| workflow | State machines | [workflow.md](references/gems/workflow.md) |205| rubanok | Filter/transformation DSL | [rubanok.md](references/gems/rubanok.md) |206| active_agent | AI agent framework | [active-agent.md](references/gems/active-agent.md) |207| active_job-performs | Eliminate anemic jobs | [active-job-performs.md](references/gems/active-job-performs.md) |208| archspec | Enforce layer boundaries in CI (reference `Archspec.rb` config) | [archspec.md](references/gems/archspec.md) |209210## Extraction Signals211212**When to extract from models:**213214| Signal | Metric | Action |215|--------|--------|--------|216| God object | High churn × complexity | Decompose into concerns, delegates, or separate models |217| Operation callback | Score 1-2/5 | Extract to service or event handler |218| Code-slicing concern | Groups by artifact type | Convert to behavioral concern or extract |219| Current dependency | Model reads Current.* | Pass as explicit parameter |220221**Callback Scoring:**222| Type | Score | Keep? |223|------|-------|-------|224| Transformer (compute values) | 5/5 | Yes |225| Normalizer (sanitize input) | 4/5 | Yes |226| Utility (counter caches) | 4/5 | Yes |227| Observer (side effects) | 2/5 | Maybe |228| Operation (business steps) | 1/5 | Extract |229230See [Extraction Signals Reference](references/core/extraction-signals.md) for detailed guide.231232## Model Organization233234Recommended order within model files:235236```ruby237class User < ApplicationRecord238 # 1. Gems/DSL extensions239 has_secure_password240241 # 2. Associations242 belongs_to :account243 has_many :posts244245 # 3. Enums246 enum :status, { pending: 0, active: 1 }247248 # 4. Normalization249 normalizes :email, with: -> { _1.strip.downcase }250251 # 5. Validations252 validates :email, presence: true253254 # 6. Scopes255 scope :active, -> { where(status: :active) }256257 # 7. Callbacks (transformers only)258 before_validation :set_defaults259260 # 8. Delegations261 delegate :name, to: :account, prefix: true262263 # 9. Public methods264 def full_name = "#{first_name} #{last_name}"265266 # 10. Private methods267 private268269 def set_defaults270 self.locale ||= I18n.default_locale271 end272end273```274275## Success Checklist276277Well-layered code:278279- [ ] No reverse dependencies (lower layers don't depend on higher)280- [ ] Models don't access Current attributes281- [ ] Services don't accept request objects282- [ ] Controllers are thin (HTTP concerns only)283- [ ] Domain logic lives in models, not services284- [ ] Callbacks score 4+ or are extracted285- [ ] Concerns are behavioral, not code-slicing286- [ ] Abstractions don't span multiple layers287- [ ] Tests verify appropriate layer responsibilities288289## Guidelines290291- **Use domain language** - Name models after business concepts (Participant, not User; Cloud, not GeneratedImage)292- **Patterns before abstractions** - Let code age before extracting; premature abstraction is worse than duplication293- **Services as waiting room** - Don't let `app/services` become permanent residence for code294- **Explicit over implicit** - Prefer explicit parameters over Current attributes295- **Extraction thresholds** - Consider extraction when methods exceed 15 lines or call external APIs