AgentSkillsCN

layered-rails

依据《面向 Ruby on Rails 应用程序的分层设计》中的分层架构原则,设计并评审 Rails 应用程序。每当您分析 Rails 代码库、审查 PR 中是否存在架构违规、规划功能实现,或实施授权、视图组件、AI 集成等模式时,均可使用此技能。可通过“分层设计”、“架构层级”、“抽象”、“规格测试”、“层级违规”、“提取服务”、“胖控制器”、“上帝对象”等短语触发。

SKILL.md
--- frontmatter
name: layered-rails
description: Design and review Rails applications using layered architecture principles from "Layered Design for Ruby on Rails Applications". Use when analyzing Rails codebases, reviewing PRs for architecture violations, planning feature implementations, or implementing patterns like authorization, view components, or AI integration. Triggers on "layered design", "architecture layers", "abstraction", "specification test", "layer violation", "extract service", "fat controller", "god object".
allowed-tools:
  - Grep
  - Glob
  - Read
  - Task

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:

code
┌─────────────────────────────────────────┐
│           PRESENTATION LAYER            │
│  Controllers, Views, Channels, Mailers  │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│           APPLICATION LAYER             │
│   Service Objects, Form Objects, etc.   │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│             DOMAIN LAYER                │
│  Models, Value Objects, Domain Events   │
└─────────────────────────────────────────┘
                    ↓
┌─────────────────────────────────────────┐
│          INFRASTRUCTURE LAYER           │
│  Active Record, APIs, File Storage      │
└─────────────────────────────────────────┘

Core Rule: Lower layers must never depend on higher layers.

What Would You Like To Do?

  1. Analyze codebase - Run /layers:analyze for full analysis or /layers:analyze:callbacks, /layers:analyze:gods for specific checks
  2. Review code changes - Run /layers:review for layered architecture review
  3. Run specification test - Run /layers:spec-test on specific files
  4. Plan gradual adoption - Run /layers:gradual [goal] to plan incremental layerification
  5. Plan feature implementation - I'll guide you using layered principles
  6. Implement specific pattern - I'll help with authorization, notifications, view components, AI integration, etc.

Core Principles

The Four Rules

  1. Unidirectional Data Flow - Data flows top-to-bottom only
  2. No Reverse Dependencies - Lower layers never depend on higher layers
  3. Abstraction Boundaries - Each abstraction belongs to exactly one layer
  4. Minimize Connections - Fewer inter-layer connections = looser coupling

Common Violations

ViolationExampleFix
Model uses CurrentCurrent.user in modelPass user as explicit parameter
Service accepts requestparam :request in serviceExtract value object from request
Controller has business logicPricing calculations in actionExtract to service or model
Anemic modelsAll logic in servicesKeep domain logic in models

See Anti-Patterns Reference for complete list.

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:

  1. List responsibilities the code handles
  2. Evaluate each against the layer's primary concern
  3. Extract misplaced responsibilities to appropriate layers

See Specification Test Reference for detailed guide.

Pattern Catalog

PatternLayerUse WhenReference
Service ObjectApplicationOrchestrating domain operationsservice-objects.md
Query ObjectDomainComplex, reusable queriesquery-objects.md
Form ObjectPresentationMulti-model forms, complex validationform-objects.md
Filter ObjectPresentationRequest parameter transformationfilter-objects.md
PresenterPresentationView-specific logic, multiple modelspresenters.md
SerializerPresentationAPI response formattingserializers.md
Policy ObjectApplicationAuthorization decisionspolicy-objects.md
Value ObjectDomainImmutable, identity-less conceptsvalue-objects.md
State MachineDomainStates, events, transitionsstate-machines.md
ConcernDomainShared behavioral extractionconcerns.md

Pattern Selection Guide

"Where should this code go?"

If you have...Consider...
Complex multi-model formForm Object
Request parameter filtering/transformationFilter Object
View-specific formattingPresenter
Complex database query used in multiple placesQuery Object
Business operation spanning multiple modelsService Object (as waiting room)
Authorization rulesPolicy Object
Multi-channel notificationsDelivery 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.

Commands Reference

CommandPurpose
/layers:reviewReview code changes from layered architecture perspective
/layers:spec-testRun specification test on specific files
/layers:analyzeFull codebase abstraction layer analysis
/layers:analyze:callbacksScore model callbacks, find extraction candidates
/layers:analyze:godsFind God objects via churn × complexity
/layers:gradual [goal]Plan gradual adoption of layered patterns

Topic References

For deep dives on specific topics:

TopicReference
Authorization (RBAC, ABAC, policies)authorization.md
Notifications (multi-channel delivery)notifications.md
View Componentsview-components.md
AI Integration (LLM, agents, RAG, MCP)ai-integration.md
Configurationconfiguration.md
Callbacks (scoring, extraction)callbacks.md
Current Attributescurrent-attributes.md
Instrumentation (logging, metrics)instrumentation.md

Gem References

For library-specific guidance:

GemPurposeReference
action_policyAuthorization frameworkaction-policy.md
view_componentComponent frameworkview-component.md
anyway_configTyped configurationanyway-config.md
active_deliveryMulti-channel notificationsactive-delivery.md
albaJSON serializationalba.md
workflowState machinesworkflow.md
rubanokFilter/transformation DSLrubanok.md
active_agentAI agent frameworkactive-agent.md
active_job-performsEliminate anemic jobsactive-job-performs.md

Extraction Signals

When to extract from models:

SignalMetricAction
God objectHigh churn × complexityDecompose into concerns, delegates, or separate models
Operation callbackScore 1-2/5Extract to service or event handler
Code-slicing concernGroups by artifact typeConvert to behavioral concern or extract
Current dependencyModel reads Current.*Pass as explicit parameter

Callback Scoring:

TypeScoreKeep?
Transformer (compute values)5/5Yes
Normalizer (sanitize input)4/5Yes
Utility (counter caches)4/5Yes
Observer (side effects)2/5Maybe
Operation (business steps)1/5Extract

See Extraction Signals Reference for detailed guide.

Model Organization

Recommended order within model files:

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

  • No reverse dependencies (lower layers don't depend on higher)
  • Models don't access Current attributes
  • Services don't accept request objects
  • Controllers are thin (HTTP concerns only)
  • Domain logic lives in models, not services
  • Callbacks score 4+ or are extracted
  • Concerns are behavioral, not code-slicing
  • Abstractions don't span multiple layers
  • Tests verify appropriate layer responsibilities

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