seedie 0.3.0 → 0.4.1
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 +4 -4
- data/.rubocop.yml +91 -9
- data/CHANGELOG.md +101 -0
- data/Gemfile +4 -1
- data/README.md +18 -9
- data/Rakefile +1 -1
- data/lib/generators/seedie/install_generator.rb +28 -30
- data/lib/generators/seedie/templates/seedie_initializer.rb +15 -1
- data/lib/seedie/associations/base_association.rb +35 -31
- data/lib/seedie/associations/belongs_to.rb +12 -13
- data/lib/seedie/associations/has_and_belongs_to_many.rb +26 -0
- data/lib/seedie/associations/has_many.rb +6 -4
- data/lib/seedie/associations/has_one.rb +13 -13
- data/lib/seedie/configuration.rb +21 -1
- data/lib/seedie/field_values/custom_value.rb +14 -83
- data/lib/seedie/field_values/fake_value.rb +85 -17
- data/lib/seedie/field_values/faker_builder.rb +36 -36
- data/lib/seedie/field_values/value_template_validator.rb +91 -0
- data/lib/seedie/field_values_set.rb +21 -4
- data/lib/seedie/model/creator.rb +7 -5
- data/lib/seedie/model/id_generator.rb +10 -8
- data/lib/seedie/model/model_sorter.rb +14 -18
- data/lib/seedie/model_fields.rb +5 -3
- data/lib/seedie/model_seeder.rb +11 -22
- data/lib/seedie/polymorphic_association_helper.rb +20 -16
- data/lib/seedie/railtie.rb +3 -2
- data/lib/seedie/reporters/base_reporter.rb +75 -68
- data/lib/seedie/reporters/console_reporter.rb +16 -12
- data/lib/seedie/reporters/reportable.rb +14 -10
- data/lib/seedie/seeder.rb +5 -3
- data/lib/seedie/version.rb +1 -1
- data/lib/seedie.rb +11 -28
- data/lib/tasks/seedie.rake +4 -2
- metadata +28 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a25c63e4fcff73414ac22c60e08d8f7e2c7cd831e0480922cd3dd64bb5598b3
|
4
|
+
data.tar.gz: b824464c1d4d084856adbaec92e677ac09f1e3f983fd103ada6638df0b535d23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 901e08a66477d23d1c108071f116206d4f530a46911402ca178baf66e433cda231c884fbbb667b186842c03e6d1b608bda31be44071d360cfdc28f1f46242482
|
7
|
+
data.tar.gz: ff5a02c6f6a14af3bc041fc2d39a1550d66dcca61f1491160718cb59a5fc17a07963cf5e6302bbbc89f856796d6c4a0d0a7080167b48173023cbf5454170276d
|
data/.rubocop.yml
CHANGED
@@ -1,16 +1,98 @@
|
|
1
1
|
AllCops:
|
2
|
-
TargetRubyVersion: 2.
|
3
|
-
|
4
|
-
|
2
|
+
TargetRubyVersion: 2.7
|
3
|
+
NewCops: disable
|
4
|
+
SuggestExtensions: false
|
5
|
+
Exclude:
|
6
|
+
- "spec/dummy/bin/*"
|
7
|
+
|
8
|
+
Style/HashSyntax:
|
9
|
+
EnforcedStyle: ruby19_no_mixed_keys
|
10
|
+
EnforcedShorthandSyntax: never
|
11
|
+
|
12
|
+
Style/GuardClause:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions.
|
16
|
+
# SupportedStyles: assign_to_condition, assign_inside_condition
|
17
|
+
Style/ConditionalAssignment:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
# Configuration parameters: AllowedConstants.
|
21
|
+
Style/Documentation:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Style/IfUnlessModifier:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
|
28
|
+
# SupportedStyles: single_quotes, double_quotes
|
5
29
|
Style/StringLiterals:
|
6
|
-
Enabled: true
|
7
30
|
EnforcedStyle: double_quotes
|
31
|
+
ConsistentQuotesInMultiline: true
|
8
32
|
|
9
|
-
|
10
|
-
|
11
|
-
|
33
|
+
# Configuration parameters: EnforcedStyle.
|
34
|
+
# SupportedStyles: both, prefix, postfix
|
35
|
+
Style/NegatedIf:
|
36
|
+
Enabled: false
|
37
|
+
|
38
|
+
Style/RedundantBegin:
|
39
|
+
Enabled: false
|
40
|
+
|
41
|
+
# Configuration parameters: MinDigits, Strict, AllowedNumbers, AllowedPatterns.
|
42
|
+
Style/NumericLiterals:
|
43
|
+
Exclude:
|
44
|
+
- 'spec/dummy/db/*.rb'
|
12
45
|
|
46
|
+
Style/RedundantInterpolation:
|
47
|
+
Exclude:
|
48
|
+
- 'lib/seedie/reporters/console_reporter.rb'
|
49
|
+
|
50
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
|
51
|
+
# URISchemes: http, https
|
13
52
|
Layout/LineLength:
|
14
|
-
Max:
|
53
|
+
Max: 150
|
54
|
+
|
55
|
+
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros.
|
56
|
+
# NamePrefix: is_, has_, have_
|
57
|
+
# ForbiddenPrefixes: is_, has_, have_
|
58
|
+
# AllowedMethods: is_a?
|
59
|
+
# MethodDefinitionMacros: define_method, define_singleton_method
|
60
|
+
Naming/PredicateName:
|
15
61
|
Exclude:
|
16
|
-
-
|
62
|
+
- 'spec/**/*'
|
63
|
+
- 'lib/seedie/field_values/faker_builder.rb'
|
64
|
+
- 'lib/seedie/polymorphic_association_helper.rb'
|
65
|
+
|
66
|
+
Metrics/AbcSize:
|
67
|
+
Max: 30
|
68
|
+
Exclude:
|
69
|
+
- "lib/generators/seedie/install_generator.rb"
|
70
|
+
|
71
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
72
|
+
# AllowedMethods: refine
|
73
|
+
Metrics/BlockLength:
|
74
|
+
CountComments: false
|
75
|
+
Max: 30
|
76
|
+
Exclude:
|
77
|
+
- "spec/integration/*.rb"
|
78
|
+
- "spec/seedie/**/*.rb"
|
79
|
+
- "spec/generators/*.rb"
|
80
|
+
- "spec/dummy/**/*.rb"
|
81
|
+
|
82
|
+
# Configuration parameters: CountComments, CountAsOne.
|
83
|
+
Metrics/ClassLength:
|
84
|
+
Exclude:
|
85
|
+
- "lib/generators/seedie/install_generator.rb"
|
86
|
+
- "lib/seedie/field_values/faker_builder.rb"
|
87
|
+
|
88
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns.
|
89
|
+
Metrics/CyclomaticComplexity:
|
90
|
+
Max: 17
|
91
|
+
|
92
|
+
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
|
93
|
+
Metrics/MethodLength:
|
94
|
+
Max: 52
|
95
|
+
|
96
|
+
# Configuration parameters: AllowedMethods, AllowedPatterns.
|
97
|
+
Metrics/PerceivedComplexity:
|
98
|
+
Max: 15
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,104 @@
|
|
1
|
+
## Verson 0.4.1
|
2
|
+
|
3
|
+
### New Features
|
4
|
+
|
5
|
+
#### Added Model-specific Custom Attributes
|
6
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/47)
|
7
|
+
|
8
|
+
You can now define custom attributes for specific models in your `Seedie.configure` block:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
Seedie.configure do |config|
|
12
|
+
# Default custom attribute for all models
|
13
|
+
config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
|
14
|
+
|
15
|
+
# Model-specific custom attribute
|
16
|
+
config.custom_attributes[:user][:email] = "email_#{rand(10)}@example.com"
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
### Enhancements
|
21
|
+
|
22
|
+
#### Rubocop Cleanup
|
23
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/45)
|
24
|
+
|
25
|
+
#### Improved README
|
26
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/44)
|
27
|
+
|
28
|
+
#### Added Screencast
|
29
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/46)
|
30
|
+
|
31
|
+
## Version 0.4.0
|
32
|
+
|
33
|
+
### New Features
|
34
|
+
|
35
|
+
#### Added HasAndBelongsToMany Association support
|
36
|
+
|
37
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/43)
|
38
|
+
|
39
|
+
You can now add `has_and_belongs_to_many` associations to your seedie.yml file:
|
40
|
+
|
41
|
+
```yaml
|
42
|
+
has_and_belongs_to_many:
|
43
|
+
posts:
|
44
|
+
count: 5
|
45
|
+
attributes:
|
46
|
+
title: "{{Faker::Lorem.sentence}}"
|
47
|
+
body: "{{Faker::Lorem.paragraph}}"
|
48
|
+
```
|
49
|
+
|
50
|
+
#### Added the ability to generate nested BelongsTo Associations
|
51
|
+
|
52
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/34)
|
53
|
+
|
54
|
+
Before, Models had no ability to add assocations to the child_model.
|
55
|
+
|
56
|
+
```yaml
|
57
|
+
parent_model:
|
58
|
+
attributes:
|
59
|
+
some_attribute: 'hello'
|
60
|
+
associations:
|
61
|
+
has_many:
|
62
|
+
child_model: 2
|
63
|
+
```
|
64
|
+
|
65
|
+
Now you can add associations to the child_model:
|
66
|
+
|
67
|
+
```yaml
|
68
|
+
parent_model:
|
69
|
+
attributes:
|
70
|
+
some_attribute: 'hello'
|
71
|
+
associations:
|
72
|
+
has_many:
|
73
|
+
child_model:
|
74
|
+
attributes:
|
75
|
+
title: 'some title'
|
76
|
+
associations:
|
77
|
+
belongs_to:
|
78
|
+
another_parent_model: 'random'
|
79
|
+
```
|
80
|
+
|
81
|
+
### Enhancements
|
82
|
+
|
83
|
+
#### Complete Rubocop Integration with CI
|
84
|
+
|
85
|
+
* [Rubocop Integration](https://github.com/keshavbiswa/seedie/pull/33) by [@giovannism20](https://github.com/giovannism20)
|
86
|
+
* Rubocop cleanups:
|
87
|
+
- https://github.com/keshavbiswa/seedie/pull/37
|
88
|
+
- https://github.com/keshavbiswa/seedie/pull/38
|
89
|
+
- https://github.com/keshavbiswa/seedie/pull/39
|
90
|
+
- https://github.com/keshavbiswa/seedie/pull/40
|
91
|
+
- https://github.com/keshavbiswa/seedie/pull/41
|
92
|
+
- https://github.com/keshavbiswa/seedie/pull/42
|
93
|
+
|
94
|
+
#### Added Zeitwerk Support
|
95
|
+
|
96
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/35)
|
97
|
+
|
98
|
+
#### Added Rails 7.1 Support
|
99
|
+
|
100
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/36)
|
101
|
+
|
1
102
|
## Version 0.3.0
|
2
103
|
|
3
104
|
### New Features
|
data/Gemfile
CHANGED
@@ -6,10 +6,13 @@ source "https://rubygems.org"
|
|
6
6
|
gemspec
|
7
7
|
|
8
8
|
gem "rake", "~> 13.0"
|
9
|
-
gem "rubocop", "~> 1.21"
|
10
9
|
|
11
10
|
gem "pry", "~> 0.14.2"
|
12
11
|
|
13
12
|
group :test do
|
14
13
|
gem "simplecov", "~> 0.22.0", require: false
|
15
14
|
end
|
15
|
+
|
16
|
+
group :development, :test do
|
17
|
+
gem "rubocop", require: false
|
18
|
+
end
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Seedie
|
2
2
|
|
3
|
-
Seedie is a Ruby gem designed to
|
3
|
+
Seedie is a Ruby gem designed to make it easy to seed your database with realistic data.
|
4
4
|
Utilizing the Faker library, Seedie generates realistic data for ActiveRecord models.
|
5
5
|
Currently supports only PostrgreSQL and SQLite3 databases.
|
6
6
|
The gem includes a Rake task for seeding models and a Rails generator for easy setup.
|
@@ -8,6 +8,10 @@ The gem includes a Rake task for seeding models and a Rails generator for easy s
|
|
8
8
|
[](https://badge.fury.io/rb/seedie)
|
9
9
|

|
10
10
|
|
11
|
+
## Screencast
|
12
|
+
|
13
|
+
[](https://www.youtube.com/watch?v=3W0wRrOtsTk)
|
14
|
+
|
11
15
|
## Installation
|
12
16
|
|
13
17
|
Add the following line to your application's Gemfile:
|
@@ -54,13 +58,13 @@ $ rails generate seedie:install --blank
|
|
54
58
|
This will generate a blank seedie.yml config file for you that you can now customize according to your needs.
|
55
59
|
|
56
60
|
### Excluding Models
|
57
|
-
If you want to exclude certain models while generating the `seedie.yml`, use the `--
|
61
|
+
If you want to exclude certain models while generating the `seedie.yml`, use the `--excluded_models` option:
|
58
62
|
|
59
63
|
```bash
|
60
|
-
$ rails generate seedie:install --
|
64
|
+
$ rails generate seedie:install --excluded_models User Admin Post
|
61
65
|
```
|
62
66
|
|
63
|
-
NOTE: Some models may not be excluded because of their dependencies. For example, if you have a model `Post` that belongs to a model `User`, then the `User` model will not be excluded even if you specify it in the `--
|
67
|
+
NOTE: Some models may not be excluded because of their dependencies. For example, if you have a model `Post` that belongs to a model `User`, then the `User` model will not be excluded even if you specify it in the `--excluded_models` option.
|
64
68
|
|
65
69
|
You'll get a warning in your console if any models are not excluded:
|
66
70
|
|
@@ -113,7 +117,7 @@ $ rake seedie:seed
|
|
113
117
|
|
114
118
|
This will use the configurations specified in seedie.yml to seed your models.
|
115
119
|
|
116
|
-
The seedie.yml file has entries for each model in your application, and you can customize the configuration for each one.
|
120
|
+
The seedie.yml file has entries for each model in your application, and you can customize the configuration for each one.
|
117
121
|
|
118
122
|
Here's an example of a more advanced configuration in seedie.yml:
|
119
123
|
|
@@ -133,7 +137,7 @@ models:
|
|
133
137
|
title: "title {{index}}"
|
134
138
|
category:
|
135
139
|
values: [tech, sports, politics, entertainment]
|
136
|
-
options:
|
140
|
+
options:
|
137
141
|
pick_strategy: random # or sequential
|
138
142
|
associations:
|
139
143
|
has_many:
|
@@ -141,9 +145,14 @@ models:
|
|
141
145
|
belongs_to:
|
142
146
|
user: random # or new
|
143
147
|
has_one:
|
144
|
-
post_metadatum:
|
148
|
+
post_metadatum:
|
145
149
|
attributes:
|
146
150
|
seo_text: "{{Faker::Lorem.paragraph}}"
|
151
|
+
has_and_belongs_to_many:
|
152
|
+
tags:
|
153
|
+
count: 3
|
154
|
+
attributes:
|
155
|
+
name: "{{Faker::Lorem.word}}"
|
147
156
|
disabled_fields: []
|
148
157
|
comment:
|
149
158
|
attributes:
|
@@ -165,7 +174,7 @@ In this file:
|
|
165
174
|
- Additionally, we can use placeholders like `{{Faker::Internet.email}}` to generate dynamic and unique data for each record using Faker.
|
166
175
|
- We can also specify an array of values that can be picked from randomly or sequentially using the `values` and `pick_strategy` options.
|
167
176
|
- `disabled_fields` is an array of fields that should not be automatically filled by Seedie.
|
168
|
-
- `associations` specify how associated models should be generated. Here, `has_many`, `belongs_to`, and `
|
177
|
+
- `associations` specify how associated models should be generated. Here, `has_many`, `belongs_to`, `has_one` and `has_and_belongs_to_many` are supported.
|
169
178
|
- The specified number for `has_many` represents the number of associated records to create.
|
170
179
|
- For `belongs_to`, the value `random` means that a random existing record will be associated. If there is a unique index associated, then `unique` will be set or else `random` is the default.
|
171
180
|
- If attributes are specified under an association, those attributes will be used when creating the associated record(s)
|
@@ -173,7 +182,7 @@ In this file:
|
|
173
182
|
|
174
183
|
## Development
|
175
184
|
|
176
|
-
After checking out the repo, run `bin/setup` to install dependencies.
|
185
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
177
186
|
Then, run `bundle exec rspec` to run the tests.
|
178
187
|
By default, the tests will supress output of the seeds progress.
|
179
188
|
Use `DEBUG_OUTPUT=true bundle exec rspec` to see the output of the seeds.
|
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "rails/generators/base"
|
2
4
|
require "seedie"
|
3
5
|
|
@@ -5,7 +7,7 @@ module Seedie
|
|
5
7
|
module Generators
|
6
8
|
class InstallGenerator < Rails::Generators::Base
|
7
9
|
include PolymorphicAssociationHelper
|
8
|
-
|
10
|
+
|
9
11
|
EXCLUDED_MODELS = %w[
|
10
12
|
ActiveRecord::SchemaMigration
|
11
13
|
ActiveRecord::InternalMetadata
|
@@ -15,25 +17,24 @@ module Seedie
|
|
15
17
|
ActionText::RichText
|
16
18
|
ActionMailbox::InboundEmail
|
17
19
|
ActionText::EncryptedRichText
|
18
|
-
]
|
20
|
+
].freeze
|
19
21
|
|
20
22
|
source_root File.expand_path("templates", __dir__)
|
21
23
|
|
22
24
|
class_option :blank, type: :boolean, default: false, desc: "Generate a blank seedie.yml with examples"
|
23
25
|
class_option :excluded_models, type: :array, default: [], desc: "Models to exclude from seedie.yml"
|
24
|
-
class_option :include_only_models, type: :array, default: [],
|
25
|
-
|
26
|
-
|
26
|
+
class_option :include_only_models, type: :array, default: [],
|
27
|
+
desc: "Models to be specifically included in seedie.yml. This will ignore all other models."
|
27
28
|
|
28
29
|
desc "Creates a seedie.yml for your application."
|
29
|
-
def generate_seedie_file(output =
|
30
|
+
def generate_seedie_file(output = $stdout)
|
30
31
|
if options[:include_only_models].present? && options[:excluded_models].present?
|
31
|
-
raise ArgumentError, "Cannot use both --include_only_models and --excluded_models together."
|
32
|
+
raise ArgumentError, "Cannot use both --include_only_models and --excluded_models together."
|
32
33
|
end
|
33
|
-
|
34
|
+
|
34
35
|
# This needs to be generated before anything else.
|
35
36
|
template "seedie_initializer.rb", "config/initializers/seedie.rb"
|
36
|
-
|
37
|
+
|
37
38
|
@excluded_models = options[:excluded_models] + EXCLUDED_MODELS
|
38
39
|
@output = output
|
39
40
|
|
@@ -42,7 +43,7 @@ module Seedie
|
|
42
43
|
else
|
43
44
|
Rails.application.eager_load! # Load all models. This is required!!
|
44
45
|
|
45
|
-
@models =
|
46
|
+
@models = collect_models
|
46
47
|
@models_config = build_models_config
|
47
48
|
template "seedie.yml", "config/seedie.yml"
|
48
49
|
end
|
@@ -57,9 +58,8 @@ module Seedie
|
|
57
58
|
|
58
59
|
output_warning_for_extra_models(models)
|
59
60
|
|
60
|
-
models.
|
61
|
+
models.each_with_object({}) do |model, config|
|
61
62
|
config[model.name.underscore] = model_configuration(model)
|
62
|
-
config
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
@@ -90,12 +90,12 @@ module Seedie
|
|
90
90
|
if polymorphic_types.include?(column.name) || foreign_keys.include?(column.name)
|
91
91
|
next
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
# Adding default columns to default_columns
|
95
95
|
if column.default.present? || column.default_function.present?
|
96
96
|
default_columns << column
|
97
|
-
elsif column.null == false ||
|
98
|
-
|
97
|
+
elsif column.null == false || presence_validator?(model, column.name)
|
98
|
+
# Only add to active if its required or has presence validator
|
99
99
|
active_columns << column
|
100
100
|
else
|
101
101
|
disabled_columns << column
|
@@ -104,7 +104,7 @@ module Seedie
|
|
104
104
|
|
105
105
|
# Add atleast one column to active columns
|
106
106
|
active_columns << disabled_columns.pop if active_columns.empty? && disabled_columns.present?
|
107
|
-
|
107
|
+
|
108
108
|
# Disable all default columns
|
109
109
|
disabled_columns += default_columns
|
110
110
|
|
@@ -115,14 +115,13 @@ module Seedie
|
|
115
115
|
end
|
116
116
|
|
117
117
|
def active_columns_configuration(model, columns)
|
118
|
-
columns.
|
118
|
+
columns.each_with_object({}) do |column, config|
|
119
119
|
validations = model.validators_on(column.name)
|
120
120
|
config[column.name] = if validations.present?
|
121
121
|
FieldValues::FakerBuilder.new(column.name, column, validations).build_faker_constant
|
122
122
|
else
|
123
123
|
FieldValues::FakeValue.new(column.name, column).generate_fake_value
|
124
124
|
end
|
125
|
-
config
|
126
125
|
end
|
127
126
|
end
|
128
127
|
|
@@ -135,7 +134,7 @@ module Seedie
|
|
135
134
|
"associations" => {
|
136
135
|
"belongs_to" => belongs_to_associations_configuration(model),
|
137
136
|
"has_one" => {}, # TODO: Add has_one associations
|
138
|
-
"has_many" => {}
|
137
|
+
"has_many" => {} # TODO: Add has_many associations
|
139
138
|
}
|
140
139
|
}
|
141
140
|
end
|
@@ -147,17 +146,16 @@ module Seedie
|
|
147
146
|
|
148
147
|
unique_indexes = model.connection.indexes(model.table_name).select(&:unique).flat_map(&:columns)
|
149
148
|
|
150
|
-
belongs_to_associations.
|
149
|
+
belongs_to_associations.each_with_object({}) do |association, config|
|
151
150
|
if association.polymorphic?
|
152
151
|
config[association.name.to_s] = set_polymorphic_association_config(model, association)
|
153
152
|
else
|
154
153
|
association_has_unique_index = unique_indexes.include?(association.foreign_key.to_s)
|
155
154
|
config[association.name.to_s] = association_has_unique_index ? "unique" : "random"
|
156
155
|
end
|
157
|
-
config
|
158
156
|
end
|
159
|
-
end
|
160
|
-
|
157
|
+
end
|
158
|
+
|
161
159
|
def set_polymorphic_association_config(model, association)
|
162
160
|
{
|
163
161
|
"polymorphic" => find_polymorphic_types(model, association.name),
|
@@ -165,12 +163,12 @@ module Seedie
|
|
165
163
|
}
|
166
164
|
end
|
167
165
|
|
168
|
-
def
|
166
|
+
def presence_validator?(model, column_name)
|
169
167
|
model.validators_on(column_name).any? { |v| v.kind == :presence }
|
170
168
|
end
|
171
169
|
|
172
|
-
def
|
173
|
-
@
|
170
|
+
def collect_models
|
171
|
+
@collect_models ||= begin
|
174
172
|
all_models = ActiveRecord::Base.descendants
|
175
173
|
|
176
174
|
if options[:include_only_models].present?
|
@@ -179,10 +177,10 @@ module Seedie
|
|
179
177
|
|
180
178
|
all_models.reject do |model|
|
181
179
|
@excluded_models.include?(model.name) || # Excluded Reserved Models
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
180
|
+
model.abstract_class? || # Excluded Abstract Models
|
181
|
+
model.table_exists? == false || # Excluded Models without tables
|
182
|
+
model.name.blank? || # Excluded Anonymous Models
|
183
|
+
model.name.start_with?("HABTM_") # Excluded HABTM Models
|
186
184
|
end
|
187
185
|
end
|
188
186
|
end
|
@@ -4,5 +4,19 @@ Seedie.configure do |config|
|
|
4
4
|
# config.default_count = 10
|
5
5
|
|
6
6
|
config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
|
7
|
+
# Model-Specific Custom Attributes
|
8
|
+
#
|
9
|
+
# Use the prepare_custom_attributes_for method to initialize the custom_attributes hash
|
10
|
+
# for the specified models. This ensures that you can safely set model-specific custom
|
11
|
+
# attributes without encountering NoMethodError.
|
12
|
+
#
|
13
|
+
# Example:
|
14
|
+
# config.prepare_custom_attributes_for :user, :account
|
15
|
+
#
|
16
|
+
# Now you can set custom attributes for these models:
|
17
|
+
# config.custom_attributes[:user][:email] = "user@example.com"
|
18
|
+
# config.custom_attributes[:account][:name] = "{{Faker::Business.name}}"
|
19
|
+
#
|
7
20
|
# Add more custom attributes here
|
8
|
-
|
21
|
+
#
|
22
|
+
end
|
@@ -1,44 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
DEFAULT_COUNT = 1
|
6
|
-
INDEX = 0
|
4
|
+
module Associations
|
5
|
+
class BaseAssociation
|
6
|
+
include Reporters::Reportable
|
7
7
|
|
8
|
-
|
8
|
+
DEFAULT_COUNT = 1
|
9
|
+
INDEX = 0
|
9
10
|
|
10
|
-
|
11
|
-
@record = record
|
12
|
-
@model = model
|
13
|
-
@association_config = association_config
|
14
|
-
@reporters = reporters
|
11
|
+
attr_reader :record, :model, :association_config, :reporters
|
15
12
|
|
16
|
-
|
17
|
-
|
13
|
+
def initialize(record, model, association_config, reporters = [])
|
14
|
+
@record = record
|
15
|
+
@model = model
|
16
|
+
@association_config = association_config
|
17
|
+
@reporters = reporters
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
end
|
19
|
+
add_observers(@reporters)
|
20
|
+
end
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def generate_associations
|
23
|
+
raise NotImplementedError
|
24
|
+
end
|
26
25
|
|
27
|
-
|
26
|
+
def generate_association
|
27
|
+
raise NotImplementedError
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
return config if only_count_given?(config)
|
31
|
-
return config["count"] if config["count"].present?
|
30
|
+
private
|
32
31
|
|
33
|
-
|
34
|
-
|
32
|
+
def get_association_count(config)
|
33
|
+
return config if only_count_given?(config)
|
34
|
+
return config["count"] if config["count"].present?
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
DEFAULT_COUNT
|
37
|
+
end
|
38
|
+
|
39
|
+
def only_count_given?(config)
|
40
|
+
config.is_a?(Numeric) || config.is_a?(String)
|
41
|
+
end
|
39
42
|
|
40
|
-
|
41
|
-
|
43
|
+
def generate_associated_field(id, association_name)
|
44
|
+
{ association_name.to_s => id }
|
45
|
+
end
|
42
46
|
end
|
43
47
|
end
|
44
|
-
end
|
48
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module Associations
|
3
5
|
class BelongsTo < BaseAssociation
|
@@ -11,7 +13,7 @@ module Seedie
|
|
11
13
|
|
12
14
|
def generate_associations
|
13
15
|
return if association_config["belongs_to"].nil?
|
14
|
-
|
16
|
+
|
15
17
|
report(:belongs_to_start)
|
16
18
|
|
17
19
|
association_config["belongs_to"].each do |association_name, association_config|
|
@@ -29,7 +31,7 @@ module Seedie
|
|
29
31
|
|
30
32
|
private
|
31
33
|
|
32
|
-
def handle_association_config_type(reflection,
|
34
|
+
def handle_association_config_type(reflection, _association_name, association_config)
|
33
35
|
if reflection.polymorphic?
|
34
36
|
handle_polymorphic_config_type(reflection, association_config)
|
35
37
|
else
|
@@ -38,7 +40,6 @@ module Seedie
|
|
38
40
|
handle_strategy(klass, reflection, strategy)
|
39
41
|
end
|
40
42
|
end
|
41
|
-
|
42
43
|
|
43
44
|
def handle_polymorphic_config_type(reflection, association_config)
|
44
45
|
type_name = get_polymorphic_class_name(association_config["polymorphic"])
|
@@ -48,7 +49,7 @@ module Seedie
|
|
48
49
|
|
49
50
|
handle_strategy(klass, reflection, strategy)
|
50
51
|
end
|
51
|
-
|
52
|
+
|
52
53
|
# Handles the strategy for belongs_to associations
|
53
54
|
# For polymorphic reflection, we might not add a strategy
|
54
55
|
# so we need to default it to random
|
@@ -89,27 +90,25 @@ module Seedie
|
|
89
90
|
|
90
91
|
def handle_new_config_type(klass, reflection)
|
91
92
|
report(:belongs_to_associations, name: klass.to_s, parent_name: model.to_s)
|
92
|
-
|
93
|
+
|
93
94
|
new_associated_record = generate_association(klass, {}, INDEX)
|
94
95
|
associated_field_set.merge!(generate_associated_field(new_associated_record.id, reflection.foreign_key))
|
95
96
|
end
|
96
97
|
|
97
98
|
def handle_other_config_type(klass, reflection, association_config)
|
98
99
|
report(:belongs_to_associations, name: klass.to_s, parent_name: model.to_s)
|
99
|
-
|
100
|
+
|
100
101
|
new_associated_record = generate_association(klass, association_config, INDEX)
|
101
102
|
associated_field_set.merge!(generate_associated_field(new_associated_record.id, reflection.foreign_key))
|
102
103
|
end
|
103
104
|
|
104
105
|
def get_type(association_config)
|
105
|
-
if association_config.is_a?(String)
|
106
|
-
raise InvalidAssociationConfigError, "Invalid association config"
|
107
|
-
|
108
|
-
return association_config
|
109
|
-
else
|
110
|
-
association_config
|
106
|
+
if association_config.is_a?(String) && !%w[random new unique].include?(association_config)
|
107
|
+
raise InvalidAssociationConfigError, "Invalid association config"
|
111
108
|
end
|
109
|
+
|
110
|
+
association_config
|
112
111
|
end
|
113
112
|
end
|
114
113
|
end
|
115
|
-
end
|
114
|
+
end
|