factory_seeder 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +111 -0
- data/LICENSE.txt +21 -0
- data/README.md +445 -0
- data/app/assets/stylesheets/factory_seeder.css +637 -0
- data/app/controllers/factory_seeder/application_controller.rb +8 -0
- data/app/controllers/factory_seeder/custom_seeds_controller.rb +134 -0
- data/app/controllers/factory_seeder/dashboard_controller.rb +36 -0
- data/app/controllers/factory_seeder/factory_controller.rb +70 -0
- data/app/views/factory_seeder/custom_seeds/index.html.erb +51 -0
- data/app/views/factory_seeder/custom_seeds/show.html.erb +113 -0
- data/app/views/factory_seeder/dashboard/index.html.erb +99 -0
- data/app/views/factory_seeder/factory/index.html.erb +71 -0
- data/app/views/factory_seeder/factory/show.html.erb +108 -0
- data/app/views/factory_seeder/seeds/show.html.erb +2 -0
- data/app/views/layouts/factory_seeder/application.html.erb +25 -0
- data/bin/factory_seeder +27 -0
- data/config/factory_seeder.rb +24 -0
- data/config/routes.rb +20 -0
- data/lib/factory_seeder/asset_helper.rb +34 -0
- data/lib/factory_seeder/cli.rb +352 -0
- data/lib/factory_seeder/configuration.rb +32 -0
- data/lib/factory_seeder/custom_seed_loader.rb +39 -0
- data/lib/factory_seeder/engine.rb +16 -0
- data/lib/factory_seeder/execution_log_store.rb +48 -0
- data/lib/factory_seeder/factory_scanner.rb +149 -0
- data/lib/factory_seeder/loader.rb +26 -0
- data/lib/factory_seeder/rails_integration.rb +29 -0
- data/lib/factory_seeder/seed.rb +102 -0
- data/lib/factory_seeder/seed_builder.rb +67 -0
- data/lib/factory_seeder/seed_generator.rb +305 -0
- data/lib/factory_seeder/seed_manager.rb +128 -0
- data/lib/factory_seeder/seeder.rb +41 -0
- data/lib/factory_seeder/version.rb +5 -0
- data/lib/factory_seeder/web_interface.rb +119 -0
- data/lib/factory_seeder.rb +209 -0
- data/templates/seed_template.rb +84 -0
- metadata +276 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 515d25311b8a818c9f74754ae339a5c359123a4052801c319a21d71953f23428
|
|
4
|
+
data.tar.gz: 04e02636e4d566d7f27000063584723a3ddc14d438027802e6b2255e3782e9a9
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 14d2b7088c42b05be4742efc07933e942c104237aef374d523be4efcf7a4588685eb578f88e8adc94a6920952f990832b0b12a09b304c08dc2dda44b3226491a
|
|
7
|
+
data.tar.gz: 2e4c6314258c616ea4019deec5d66449ee0516b0cb8b1b267f286c56e64b39cdeb8964ae8ff57d7d7b5049d165bcd2f411a2cda9fab14354ea929bef21ac97f9
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
#### Core Features
|
|
13
|
+
- **Automatic Factory Detection**: Scans and analyzes FactoryBot factories automatically
|
|
14
|
+
- **CLI Interface**: Command-line tool with interactive mode using Thor
|
|
15
|
+
- **Web Interface**: Modern Sinatra-based UI for visual seeding
|
|
16
|
+
- **Rails Engine Integration**: Seamless Rails integration with automatic mounting
|
|
17
|
+
- **Ruby API**: Programmatic interface for seeding in Ruby code
|
|
18
|
+
- **Preview Mode**: See data before creating records
|
|
19
|
+
- **Configuration System**: Flexible environment-specific configuration
|
|
20
|
+
- **Custom Seeds System**: Define reusable seeds with parameter validation and metadata
|
|
21
|
+
|
|
22
|
+
#### Custom Seeds Features
|
|
23
|
+
- **SeedBuilder DSL**: Fluent interface for defining seeds with parameter validation
|
|
24
|
+
- **Parameter Types**: Support for integer, boolean, symbol, string, and array parameters
|
|
25
|
+
- **Parameter Validation**: Type checking, min/max values, allowed values, and required fields
|
|
26
|
+
- **Metadata Support**: Descriptions and documentation for seeds and parameters
|
|
27
|
+
- **Web UI for Custom Seeds**: Dynamic forms based on parameter definitions
|
|
28
|
+
- **Auto-reload**: Custom seeds under `db/factory_seeds/*.rb` are automatically reloaded
|
|
29
|
+
|
|
30
|
+
#### Rails Integration Improvements
|
|
31
|
+
- **Automatic Model Loading**: Models loaded via `config.after_initialize` hook
|
|
32
|
+
- **Development Reloading**: Models reloaded when files change via `config.to_prepare`
|
|
33
|
+
- **Conditional Eager Loading**: Only forces eager loading when necessary
|
|
34
|
+
- **Error Handling**: Graceful handling of uninitialized constants and missing dependencies
|
|
35
|
+
- **Rails Engine**: Proper Rails Engine with isolated namespace
|
|
36
|
+
|
|
37
|
+
#### CLI Enhancements
|
|
38
|
+
- **Detailed Factory Listing**: Shows class name, traits, associations, and key attributes
|
|
39
|
+
- **Default Configuration Values**: Inherits `default_count` and `default_strategy` from config
|
|
40
|
+
- **JSON Attribute Support**: Accept JSON payloads via `--attributes` flag
|
|
41
|
+
- **Verbose Mode**: Detailed error messages and loading information
|
|
42
|
+
- **Interactive Mode**: User-friendly prompts for factory and trait selection
|
|
43
|
+
|
|
44
|
+
#### Web Interface Features
|
|
45
|
+
- **Factory Metadata Display**: Shows class, traits, associations, and attributes
|
|
46
|
+
- **Custom Attribute Inputs**: Dynamic input fields for each factory attribute
|
|
47
|
+
- **Trait Selection**: Checkbox-based trait selection
|
|
48
|
+
- **Real-time Preview**: Preview data before generation
|
|
49
|
+
- **Custom Seeds Dashboard**: Browse and execute custom seeds with parameter forms
|
|
50
|
+
- **Auto-reload**: Standalone web calls `FactorySeeder.reload!` before each request
|
|
51
|
+
- **Execution Logs**: Capture and display execution logs in console panel
|
|
52
|
+
|
|
53
|
+
### Changed
|
|
54
|
+
- **CLI `list` command**: Now reports class name, traits, associations, and attribute hints
|
|
55
|
+
- **CLI `generate` and `preview`**: Default to `config.default_count` and `config.default_strategy` when options are omitted
|
|
56
|
+
- **Attribute Support**: Accept JSON payloads for `--attributes` in CLI
|
|
57
|
+
- **Factory Loading**: Improved error handling with retry mechanism for failed factories
|
|
58
|
+
- **Model Class Resolution**: Safer approach using inferred class names when models aren't loaded
|
|
59
|
+
- **Custom Seed Reloading**: Rails engine reloads `db/factory_seeds/*.rb` automatically on each request
|
|
60
|
+
- **Web Interface Reloading**: Standalone web interface reloads configuration and seeds on each request
|
|
61
|
+
|
|
62
|
+
### Technical Details
|
|
63
|
+
|
|
64
|
+
#### Architecture
|
|
65
|
+
- **FactoryScanner**: Detects and analyzes FactoryBot factories with robust error handling
|
|
66
|
+
- **SeedGenerator**: Creates database records using factories, traits, and custom attributes
|
|
67
|
+
- **SeedManager**: Manages custom seed registry with validation
|
|
68
|
+
- **Seed**: Represents a seed definition with parameters and metadata
|
|
69
|
+
- **SeedBuilder**: DSL for building seed definitions
|
|
70
|
+
- **CLI**: Thor-based command-line interface
|
|
71
|
+
- **WebInterface**: Sinatra application with JSON API
|
|
72
|
+
- **Engine**: Rails::Engine for Rails integration
|
|
73
|
+
- **Configuration**: Centralized configuration management
|
|
74
|
+
- **CustomSeedLoader**: Auto-loads custom seeds from `db/factory_seeds/`
|
|
75
|
+
|
|
76
|
+
#### Dependencies
|
|
77
|
+
- Built on FactoryBot 6.x
|
|
78
|
+
- Thor for CLI interface
|
|
79
|
+
- Sinatra for web interface
|
|
80
|
+
- ActiveSupport for Rails integration
|
|
81
|
+
- Faker for realistic test data
|
|
82
|
+
- Zeitwerk for autoloading
|
|
83
|
+
- WebRick for standalone web server
|
|
84
|
+
|
|
85
|
+
### Fixed
|
|
86
|
+
- **NameError handling**: Gracefully handle uninitialized constant errors during factory loading
|
|
87
|
+
- **Model loading race conditions**: Ensure Rails models are loaded before factory analysis
|
|
88
|
+
- **Trait parsing**: Correct parsing of comma-separated traits in CLI and web API
|
|
89
|
+
- **Factory class resolution**: Safe class name inference when models aren't loaded
|
|
90
|
+
- **Constant inflection**: Fixed CLI constant inflection for Zeitwerk compatibility
|
|
91
|
+
- **Session-based logs**: Store execution logs in session instead of flash for persistence
|
|
92
|
+
|
|
93
|
+
### Security
|
|
94
|
+
- Input validation for all custom seed parameters
|
|
95
|
+
- Type checking and sanitization for user inputs
|
|
96
|
+
- Protection against invalid factory names
|
|
97
|
+
|
|
98
|
+
## [0.1.0] - 2024-08-19
|
|
99
|
+
|
|
100
|
+
### Added
|
|
101
|
+
- Initial release
|
|
102
|
+
- Core functionality implemented
|
|
103
|
+
- Basic CLI and web interfaces
|
|
104
|
+
- Factory scanning and analysis
|
|
105
|
+
- Seed generation with traits and associations
|
|
106
|
+
- Configuration system
|
|
107
|
+
- Documentation and examples
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
For detailed usage examples and migration guides, see the README.md file.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 FactorySeeder Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
# FactorySeeder
|
|
2
|
+
|
|
3
|
+
A powerful Ruby gem that simplifies database seeding using your existing FactoryBot factories, traits, and associations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Automatic Factory Detection**: Scans your FactoryBot factories automatically
|
|
8
|
+
- **Trait Support**: Use existing traits to create varied data
|
|
9
|
+
- **Association Handling**: Manage complex relationships between models
|
|
10
|
+
- **Web Interface**: Beautiful web UI for visual seeding with Rails Engine integration
|
|
11
|
+
- **CLI Interface**: Command-line tool for quick seeding
|
|
12
|
+
- **Custom Seeds System**: Define reusable seeds with parameter validation
|
|
13
|
+
- **Preview Mode**: See what data will be generated before creating it
|
|
14
|
+
- **Configuration**: Flexible configuration for different environments
|
|
15
|
+
- **Auto-reload**: Changes to custom seeds are automatically reloaded without server restart
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Add this line to your application's Gemfile:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
group :development, :test do
|
|
23
|
+
gem 'factory_seeder'
|
|
24
|
+
end
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then execute:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
bundle install
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
### 1. Initialize FactorySeeder
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
bundle exec factory_seeder init
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This creates:
|
|
42
|
+
- `config/initializers/factory_seeder.rb` - Configuration file
|
|
43
|
+
- `db/seeds_factory_seeder.rb` - Sample seeds file
|
|
44
|
+
|
|
45
|
+
### 2. List Available Factories
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
bundle exec factory_seeder list
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The CLI now displays detailed information for each factory including class name, traits, associations, and key attributes - matching the web interface experience.
|
|
52
|
+
|
|
53
|
+
### 3. Generate Seeds
|
|
54
|
+
|
|
55
|
+
#### Interactive Mode
|
|
56
|
+
```bash
|
|
57
|
+
bundle exec factory_seeder generate
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### Direct Mode
|
|
61
|
+
```bash
|
|
62
|
+
bundle exec factory_seeder generate user --count=10 --traits=admin,vip
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The `generate` and `preview` commands now use `config.default_count` and `config.default_strategy` when options are omitted.
|
|
66
|
+
|
|
67
|
+
#### With Custom Attributes
|
|
68
|
+
```bash
|
|
69
|
+
bundle exec factory_seeder generate user --count=5 --attributes='{"email":"admin@example.com","role":"admin"}'
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 4. Web Interface (Rails Engine)
|
|
73
|
+
|
|
74
|
+
Mount the engine in your Rails application routes:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
# config/routes.rb
|
|
78
|
+
Rails.application.routes.draw do
|
|
79
|
+
mount FactorySeeder::Engine => "/factory-seeder"
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Then access the web interface at `http://localhost:3000/factory-seeder`
|
|
84
|
+
|
|
85
|
+
**Features:**
|
|
86
|
+
- Visual factory listing with detailed metadata
|
|
87
|
+
- Trait selection with checkboxes
|
|
88
|
+
- Custom attribute inputs for each factory field
|
|
89
|
+
- Data preview before generation
|
|
90
|
+
- Real-time generation
|
|
91
|
+
- Auto-reload: Custom seeds under `db/factory_seeds/*.rb` are automatically reloaded when the Rails engine prepares a request - no server restart needed
|
|
92
|
+
|
|
93
|
+
### 5. Standalone Web Interface
|
|
94
|
+
|
|
95
|
+
For non-Rails projects or standalone usage:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
bundle exec factory_seeder web --port=4567
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The standalone web interface calls `FactorySeeder.reload!` before each request, so file edits take effect immediately.
|
|
102
|
+
|
|
103
|
+
## Usage
|
|
104
|
+
|
|
105
|
+
### Ruby API
|
|
106
|
+
|
|
107
|
+
#### Basic Generation
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
# In your seeds file
|
|
111
|
+
FactorySeeder.generate do |seeder|
|
|
112
|
+
# Create 10 users with admin trait
|
|
113
|
+
seeder.create(:user, count: 10, traits: [:admin])
|
|
114
|
+
|
|
115
|
+
# Create posts with associations
|
|
116
|
+
seeder.create_with_associations(:post, count: 5, associations: {
|
|
117
|
+
author: { factory: :user, count: 1 },
|
|
118
|
+
comments: { factory: :comment, count: 3 }
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
# Create with custom attributes
|
|
122
|
+
seeder.create(:user, count: 5, attributes: {
|
|
123
|
+
email: "custom@example.com",
|
|
124
|
+
role: "moderator"
|
|
125
|
+
})
|
|
126
|
+
end
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### Different Strategies
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
# Build instead of create (useful for testing)
|
|
133
|
+
seeder.create(:user, count: 3, strategy: :build)
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Custom Seeds System
|
|
137
|
+
|
|
138
|
+
Define reusable seeds with parameter validation:
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
# db/factory_seeds/users.rb
|
|
142
|
+
FactorySeeder.define_seed(:create_users) do |builder|
|
|
143
|
+
builder
|
|
144
|
+
.description("Create users with customizable options")
|
|
145
|
+
.integer_param(:count, required: true, min: 1, max: 100, description: "Number of users")
|
|
146
|
+
.boolean_param(:admin, required: false, default: false, description: "Create admin users")
|
|
147
|
+
.symbol_param(:country, required: false, default: :fr,
|
|
148
|
+
allowed_values: [:fr, :en, :es, :de], description: "User country")
|
|
149
|
+
end do |count:, admin: false, country: :fr|
|
|
150
|
+
traits = admin ? [:admin] : []
|
|
151
|
+
count.times do
|
|
152
|
+
FactoryBot.create(:user, *traits, country: country)
|
|
153
|
+
end
|
|
154
|
+
puts "✅ Created #{count} user(s) with country: #{country}#{admin ? ' (admin)' : ''}"
|
|
155
|
+
end
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### Parameter Types
|
|
159
|
+
|
|
160
|
+
- **Integer**: `.integer_param(:count, min: 1, max: 100)`
|
|
161
|
+
- **Boolean**: `.boolean_param(:admin, default: false)`
|
|
162
|
+
- **Symbol**: `.symbol_param(:status, allowed_values: [:active, :inactive])`
|
|
163
|
+
- **String**: `.string_param(:name, allowed_values: ['admin', 'user'])`
|
|
164
|
+
- **Array**: `.array_param(:items, default: [])`
|
|
165
|
+
|
|
166
|
+
#### Running Custom Seeds
|
|
167
|
+
|
|
168
|
+
```ruby
|
|
169
|
+
# Programmatically
|
|
170
|
+
result = FactorySeeder.run_custom_seed(:create_users, count: 10, admin: true)
|
|
171
|
+
|
|
172
|
+
# Via web interface - navigate to custom seeds section
|
|
173
|
+
# Via CLI - use the custom seeds commands
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### CLI Commands
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
# List all factories with detailed metadata
|
|
180
|
+
factory_seeder list
|
|
181
|
+
|
|
182
|
+
# Generate seeds interactively
|
|
183
|
+
factory_seeder generate
|
|
184
|
+
|
|
185
|
+
# Generate specific factory
|
|
186
|
+
factory_seeder generate user --count=5 --traits=admin,vip
|
|
187
|
+
|
|
188
|
+
# Generate with custom attributes (JSON format)
|
|
189
|
+
factory_seeder generate user --count=3 --attributes='{"email":"test@example.com","admin":true}'
|
|
190
|
+
|
|
191
|
+
# Preview factory data
|
|
192
|
+
factory_seeder preview user --traits=admin
|
|
193
|
+
|
|
194
|
+
# Start standalone web interface
|
|
195
|
+
factory_seeder web --port=4567
|
|
196
|
+
|
|
197
|
+
# Initialize configuration
|
|
198
|
+
factory_seeder init
|
|
199
|
+
|
|
200
|
+
# List available custom seeds
|
|
201
|
+
factory_seeder seeds --list
|
|
202
|
+
|
|
203
|
+
# Run a specific custom seed
|
|
204
|
+
factory_seeder seeds development
|
|
205
|
+
|
|
206
|
+
# Run all custom seeds
|
|
207
|
+
factory_seeder seeds --all
|
|
208
|
+
|
|
209
|
+
# Preview what would be generated (dry run)
|
|
210
|
+
factory_seeder seeds development --dry_run
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Configuration
|
|
214
|
+
|
|
215
|
+
Edit `config/factory_seeder.rb`:
|
|
216
|
+
|
|
217
|
+
```ruby
|
|
218
|
+
FactorySeeder.configure do |config|
|
|
219
|
+
# Add custom factory paths
|
|
220
|
+
config.factory_paths << "spec/factories"
|
|
221
|
+
config.factory_paths << "test/factories"
|
|
222
|
+
|
|
223
|
+
# Default options
|
|
224
|
+
config.default_count = 10
|
|
225
|
+
config.default_strategy = :create
|
|
226
|
+
|
|
227
|
+
# Verbose mode
|
|
228
|
+
config.verbose = Rails.env.development?
|
|
229
|
+
|
|
230
|
+
# Environment-specific settings
|
|
231
|
+
config.environments = {
|
|
232
|
+
development: { default_count: 50 },
|
|
233
|
+
test: { default_count: 5 },
|
|
234
|
+
production: { default_count: 1 }
|
|
235
|
+
}
|
|
236
|
+
end
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Rails Integration
|
|
240
|
+
|
|
241
|
+
FactorySeeder integrates seamlessly with Rails through a Rails Engine:
|
|
242
|
+
|
|
243
|
+
### Features
|
|
244
|
+
|
|
245
|
+
- **Automatic Model Loading**: Models are loaded before factory analysis using `config.after_initialize`
|
|
246
|
+
- **Development Reloading**: In development, models are reloaded when files change via `config.to_prepare`
|
|
247
|
+
- **Conditional Loading**: Only forces eager loading when necessary
|
|
248
|
+
- **Error Handling**: Gracefully handles uninitialized constants and missing dependencies
|
|
249
|
+
|
|
250
|
+
### Troubleshooting Rails Integration
|
|
251
|
+
|
|
252
|
+
#### "uninitialized constant" errors
|
|
253
|
+
|
|
254
|
+
If you encounter errors like `NameError: uninitialized constant ModelName`, the Rails engine should handle this automatically. If issues persist:
|
|
255
|
+
|
|
256
|
+
1. Ensure all migrations are up to date: `rails db:migrate`
|
|
257
|
+
2. Check that models are properly defined in `app/models`
|
|
258
|
+
3. Enable verbose mode to see detailed loading information:
|
|
259
|
+
|
|
260
|
+
```ruby
|
|
261
|
+
FactorySeeder.configure do |config|
|
|
262
|
+
config.verbose = true
|
|
263
|
+
end
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
#### Factories not detected
|
|
267
|
+
|
|
268
|
+
If factories aren't appearing:
|
|
269
|
+
|
|
270
|
+
```ruby
|
|
271
|
+
FactorySeeder.configure do |config|
|
|
272
|
+
config.factory_paths << 'spec/factories'
|
|
273
|
+
config.factory_paths << 'test/factories'
|
|
274
|
+
end
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Factory Examples
|
|
278
|
+
|
|
279
|
+
### Basic Factory
|
|
280
|
+
|
|
281
|
+
```ruby
|
|
282
|
+
# spec/factories/users.rb
|
|
283
|
+
FactoryBot.define do
|
|
284
|
+
factory :user do
|
|
285
|
+
sequence(:email) { |n| "user#{n}@example.com" }
|
|
286
|
+
password { "password123" }
|
|
287
|
+
|
|
288
|
+
trait :admin do
|
|
289
|
+
role { "admin" }
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
trait :vip do
|
|
293
|
+
vip_status { true }
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Factory with Associations
|
|
300
|
+
|
|
301
|
+
```ruby
|
|
302
|
+
# spec/factories/posts.rb
|
|
303
|
+
FactoryBot.define do
|
|
304
|
+
factory :post do
|
|
305
|
+
title { Faker::Lorem.sentence }
|
|
306
|
+
content { Faker::Lorem.paragraph }
|
|
307
|
+
association :author, factory: :user
|
|
308
|
+
|
|
309
|
+
trait :published do
|
|
310
|
+
published_at { Time.current }
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
trait :with_comments do
|
|
314
|
+
after(:create) do |post|
|
|
315
|
+
create_list(:comment, 3, post: post)
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## Advanced Usage
|
|
323
|
+
|
|
324
|
+
### Environment-Specific Seeds
|
|
325
|
+
|
|
326
|
+
```ruby
|
|
327
|
+
FactorySeeder.generate do |seeder|
|
|
328
|
+
if Rails.env.development?
|
|
329
|
+
seeder.create(:user, count: 100, traits: [:admin])
|
|
330
|
+
seeder.create(:post, count: 500)
|
|
331
|
+
elsif Rails.env.test?
|
|
332
|
+
seeder.create(:user, count: 5)
|
|
333
|
+
seeder.create(:post, count: 10)
|
|
334
|
+
elsif Rails.env.production?
|
|
335
|
+
seeder.create(:user, count: 1, traits: [:admin])
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### Complex Associations
|
|
341
|
+
|
|
342
|
+
```ruby
|
|
343
|
+
seeder.create_with_associations(:order, count: 10, associations: {
|
|
344
|
+
customer: { factory: :user, traits: [:vip] },
|
|
345
|
+
items: { factory: :product, count: 3 },
|
|
346
|
+
shipping_address: { factory: :address, count: 1 }
|
|
347
|
+
})
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### Using Rails Model Constants
|
|
351
|
+
|
|
352
|
+
With the Rails engine, you can safely use model constants in your custom seeds:
|
|
353
|
+
|
|
354
|
+
```ruby
|
|
355
|
+
FactorySeeder.define_seed(:create_orders_with_status) do |builder|
|
|
356
|
+
# Rails models are automatically loaded
|
|
357
|
+
order_statuses = if defined?(Order) && Order.const_defined?(:STATUSES)
|
|
358
|
+
Order::STATUSES.map(&:to_sym)
|
|
359
|
+
else
|
|
360
|
+
[:pending, :confirmed, :completed, :cancelled]
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
builder
|
|
364
|
+
.description("Create orders with specific status")
|
|
365
|
+
.symbol_param(:status, required: true, allowed_values: order_statuses)
|
|
366
|
+
end do |status:, count: 1|
|
|
367
|
+
count.times do
|
|
368
|
+
Order.create!(status: status)
|
|
369
|
+
end
|
|
370
|
+
end
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## Development
|
|
374
|
+
|
|
375
|
+
### Setup
|
|
376
|
+
|
|
377
|
+
```bash
|
|
378
|
+
git clone https://github.com/factoryseeder/factory_seeder.git
|
|
379
|
+
cd factory_seeder
|
|
380
|
+
bundle install
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Testing
|
|
384
|
+
|
|
385
|
+
```bash
|
|
386
|
+
bundle exec rspec
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### Building the Gem
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
gem build factory_seeder.gemspec
|
|
393
|
+
gem install factory_seeder-0.1.0.gem
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## Architecture
|
|
397
|
+
|
|
398
|
+
### Core Components
|
|
399
|
+
|
|
400
|
+
- **FactoryScanner**: Automatically detects and analyzes FactoryBot factories
|
|
401
|
+
- **SeedGenerator**: Creates database records using factories and traits
|
|
402
|
+
- **SeedManager**: Manages custom seed definitions with parameter validation
|
|
403
|
+
- **Seed & SeedBuilder**: Define reusable seeds with type-safe parameters
|
|
404
|
+
- **CLI**: Command-line interface with interactive prompts
|
|
405
|
+
- **WebInterface**: Sinatra-based web UI for visual seeding
|
|
406
|
+
- **Engine**: Rails Engine for seamless Rails integration
|
|
407
|
+
- **Configuration**: Flexible configuration system
|
|
408
|
+
|
|
409
|
+
### Dependencies
|
|
410
|
+
|
|
411
|
+
- **Ruby**: >= 2.7.0
|
|
412
|
+
- **FactoryBot**: ~> 6.0
|
|
413
|
+
- **ActiveSupport**: >= 6.0
|
|
414
|
+
- **Thor**: ~> 1.0 (CLI)
|
|
415
|
+
- **Sinatra**: ~> 2.0 (Web interface)
|
|
416
|
+
- **Faker**: ~> 3.0 (Test data generation)
|
|
417
|
+
- **Zeitwerk**: ~> 2.6 (Autoloading)
|
|
418
|
+
|
|
419
|
+
## Contributing
|
|
420
|
+
|
|
421
|
+
1. Fork the repository
|
|
422
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
423
|
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
|
424
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
425
|
+
5. Open a Pull Request
|
|
426
|
+
|
|
427
|
+
## License
|
|
428
|
+
|
|
429
|
+
This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
430
|
+
|
|
431
|
+
## Support
|
|
432
|
+
|
|
433
|
+
- Issues: [GitHub Issues](https://github.com/factoryseeder/factory_seeder/issues)
|
|
434
|
+
- Email: team@factoryseeder.com
|
|
435
|
+
|
|
436
|
+
## Acknowledgments
|
|
437
|
+
|
|
438
|
+
- Built on top of [FactoryBot](https://github.com/thoughtbot/factory_bot)
|
|
439
|
+
- CLI powered by [Thor](https://github.com/erikhuda/thor)
|
|
440
|
+
- Web interface built with [Sinatra](https://sinatrarb.com/)
|
|
441
|
+
- Autoloading with [Zeitwerk](https://github.com/fxn/zeitwerk)
|
|
442
|
+
|
|
443
|
+
---
|
|
444
|
+
|
|
445
|
+
Made with ❤️ by Wecasa
|