seedie 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +91 -9
- data/CHANGELOG.md +71 -0
- data/Gemfile +4 -1
- data/README.md +7 -2
- data/Rakefile +1 -1
- data/lib/generators/seedie/install_generator.rb +28 -30
- data/lib/generators/seedie/templates/seedie_initializer.rb +1 -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 +3 -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 +26 -35
- 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 +27 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b22e6527a57385dbb0b5fc616cf632b84550a3ce8237f286a64b931670bd34a
|
4
|
+
data.tar.gz: edeb838fb02090e02a9434260e08f6cb376b4b9d1e16a75e7dc3bd97c6d0a14a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85ae796fa177098ac721addf4e4da3a10f12e26613318fb8f3fe8d126d008ec3de310115b877848fa607a91ad69e51c7c10a531322100050e76e45e89a2f2b72
|
7
|
+
data.tar.gz: cb9fe8d4333e9a80f32fc33bab4e8fbe5066832121ab5c49bb3701691e8a07147115819f13ee41e58eb91d90d03a247e6bfe5f5280cc600b4c307c251b0e98fc
|
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,74 @@
|
|
1
|
+
## Version 0.4.0
|
2
|
+
|
3
|
+
### New Features
|
4
|
+
|
5
|
+
#### Added HasAndBelongsToMany Association support
|
6
|
+
|
7
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/43)
|
8
|
+
|
9
|
+
You can now add `has_and_belongs_to_many` associations to your seedie.yml file:
|
10
|
+
|
11
|
+
```yaml
|
12
|
+
has_and_belongs_to_many:
|
13
|
+
posts:
|
14
|
+
count: 5
|
15
|
+
attributes:
|
16
|
+
title: "{{Faker::Lorem.sentence}}"
|
17
|
+
body: "{{Faker::Lorem.paragraph}}"
|
18
|
+
```
|
19
|
+
|
20
|
+
#### Added the ability to generate nested BelongsTo Associations
|
21
|
+
|
22
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/34)
|
23
|
+
|
24
|
+
Before, Models had no ability to add assocations to the child_model.
|
25
|
+
|
26
|
+
```yaml
|
27
|
+
parent_model:
|
28
|
+
attributes:
|
29
|
+
some_attribute: 'hello'
|
30
|
+
associations:
|
31
|
+
has_many:
|
32
|
+
child_model: 2
|
33
|
+
```
|
34
|
+
|
35
|
+
Now you can add associations to the child_model:
|
36
|
+
|
37
|
+
```yaml
|
38
|
+
parent_model:
|
39
|
+
attributes:
|
40
|
+
some_attribute: 'hello'
|
41
|
+
associations:
|
42
|
+
has_many:
|
43
|
+
child_model:
|
44
|
+
attributes:
|
45
|
+
title: 'some title'
|
46
|
+
associations:
|
47
|
+
belongs_to:
|
48
|
+
another_parent_model: 'random'
|
49
|
+
```
|
50
|
+
|
51
|
+
### Enhancements
|
52
|
+
|
53
|
+
#### Complete Rubocop Integration with CI
|
54
|
+
|
55
|
+
* [Rubocop Integration](https://github.com/keshavbiswa/seedie/pull/33) by [@giovannism20](https://github.com/giovannism20)
|
56
|
+
* Rubocop cleanups:
|
57
|
+
- https://github.com/keshavbiswa/seedie/pull/37
|
58
|
+
- https://github.com/keshavbiswa/seedie/pull/38
|
59
|
+
- https://github.com/keshavbiswa/seedie/pull/39
|
60
|
+
- https://github.com/keshavbiswa/seedie/pull/40
|
61
|
+
- https://github.com/keshavbiswa/seedie/pull/41
|
62
|
+
- https://github.com/keshavbiswa/seedie/pull/42
|
63
|
+
|
64
|
+
#### Added Zeitwerk Support
|
65
|
+
|
66
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/35)
|
67
|
+
|
68
|
+
#### Added Rails 7.1 Support
|
69
|
+
|
70
|
+
* [GitHub PR](https://github.com/keshavbiswa/seedie/pull/36)
|
71
|
+
|
1
72
|
## Version 0.3.0
|
2
73
|
|
3
74
|
### 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.
|
@@ -144,6 +144,11 @@ models:
|
|
144
144
|
post_metadatum:
|
145
145
|
attributes:
|
146
146
|
seo_text: "{{Faker::Lorem.paragraph}}"
|
147
|
+
has_and_belongs_to_many:
|
148
|
+
tags:
|
149
|
+
count: 3
|
150
|
+
attributes:
|
151
|
+
name: "{{Faker::Lorem.word}}"
|
147
152
|
disabled_fields: []
|
148
153
|
comment:
|
149
154
|
attributes:
|
@@ -165,7 +170,7 @@ In this file:
|
|
165
170
|
- Additionally, we can use placeholders like `{{Faker::Internet.email}}` to generate dynamic and unique data for each record using Faker.
|
166
171
|
- We can also specify an array of values that can be picked from randomly or sequentially using the `values` and `pick_strategy` options.
|
167
172
|
- `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 `
|
173
|
+
- `associations` specify how associated models should be generated. Here, `has_many`, `belongs_to`, `has_one` and `has_and_belongs_to_many` are supported.
|
169
174
|
- The specified number for `has_many` represents the number of associated records to create.
|
170
175
|
- 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
176
|
- If attributes are specified under an association, those attributes will be used when creating the associated record(s)
|
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
|
@@ -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
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Seedie
|
4
|
+
module Associations
|
5
|
+
class HasAndBelongsToMany < BaseAssociation
|
6
|
+
def generate_associations
|
7
|
+
return if association_config["has_and_belongs_to_many"].nil?
|
8
|
+
|
9
|
+
report(:has_and_belongs_to_many_start)
|
10
|
+
association_config["has_and_belongs_to_many"].each do |association_name, association_config|
|
11
|
+
association_class = association_name.to_s.classify.constantize
|
12
|
+
count = get_association_count(association_config)
|
13
|
+
config = only_count_given?(association_config) ? {} : association_config
|
14
|
+
|
15
|
+
report(:associated_records, name: association_name, count: count, parent_name: model.to_s)
|
16
|
+
count.times do |index|
|
17
|
+
field_values_set = FieldValuesSet.new(association_class, config, index).generate_field_values_with_associations
|
18
|
+
record_creator = Model::Creator.new(record.send(association_name), reporters)
|
19
|
+
|
20
|
+
record_creator.create!(field_values_set)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,18 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Seedie
|
2
4
|
module Associations
|
3
5
|
class HasMany < BaseAssociation
|
4
6
|
def generate_associations
|
5
7
|
return if association_config["has_many"].nil?
|
6
|
-
|
8
|
+
|
7
9
|
report(:has_many_start)
|
8
10
|
association_config["has_many"].each do |association_name, association_config|
|
9
11
|
association_class = association_name.to_s.classify.constantize
|
10
12
|
count = get_association_count(association_config)
|
11
13
|
config = only_count_given?(association_config) ? {} : association_config
|
12
|
-
|
14
|
+
|
13
15
|
report(:associated_records, name: association_name, count: count, parent_name: model.to_s)
|
14
16
|
count.times do |index|
|
15
|
-
field_values_set = FieldValuesSet.new(association_class, config, index).
|
17
|
+
field_values_set = FieldValuesSet.new(association_class, config, index).generate_field_values_with_associations
|
16
18
|
record_creator = Model::Creator.new(record.send(association_name), reporters)
|
17
19
|
|
18
20
|
record_creator.create!(field_values_set)
|
@@ -21,4 +23,4 @@ module Seedie
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
end
|
24
|
-
end
|
26
|
+
end
|