Skills

Concern Patterns

Creates and refactors model and controller concerns for shared behavior.

Install

npx skills add ThibautBaissac/rails_ai_agents --skill concern-patterns -g -a claude-code -y
Category
Engineering
Updated
5mo ago
License
MIT
Context cost
Mediumabout 1,682 tokens
Runs scripts
No, instructions only

What it does

Creates and refactors model and controller concerns for shared behavior. Use when extracting shared code, organizing models with horizontal concerns, DRYing up controllers, or when user mentions concerns, mixins, or modules. WHEN NOT: Logic used by only one model (keep in place), service object extraction (use model-patterns), or job organization (use job-patterns).

Concern Patterns (37signals)

Concerns for horizontal behavior, inheritance for vertical specialization.

Project knowledge

Tech Stack: Rails 8.2 (edge), ActiveSupport::Concern Location: app/models/[model]/ for model concerns, app/controllers/concerns/ for controller concerns

Commands:

ls app/models/concerns/                      # List shared concerns
ls app/models/card/                          # List Card concerns
bin/rails runner "puts Card.included_modules" # Check usage
bin/rails test test/models/                  # Run model tests

Core principles

Each concern should be:

  • Self-contained: All related code (associations, validations, scopes, methods) in one place
  • Cohesive: Focused on one aspect (e.g., Closeable, Watchable, Searchable)
  • Composable: Models include multiple concerns to build up behavior

When to extract a concern

Extract when you see:

  1. Repeated associations across models

    # Multiple models have:
    has_many :comments, as: :commentable
    # Extract to: app/models/concerns/commentable.rb
    
  2. Repeated state patterns

    # Multiple models have close/reopen pattern
    # Extract to: Card::Closeable, Board::Publishable, etc.
    
  3. Repeated scopes

    # Multiple models have:
    scope :recent, -> { order(created_at: :desc) }
    # Extract to: Timestampable concern
    
  4. Repeated controller patterns

    # Multiple controllers load parent resource
    # Extract to: ParentScoped concern
    

Do NOT extract when:

  • Code is used by only one model (YAGNI)
  • You'd create a god concern with unrelated methods
  • Logic should be in explicit model methods instead

Model concern structure

State management concern

# app/models/card/closeable.rb
module Card::Closeable
  extend ActiveSupport::Concern

  included do
    has_one :closure, dependent: :destroy
Read the whole SKILL.md on GitHub

More in Engineering