Skill站外收录 @skilllib/skill-2014279424164704259
rails-model-generator
以测试驱动开发方式自动化构建 Rails 模型,涵盖需求定义、RSpec 测试编写、FactoryBot 工厂生成、数据库迁移创建与执行、模型类实现及验证,并支持关联关系、枚举、作用域、回调等完整功能特性。
软件开发 AI 技能
Skills.MD
# Rails Model Generator (TDD Approach)
## Overview
This skill creates models the TDD way:
1. Define requirements (attributes, validations, associations)
2. Write model spec with expected behavior (RED)
3. Create factory for test data
4. Generate migration
5. Implement model to pass specs (GREEN)
6. Refactor if needed
## Workflow Checklist
```
Model Creation Progress:
- [ ] Step 1: Define requirements (attributes, validations, associations)
- [ ] Step 2: Create model spec (RED)
- [ ] Step 3: Create factory
- [ ] Step 4: Run spec (should fail - no model/table)
- [ ] Step 5: Generate migration
- [ ] Step 6: Run migration
- [ ] Step 7: Create model file (empty)
- [ ] Step 8: Run spec (should fail - no validations)
- [ ] Step 9: Add validations and associations
- [ ] Step 10: Run spec (GREEN)
```
## Step 1: Requirements Template
Before writing code, define the model:
```markdown
## Model: [ModelName]
### Table: [table_name]
### Attributes
| Name | Type | Constraints | Default |
|------|------|-------------|---------|
| name | string | required, unique | - |
| email | string | required, unique, email format | - |
| status | integer | enum | 0 (pending) |
| organization_id | bigint | foreign key | - |
### Associations
- belongs_to :organization
- has_many :posts, dependent: :destroy
- has_one :profile, dependent: :destroy
### Validations
- name: presence, uniqueness, length(max: 100)
- email: presence, uniqueness, format(email)
- status: inclusion in enum values
### Scopes
- active: status = active
- recent: ordered by created_at desc
- by_organization(org): where organization_id = org.id
### Instance Methods
- full_name: combines first_name and last_name
- active?: checks if status is active
### Callbacks
- before_save :normalize_email
- after_create :send_welcome_email
```
## Step 2: Create Model Spec
Location: `spec/models/[model_name]_spec.rb`
```ruby
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ModelName, type: :model do
subject { build(:model_name) }
# === Associations ===
describe 'associations' do
it { is_expected.to belong_to(:organization) }
it { is_expected.to have_many(:posts).dependent(:destroy) }
end
# === Validations ===
describe 'validations' do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:email).case_insensitive }
it { is_expected.to validate_length_of(:name).is_at_most(100) }
end
# === Scopes ===
describe '.active' do
let!(:active_record) { create(:model_name, status: :active) }
let!(:inactive_record) { create(:model_name, status: :inactive) }
it 'returns only active records' do
expect(described_class.active).to include(active_record)
expect(described_class.active).not_to include(inactive_record)
end
end
# === Instance Methods ===
describe '#full_name' do
subject { build(:model_name, first_name: 'John', last_name: 'Doe') }
it 'returns combined name' do
expect(subject.full_name).to eq('John Doe')
end
end
end
```
See [templates/model_spec.erb](templates/model_spec.erb) for full template.
## Step 3: Create Factory
Location: `spec/factories/[model_name_plural].rb`
```ruby
# frozen_string_literal: true
FactoryBot.define do
factory :model_name do
sequence(:name) { |n| "Name #{n}" }
sequence(:email) { |n| "user#{n}@example.com" }
status { :pending }
association :organization
trait :active do
status { :active }
end
trait :with_posts do
after(:create) do |record|
create_list(:post, 3, model_name: record)
end
end
end
end
```
See [templates/factory.erb](templates/factory.erb) for full template.
## Step 4: Run Spec (Verify RED)
```bash
bundle exec rspec spec/models/model_name_spec.rb
```
Expected: Failure because model/table doesn't exist.
## Step 5: Generate Migration
```bash
bin/rails generate migration CreateModelNames \
name:string \
email:string:uniq \
status:integer \
organization:references
```
Review the generated migration and add:
- Null constraints: `null: false`
- Defaults: `default: 0`
- Indexes: `add_index :table, :column`
```ruby
# db/migrate/YYYYMMDDHHMMSS_create_model_names.rb
class CreateModelNames < ActiveRecord::Migration[8.0]
def change
create_table :model_names do |t|
t.string :name, null: false
t.string :email, null: false
t.integer :status, null: false, default: 0
t.references :organization, null: false, foreign_key: true
t.timestamps
end
add_index :model_names, :email, unique: true
add_index :model_names, :status
end
end
```
## Step 6: Run Migration
```bash
bin/rails db:migrate
```
Verify with:
```bash
bin/rails db:migrate:status
```
## Step 7: Create Model File
Location: `app/models/[model_name].rb`
```ruby
# frozen_string_literal: true
class ModelName < ApplicationRecord
end
```
## Step 8: Run Spec (Still RED)
```bash
bundle exec rspec spec/models/model_name_spec.rb
```
Expected: Failures for missing validations/associations.
## Step 9: Add Validations & Associations
```ruby
# frozen_string_literal: true
class ModelName < ApplicationRecord
# === Associations ===
belongs_to :organization
has_many :posts, dependent: :destroy
# === Enums ===
enum :status, { pending: 0, active: 1, suspended: 2 }
# === Validations ===
validates :name, presence: true,
uniqueness: true,
length: { maximum: 100 }
validates :email, presence: true,
uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
# === Scopes ===
scope :active, -> { where(status: :active) }
scope :recent, -> { order(created_at: :desc) }
# === Instance Methods ===
def full_name
"#{first_name} #{last_name}".strip
end
end
```
## Step 10: Run Spec (GREEN)
```bash
bundle exec rspec spec/models/model_name_spec.rb
```
All specs should pass.
## References
- See [templates/model_spec.erb](templates/model_spec.erb) for spec template
- See [templates/factory.erb](templates/factory.erb) for factory template
- See [reference/validations.md](reference/validations.md) for validation patterns
## Common Patterns
### Enum with Validation
```ruby
enum :status, { draft: 0, published: 1, archived: 2 }
validates :status, inclusion: { in: statuses.keys }
```
### Polymorphic Association
```ruby
belongs_to :commentable, polymorphic: true
```
### Counter Cache
```ruby
belongs_to :organization, counter_cache: true
# Add: organization.posts_count column
```
### Soft Delete
```ruby
scope :active, -> { where(deleted_at: nil) }
scope :deleted, -> { where.not(deleted_at: nil) }
def soft_delete
update(deleted_at: Time.current)
end
```
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「rails-model-generator」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次