Skip to content
Skills Library
Programming · Building & coding

concern-patterns

/concern-patterns

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).

Licence
MIT
Last repository change
01.06.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
2
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 01.06.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

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).

Original author description

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).

How it works

  1. Project knowledge
  2. Core principles
  3. When to extract a concern
  4. Model concern structure
  5. Controller concern structure
  6. Naming conventions
  7. Testing concerns
  8. Refactoring workflow
  9. Files to create
  10. Boundaries
Original SKILL.md headings · 257 lines, 734 words
SKILL.md preview
---
name: concern-patterns
description: >-
  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).
license: MIT
compatibility: Ruby 3.3+, Rails 8.0+
---

# 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:**
```bash
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**
   ```ruby
   # Multiple models have:
   has_many :comments, as: :commentable
   # Extract to: app/models/concerns/commentable.rb
   ```

2. **Repeated state patterns**
   ```ruby
   # Multiple models have close/reopen pattern
   # Extract to: Card::Closeable, Board::Publishable, etc.
   ```

3. **Repeated scopes**
   ```ruby
   # Multiple models have:
   scope :recent, -> { order(created_at: :desc) }
   # Extract to: Timestampable concern
   ```

4. **Repeated controller patterns**
   ```ruby
   # 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

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

  included do
    has_one :closure, dependent: :destroy

    scope :open, -> { where.missing(:closure) }
    scope :closed, -> { joins(:closure) }
  end

  def close(user: Current.user)
    create_closure!(user: user)
    track_event "card_closed", user: user
  end

  def reopen
    closure&.destroy!
    track_event "card_reopened"
  end

  def closed?
    closure.present?
  end

  def open?
    !closed?
  end

  def closed_at
    closure&.created_at
  end

  def closed_by
    closure&.user
  end
end
```

### Association concern

```ruby
# app/models/card/assignable.rb
module Card::Assignable
  extend ActiveSupport::Concern

  included do
    has_many :assignments, dependent: :destroy
    has_many :assignees, through: :assignments, source: :assignee

    scope :assigned_to, ->(users) { joins(:assignments).where(assignments: { assignee: users }).distinct }
    scope :unassigned, -> { where.missing(:assignments) }
  end

  def assign(user)
    assignments.create!(user: user) unless assigned_to?(user)
    track_event "card_assigned", user: user, particulars: { assignee_id: user.id }
  end

  def unassign(user)
    assignments.where(user: user).destroy_all
  end

  def assigned_to?(user)
    assignees.include?(user)
  end
end
```

### Behavior concern with class methods

```ruby
# app/models/card/searchable.rb
module Card::Searchable
  extend ActiveSupport::Concern

  included do
    scope :search, ->(query) { where("title LIKE ? OR body LIKE ?", "%#{query}%", "%#{query}%") }
  end

  class_methods do
    def search_with_ranking(query)
      search(query).order("search_rank DESC")
    end

    def top_results(query, limit: 10)
      search_with_ranking(query).limit(limit)
    end
  end
end
```

## Controller concern structure

```ruby
# app/controllers/concerns/card_scoped.rb
module CardScoped
  extend ActiveSupport::Concern

  included do
    before_action :set_card
    before_action :set_board
  end

  private

  def set_card
    @card = Current.account.cards.find(params[:card_id])
  end

  def set_board
    @board = @card.board
  end

  def render_card_replacement
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          dom_id(@card, :card_container),
          partial: "cards/container",
          locals: { card: @card.reload }
        )
      end
      format.html { redirect_to @card }
    end
  end
end
```

## Naming conventions

- **Model concerns** (adjectives): `Closeable`, `Publishable`, `Watchable`, `Assignable`, `Searchable`, `Eventable`, `Broadcastable`, `Readable`, `Positionable`
- **Controller concerns** (nouns): `CardScoped`, `BoardScoped`, `FilterScoped`, `CurrentRequest`, `CurrentTimezone`, `Authentication`

## Testing concerns

### Test in isolation:

```ruby
# test/models/concerns/closeable_test.rb
class CloseableTest < ActiveSupport::TestCase
  setup do
    @card = cards(:logo)
  end

  test "close creates closure record" do
    assert_difference -> { Closure.count }, 1 do
      @card.close
    end
    assert @card.closed?
  end

  test "reopen destroys closure record" do
    @card.close
    assert_difference -> { Closure.count }, -1 do
      @card.reopen
    end
    assert @card.open?
  end

  test "closed scope finds closed records" do
    @card.close
    assert_includes Card.closed, @card
    refute_includes Card.open, @card
  end
end
```

## Refactoring workflow

1. **Identify the pattern** -- Find duplicated code across models/controllers
2. **Name the concern** -- Use an adjective describing the capability
3. **Create the file** -- `app/models/[model]/[concern].rb` or `app/controllers/concerns/[concern].rb`
4. **Move code** -- Associations, validations, scopes, methods
5. **Include it** -- Add `include ConcernName` to models/controllers
6. **Write tests** -- Test concern in isolation and in context
7. **Remove duplication** -- Delete the old code from models/controllers

## Files to create

1. **Concern file:** `app/models/card/closeable.rb` or `app/controllers/concerns/card_scoped.rb`
2. **Model/Controller:** Add `include ConcernName`
3. **Test file:** `test/models/concerns/closeable_test.rb`

See `references/concern-catalog.md` for the full catalog of concern types.

## Boundaries

- **Always:** Extract repeated code into concerns, keep concerns focused on one aspect, include all related code (associations, scopes, methods), write tests, use `extend ActiveSupport::Concern`, namespace model concerns under the model
- **Ask first:** Before creating concerns that span multiple domains, before extracting concerns with complex dependencies, before modifying widely-used concerns
- **Never:** Create god concerns with too many responsibilities, use concerns to hide service objects, skip `included do` block for callbacks/associations, create concerns for one-off code
Files in this package (2)
  • SKILL.md 7 KB
  • references/concern-catalog.md 5 KB

Versions

The database has no other skills named concern-patterns.

Database snapshot: 13.11.2026 · package version d713a0109e36 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100rails-concern
    Creates Rails concerns for shared behavior across models or controllers with TDD. Use when extracting shared code, creating reusable modules, DRYing up models/controllers, or when user mentions concerns, modules, mixins, or shared behavior. WHEN NOT: Logic used by only one model…
  • 100model-patterns
    Builds rich domain models with business logic, concerns, and proper associations following the fat-models-over-service-objects philosophy. Use when creating models, adding validations, scopes, callbacks, business logic methods, or associations. WHEN NOT: Controller/routing work…
  • 100rails
    This skill should be used when writing or reviewing Ruby on Rails code, or when the project uses "Rails", "ActiveRecord", "ActiveJob", "concerns", "migrations", "Rails.configuration", "request specs", or "controller". Provides Rails coding conventions, patterns, and best…
  • 100jr-rails-classic
    Write Rails code in 37signals/classic style: rich models, CRUD controllers, concerns, state-as-records, Minitest. Use when writing or modifying Ruby on Rails application code.
  • 81rails-ai:controllers
    Use when building Rails controllers - RESTful actions, nested resources, skinny controllers, concerns, strong parameters
  • 100rails-dev
    Opinionated Rails conventions: rich models, concerns, CRUD-everything, state-as-records, minimal dependencies, Minitest with fixtures. Load this skill BEFORE any code-level thinking, not only before editing a file. It is required the moment a task touches Rails code in ANY way:…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.