$npx -y skills add ThibautBaissac/rails_ai_agents --skill rails-concernCreates 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
| 1 | # Rails Concern Generator (TDD) |
| 2 | |
| 3 | Creates concerns (ActiveSupport::Concern modules) for shared behavior with specs first. |
| 4 | |
| 5 | ## Quick Start |
| 6 | |
| 7 | 1. Write failing spec testing the concern behavior |
| 8 | 2. Run spec to confirm RED |
| 9 | 3. Implement concern in `app/models/concerns/` or `app/controllers/concerns/` |
| 10 | 4. Run spec to confirm GREEN |
| 11 | |
| 12 | ## When to Use Concerns |
| 13 | |
| 14 | **Good use cases:** |
| 15 | - Shared validations across multiple models |
| 16 | - Common scopes used by several models |
| 17 | - Shared callbacks (e.g., UUID generation) |
| 18 | - Controller authentication/authorization helpers |
| 19 | - Pagination or filtering logic |
| 20 | |
| 21 | **Avoid concerns when:** |
| 22 | - Logic is only used in one place (YAGNI) |
| 23 | - Creating "god" concerns with unrelated methods |
| 24 | - Logic should be a service object instead |
| 25 | |
| 26 | ## TDD Workflow |
| 27 | |
| 28 | ### Step 1: Create Concern Spec (RED) |
| 29 | |
| 30 | For **Model Concerns**, test via a model that includes it: |
| 31 | |
| 32 | ```ruby |
| 33 | # spec/models/concerns/[concern_name]_spec.rb |
| 34 | RSpec.describe [ConcernName] do |
| 35 | # Create a test class that includes the concern |
| 36 | let(:test_class) do |
| 37 | Class.new(ApplicationRecord) do |
| 38 | self.table_name = "events" # Use existing table |
| 39 | include [ConcernName] |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | let(:instance) { test_class.new } |
| 44 | |
| 45 | describe "included behavior" do |
| 46 | it "adds the expected methods" do |
| 47 | expect(instance).to respond_to(:method_from_concern) |
| 48 | end |
| 49 | end |
| 50 | |
| 51 | describe "#method_from_concern" do |
| 52 | it "behaves as expected" do |
| 53 | expect(instance.method_from_concern).to eq(expected_value) |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | describe "class methods" do |
| 58 | it "adds scope" do |
| 59 | expect(test_class).to respond_to(:scope_name) |
| 60 | end |
| 61 | end |
| 62 | end |
| 63 | ``` |
| 64 | |
| 65 | Alternative: Test through an actual model that uses the concern: |
| 66 | |
| 67 | ```ruby |
| 68 | # spec/models/event_spec.rb |
| 69 | RSpec.describe Event, type: :model do |
| 70 | describe "[ConcernName] behavior" do |
| 71 | describe "#method_from_concern" do |
| 72 | let(:event) { build(:event) } |
| 73 | |
| 74 | it "does something" do |
| 75 | expect(event.method_from_concern).to eq(expected) |
| 76 | end |
| 77 | end |
| 78 | end |
| 79 | end |
| 80 | ``` |
| 81 | |
| 82 | For **Controller Concerns**, test via request specs: |
| 83 | |
| 84 | ```ruby |
| 85 | # spec/requests/[feature]_spec.rb |
| 86 | RSpec.describe "[Feature]", type: :request do |
| 87 | describe "pagination (from Paginatable concern)" do |
| 88 | let(:user) { create(:user) } |
| 89 | before { sign_in user } |
| 90 | |
| 91 | it "paginates results" do |
| 92 | create_list(:resource, 30, account: user.account) |
| 93 | get resources_path |
| 94 | expect(response.body).to include("page") |
| 95 | end |
| 96 | end |
| 97 | end |
| 98 | ``` |
| 99 | |
| 100 | ### Step 2: Run Spec (Confirm RED) |
| 101 | |
| 102 | ```bash |
| 103 | bundle exec rspec spec/models/concerns/[concern_name]_spec.rb |
| 104 | # OR |
| 105 | bundle exec rspec spec/models/[model]_spec.rb |
| 106 | ``` |
| 107 | |
| 108 | ### Step 3: Implement Concern (GREEN) |
| 109 | |
| 110 | **Model Concern:** |
| 111 | |
| 112 | ```ruby |
| 113 | # app/models/concerns/[concern_name].rb |
| 114 | module [ConcernName] |
| 115 | extend ActiveSupport::Concern |
| 116 | |
| 117 | included do |
| 118 | # Callbacks |
| 119 | before_validation :generate_uuid, on: :create |
| 120 | |
| 121 | # Validations |
| 122 | validates :uuid, presence: true, uniqueness: true |
| 123 | |
| 124 | # Scopes |
| 125 | scope :with_uuid, ->(uuid) { where(uuid: uuid) } |
| 126 | scope :recent, -> { order(created_at: :desc) } |
| 127 | end |
| 128 | |
| 129 | # Class methods |
| 130 | class_methods do |
| 131 | def find_by_uuid!(uuid) |
| 132 | find_by!(uuid: uuid) |
| 133 | end |
| 134 | end |
| 135 | |
| 136 | # Instance methods |
| 137 | def generate_uuid |
| 138 | self.uuid ||= SecureRandom.uuid |
| 139 | end |
| 140 | |
| 141 | def short_uuid |
| 142 | uuid&.split("-")&.first |
| 143 | end |
| 144 | end |
| 145 | ``` |
| 146 | |
| 147 | **Controller Concern:** |
| 148 | |
| 149 | ```ruby |
| 150 | # app/controllers/concerns/[concern_name].rb |
| 151 | module [ConcernName] |
| 152 | extend ActiveSupport::Concern |
| 153 | |
| 154 | included do |
| 155 | before_action :set_locale |
| 156 | helper_method :current_locale |
| 157 | end |
| 158 | |
| 159 | class_methods do |
| 160 | def skip_locale_for(*actions) |
| 161 | skip_before_action :set_locale, only: actions |
| 162 | end |
| 163 | end |
| 164 | |
| 165 | private |
| 166 | |
| 167 | def set_locale |
| 168 | I18n.locale = params[:locale] || I18n.default_locale |
| 169 | end |
| 170 | |
| 171 | def current_locale |
| 172 | I18n.locale |
| 173 | end |
| 174 | end |
| 175 | ``` |
| 176 | |
| 177 | ### Step 4: Run Spec (Confirm GREEN) |
| 178 | |
| 179 | ```bash |
| 180 | bundle exec rspec spec/models/concerns/[concern_name]_spec.rb |
| 181 | ``` |
| 182 | |
| 183 | ## Common Concern Patterns |
| 184 | |
| 185 | ### Pattern 1: UUID Generation |
| 186 | |
| 187 | ```ruby |
| 188 | # app/models/concerns/has_uuid.rb |
| 189 | module HasUuid |
| 190 | extend ActiveSupport::Concern |
| 191 | |
| 192 | included do |
| 193 | before_validation :generate_uuid, on: :create |
| 194 | validates :uuid, presence: true, uniqueness: true |
| 195 | end |
| 196 | |
| 197 | private |
| 198 | |
| 199 | def generate_uuid |
| 200 | self.uuid ||= SecureRandom.uuid |
| 201 | end |
| 202 | end |
| 203 | ``` |
| 204 | |
| 205 | ### Pattern 2: Soft Delete |
| 206 | |
| 207 | ```ruby |
| 208 | # app/models/concerns/soft_deletable.rb |
| 209 | module SoftDeletable |
| 210 | extend ActiveSupport::Concern |
| 211 | |
| 212 | included do |
| 213 | scope :active, -> { where(deleted_at: nil) } |
| 214 | scope :deleted, -> { where.not(deleted_at: nil) |