colonization 0.3.2 → 0.3.3
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/README.md +1 -1
- data/lib/colonization/{model_instaces_repository.rb → model_instances_repository.rb} +0 -0
- data/lib/colonization/ship.rb +22 -17
- data/lib/colonization/strategies/default/colonizer.rb +105 -0
- data/lib/colonization/strategies/default/model_relation.rb +38 -0
- data/lib/colonization/strategies/fixed/colonizer.rb +105 -0
- data/lib/colonization/strategies/fixed/model_relation.rb +36 -0
- data/lib/colonization/strategies/model_field.rb +45 -0
- data/lib/colonization/values_generator.rb +44 -4
- data/lib/colonization/version.rb +1 -1
- metadata +8 -7
- data/lib/colonization/colonizer.rb +0 -111
- data/lib/colonization/model_entity.rb +0 -11
- data/lib/colonization/model_field.rb +0 -45
- data/lib/colonization/model_relation.rb +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7b238a1f52eb0e3154bc44755b8a9b9412824b4
|
4
|
+
data.tar.gz: 14c8ec0adc365c5747e15d35e66f9cb60c24ced9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0eaaa8beb2cbefd30416cea77350558864f7f1cde56c8eee70eb05d2f4cdc041385244e9eae76f840b3d0ee92396c2f5d397372b18c35df49d986031ef97fddd
|
7
|
+
data.tar.gz: 43ef128aae6cfddfb2f702cf5ae29c0bb6bdb14bb975f112083b52261444abbff7dda7046ed51bc699913f6a8953d08c32d2d17cc5d1bf6a62c584a39b8af737
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Colonization
|
2
2
|
|
3
|
-
Gem for development database populating. Data seeding regarding to your models fields, their types and validations. It is in development now - only presence and acceptance validations currently supported.
|
3
|
+
Gem for development database populating. Data seeding regarding to your models fields, their types and validations. It is in development now - only presence, exclusion and acceptance validations currently supported.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
File without changes
|
data/lib/colonization/ship.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
require 'colonization/investigator'
|
2
|
-
require 'colonization/colonizer'
|
2
|
+
require 'colonization/strategies/default/colonizer'
|
3
|
+
require 'colonization/strategies/fixed/colonizer'
|
3
4
|
|
4
5
|
class Colonization::Ship
|
5
6
|
|
6
|
-
attr_reader :
|
7
|
+
attr_reader :model_colonizers
|
7
8
|
|
8
9
|
def initialize
|
9
|
-
models =
|
10
|
-
@
|
10
|
+
models = Colonization::Investigator.new.investigate
|
11
|
+
@model_colonizers = get_model_colonizers(models)
|
11
12
|
end
|
12
13
|
|
13
14
|
def colonize
|
@@ -18,29 +19,33 @@ class Colonization::Ship
|
|
18
19
|
private
|
19
20
|
|
20
21
|
def clear_db
|
21
|
-
|
22
|
-
|
22
|
+
model_colonizers.each do |model_colnizer|
|
23
|
+
model_colnizer.clear_table
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
26
27
|
def populate
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
model_colonizers.each do |model_colonizer|
|
29
|
+
model_colonizer.print_action
|
30
|
+
model_colonizer.colonize
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
|
-
def
|
34
|
-
|
35
|
-
|
34
|
+
def get_model_colonizers(models)
|
35
|
+
models.map do |model|
|
36
|
+
colonizer_class.new(model)
|
37
|
+
end
|
36
38
|
end
|
37
39
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
def colonizer_class
|
41
|
+
case Colonization.configuration.seed_type
|
42
|
+
when :default
|
43
|
+
Colonization::Strategies::Default::Colonizer
|
44
|
+
when :fixed
|
45
|
+
Colonization::Strategies::Fixed::Colonizer
|
46
|
+
else
|
47
|
+
raise UnknownStrategyError
|
42
48
|
end
|
43
|
-
result
|
44
49
|
end
|
45
50
|
|
46
51
|
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'colonization/strategies/model_field'
|
2
|
+
require 'colonization/strategies/default/model_relation'
|
3
|
+
require 'colonization/configuration'
|
4
|
+
|
5
|
+
module Colonization::Strategies::Default
|
6
|
+
class Colonizer
|
7
|
+
|
8
|
+
attr_accessor :model
|
9
|
+
|
10
|
+
def initialize(model)
|
11
|
+
@model = model
|
12
|
+
end
|
13
|
+
|
14
|
+
def print_action
|
15
|
+
puts "colonize #{model}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def colonize(times = nil)
|
19
|
+
res = []
|
20
|
+
(times || model_instances_number).times do
|
21
|
+
res << model.create!(params_with_values)
|
22
|
+
end
|
23
|
+
res
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear_table
|
27
|
+
model.destroy_all if clear_table?
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def params_with_values
|
33
|
+
res = {}
|
34
|
+
|
35
|
+
model_fields_objects.each do |field_object|
|
36
|
+
res[field_object.name] = field_object.value
|
37
|
+
end
|
38
|
+
|
39
|
+
model_relation_objects.each do |field_object|
|
40
|
+
res[field_object.name] = field_object.value
|
41
|
+
end
|
42
|
+
|
43
|
+
res
|
44
|
+
end
|
45
|
+
|
46
|
+
def model_fields
|
47
|
+
model.column_names - unnecessary_fields
|
48
|
+
end
|
49
|
+
|
50
|
+
def model_relations_strings
|
51
|
+
all_relations.map do |relation|
|
52
|
+
"#{relation.name}_id"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def all_relations
|
57
|
+
model.reflect_on_all_associations
|
58
|
+
end
|
59
|
+
|
60
|
+
def belongs_to_relations
|
61
|
+
model.reflect_on_all_associations(:belongs_to) || []
|
62
|
+
end
|
63
|
+
|
64
|
+
def has_many_relations
|
65
|
+
model.reflect_on_all_associations(:has_many) || []
|
66
|
+
end
|
67
|
+
|
68
|
+
def unnecessary_fields
|
69
|
+
["created_at", "updated_at", "id"] + model_relations_strings
|
70
|
+
end
|
71
|
+
|
72
|
+
def model_fields_objects
|
73
|
+
res = []
|
74
|
+
model_fields.each do |field|
|
75
|
+
res << Colonization::Strategies::ModelField.new(model, field, values_generator)
|
76
|
+
end
|
77
|
+
res
|
78
|
+
end
|
79
|
+
|
80
|
+
def model_relation_objects
|
81
|
+
res = []
|
82
|
+
has_many_relations.each do |reflection|
|
83
|
+
res << Colonization::Strategies::Default::ModelRelation.new(reflection)
|
84
|
+
end
|
85
|
+
res
|
86
|
+
end
|
87
|
+
|
88
|
+
def model_instances_number
|
89
|
+
if clear_table?
|
90
|
+
Colonization.configuration.count_of_instances_to_seed - model.count
|
91
|
+
else
|
92
|
+
Colonization.configuration.count_of_instances_to_seed
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def values_generator
|
97
|
+
nil #TODO config[model][values_generator] || cofig[values_generator]
|
98
|
+
end
|
99
|
+
|
100
|
+
def clear_table?
|
101
|
+
Colonization.configuration.clear_table
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'colonization/model_instances_repository'
|
2
|
+
require 'colonization/strategies/default/colonizer'
|
3
|
+
|
4
|
+
module Colonization::Strategies::Default
|
5
|
+
class ModelRelation
|
6
|
+
|
7
|
+
attr_accessor :reflection
|
8
|
+
|
9
|
+
def initialize(reflection)
|
10
|
+
@reflection = reflection
|
11
|
+
end
|
12
|
+
|
13
|
+
def name
|
14
|
+
reflection.name
|
15
|
+
end
|
16
|
+
|
17
|
+
def value
|
18
|
+
found_instances = Colonization::ModelInstancesRepository.new.get_instance_without_relation(
|
19
|
+
model: reflection_class,
|
20
|
+
relation: reflection_owner,
|
21
|
+
limit: Colonization.configuration.seed_index
|
22
|
+
)
|
23
|
+
count_to_colonize = Colonization.configuration.seed_index - (found_instances ? found_instances.count : 0)
|
24
|
+
Colonization::Strategies::Default::Colonizer.new(reflection_class).colonize(count_to_colonize)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def reflection_class
|
30
|
+
reflection.class_name.constantize
|
31
|
+
end
|
32
|
+
|
33
|
+
def reflection_owner
|
34
|
+
reflection.active_record
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'colonization/strategies/model_field'
|
2
|
+
require 'colonization/strategies/fixed/model_relation'
|
3
|
+
require 'colonization/configuration'
|
4
|
+
|
5
|
+
module Colonization::Strategies::Fixed
|
6
|
+
class Colonizer
|
7
|
+
|
8
|
+
attr_accessor :model
|
9
|
+
|
10
|
+
def initialize(model)
|
11
|
+
@model = model
|
12
|
+
end
|
13
|
+
|
14
|
+
def print_action
|
15
|
+
puts "colonize #{model}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def colonize(times = nil)
|
19
|
+
res = []
|
20
|
+
(times || model_instances_number).times do
|
21
|
+
res << model.create!(params_with_values)
|
22
|
+
end
|
23
|
+
res
|
24
|
+
end
|
25
|
+
|
26
|
+
def clear_table
|
27
|
+
model.destroy_all if clear_table?
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def params_with_values
|
33
|
+
res = {}
|
34
|
+
|
35
|
+
model_fields_objects.each do |field_object|
|
36
|
+
res[field_object.name] = field_object.value
|
37
|
+
end
|
38
|
+
|
39
|
+
model_relation_objects.each do |field_object|
|
40
|
+
res[field_object.name] = field_object.value
|
41
|
+
end
|
42
|
+
|
43
|
+
res
|
44
|
+
end
|
45
|
+
|
46
|
+
def model_fields
|
47
|
+
model.column_names - unnecessary_fields
|
48
|
+
end
|
49
|
+
|
50
|
+
def model_relations_strings
|
51
|
+
all_relations.map do |relation|
|
52
|
+
"#{relation.name}_id"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def all_relations
|
57
|
+
model.reflect_on_all_associations
|
58
|
+
end
|
59
|
+
|
60
|
+
def belongs_to_relations
|
61
|
+
model.reflect_on_all_associations(:belongs_to) || []
|
62
|
+
end
|
63
|
+
|
64
|
+
def has_many_relations
|
65
|
+
model.reflect_on_all_associations(:has_many) || []
|
66
|
+
end
|
67
|
+
|
68
|
+
def unnecessary_fields
|
69
|
+
["created_at", "updated_at", "id"] + model_relations_strings
|
70
|
+
end
|
71
|
+
|
72
|
+
def model_fields_objects
|
73
|
+
res = []
|
74
|
+
model_fields.each do |field|
|
75
|
+
res << Colonization::Strategies::ModelField.new(model, field, values_generator)
|
76
|
+
end
|
77
|
+
res
|
78
|
+
end
|
79
|
+
|
80
|
+
def model_relation_objects
|
81
|
+
res = []
|
82
|
+
belongs_to_relations.each do |reflection|
|
83
|
+
res << Colonization::Strategies::Fixed::ModelRelation.new(reflection)
|
84
|
+
end
|
85
|
+
res
|
86
|
+
end
|
87
|
+
|
88
|
+
def model_instances_number
|
89
|
+
if clear_table?
|
90
|
+
Colonization.configuration.count_of_instances_to_seed - model.count
|
91
|
+
else
|
92
|
+
Colonization.configuration.count_of_instances_to_seed
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def values_generator
|
97
|
+
nil #TODO config[model][values_generator] || cofig[values_generator]
|
98
|
+
end
|
99
|
+
|
100
|
+
def clear_table?
|
101
|
+
Colonization.configuration.clear_table
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'colonization/model_instances_repository'
|
2
|
+
require 'colonization/strategies/fixed/colonizer'
|
3
|
+
|
4
|
+
module Colonization::Strategies::Fixed
|
5
|
+
class ModelRelation
|
6
|
+
|
7
|
+
attr_accessor :reflection
|
8
|
+
|
9
|
+
def initialize(reflection)
|
10
|
+
@reflection = reflection
|
11
|
+
end
|
12
|
+
|
13
|
+
def name
|
14
|
+
reflection.name
|
15
|
+
end
|
16
|
+
|
17
|
+
def value
|
18
|
+
instance = Colonization::ModelInstancesRepository.new.get_instance(model: reflection_class)
|
19
|
+
unless instance
|
20
|
+
instance = Colonization::Strategies::Fixed::Colonizer.new(reflection_class).colonize
|
21
|
+
end
|
22
|
+
instance
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def reflection_class
|
28
|
+
reflection.class_name.constantize
|
29
|
+
end
|
30
|
+
|
31
|
+
def reflection_owner
|
32
|
+
reflection.active_record
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'colonization/values_generator'
|
2
|
+
|
3
|
+
module Colonization::Strategies
|
4
|
+
class ModelField
|
5
|
+
|
6
|
+
attr_reader :model, :field
|
7
|
+
|
8
|
+
def initialize(model, field, values_generator)
|
9
|
+
@model = model
|
10
|
+
@field = field
|
11
|
+
@values_generator = values_generator
|
12
|
+
end
|
13
|
+
|
14
|
+
def name
|
15
|
+
field
|
16
|
+
end
|
17
|
+
|
18
|
+
def value
|
19
|
+
values_generator.call
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def values_generator
|
25
|
+
if @values_generator.class == Proc
|
26
|
+
@values_generator
|
27
|
+
else
|
28
|
+
(@values_generator || default_values_generator).new(field_type, validators)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def default_values_generator
|
33
|
+
Colonization::ValuesGenerator
|
34
|
+
end
|
35
|
+
|
36
|
+
def validators
|
37
|
+
model.validators_on(field)
|
38
|
+
end
|
39
|
+
|
40
|
+
def field_type
|
41
|
+
model.columns_hash[field].type
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -5,7 +5,7 @@ class Colonization::ValuesGenerator
|
|
5
5
|
|
6
6
|
attr_reader :validators, :validators_objects
|
7
7
|
|
8
|
-
def initialize(type, validators
|
8
|
+
def initialize(type, validators)
|
9
9
|
@config = { validations: {}, type: type }
|
10
10
|
@validators = validators
|
11
11
|
set_custom_validators_objects
|
@@ -58,6 +58,8 @@ class Colonization::ValuesGenerator
|
|
58
58
|
raise DisaparateValidatorError
|
59
59
|
when :presence
|
60
60
|
return Faker::Number.between(1, 2)
|
61
|
+
when :exclusion
|
62
|
+
return ((1..100).to_a - validator.options[:in]).sample
|
61
63
|
else
|
62
64
|
raise validator.inspect
|
63
65
|
end
|
@@ -70,6 +72,11 @@ class Colonization::ValuesGenerator
|
|
70
72
|
case validator.type
|
71
73
|
when :acceptance
|
72
74
|
raise DisaparateValidatorError
|
75
|
+
when :exclusion
|
76
|
+
begin
|
77
|
+
num = Faker::Number.decimal(2)
|
78
|
+
end while validator.options[:in].include?(num)
|
79
|
+
return num
|
73
80
|
else
|
74
81
|
raise validator.inspect
|
75
82
|
end
|
@@ -83,6 +90,11 @@ class Colonization::ValuesGenerator
|
|
83
90
|
when :acceptance
|
84
91
|
return validator.options[:accept].sample if validator.options[:accept].present?
|
85
92
|
return "1"
|
93
|
+
when :exclusion
|
94
|
+
begin
|
95
|
+
word = Faker::Lorem.word
|
96
|
+
end while validator.options[:in].include?(word)
|
97
|
+
return word
|
86
98
|
else
|
87
99
|
raise validator.inspect
|
88
100
|
end
|
@@ -95,6 +107,11 @@ class Colonization::ValuesGenerator
|
|
95
107
|
case validator.type
|
96
108
|
when :acceptance
|
97
109
|
raise DisaparateValidatorError
|
110
|
+
when :exclusion
|
111
|
+
begin
|
112
|
+
sentence = Faker::Lorem.sentence
|
113
|
+
end while validator.options[:in].include?(sentence)
|
114
|
+
return sentence
|
98
115
|
else
|
99
116
|
raise validator.inspect
|
100
117
|
end
|
@@ -102,11 +119,16 @@ class Colonization::ValuesGenerator
|
|
102
119
|
end
|
103
120
|
|
104
121
|
def date_case
|
105
|
-
return Faker::
|
122
|
+
return Faker::Date.between(30.days.ago, Date.today) unless validators_objects.present?
|
106
123
|
validators_objects.each do |validator|
|
107
124
|
case validator.type
|
108
125
|
when :acceptance
|
109
126
|
raise DisaparateValidatorError
|
127
|
+
when :exclusion
|
128
|
+
begin
|
129
|
+
date = Faker::Date.between(30.days.ago, Date.today)
|
130
|
+
end while validator.options[:in].include?(date)
|
131
|
+
return date
|
110
132
|
else
|
111
133
|
raise validator.inspect
|
112
134
|
end
|
@@ -114,11 +136,16 @@ class Colonization::ValuesGenerator
|
|
114
136
|
end
|
115
137
|
|
116
138
|
def datetime_case
|
117
|
-
return Faker::Time.between(
|
139
|
+
return Faker::Time.between(30.days.ago, Date.today) unless validators_objects.present?
|
118
140
|
validators_objects.each do |validator|
|
119
141
|
case validator.type
|
120
142
|
when :acceptance
|
121
143
|
raise DisaparateValidatorError
|
144
|
+
when :exclusion
|
145
|
+
begin
|
146
|
+
datetime = Faker::Time.between(30.days.ago, Date.today)
|
147
|
+
end while validator.options[:in].include?(datetime)
|
148
|
+
return datetime
|
122
149
|
else
|
123
150
|
raise validator.inspect
|
124
151
|
end
|
@@ -131,6 +158,14 @@ class Colonization::ValuesGenerator
|
|
131
158
|
case validator.type
|
132
159
|
when :acceptance
|
133
160
|
return true
|
161
|
+
when :exclusion
|
162
|
+
if validator.options[:in].exclude?(false)
|
163
|
+
return false
|
164
|
+
elsif validator.options[:in].exclude?(true)
|
165
|
+
return true
|
166
|
+
else
|
167
|
+
return nil
|
168
|
+
end
|
134
169
|
else
|
135
170
|
raise validator.inspect
|
136
171
|
end
|
@@ -138,11 +173,16 @@ class Colonization::ValuesGenerator
|
|
138
173
|
end
|
139
174
|
|
140
175
|
def time_case
|
141
|
-
return Faker::Time.between(
|
176
|
+
return Faker::Time.between(30.days.ago, Date.today) unless validators_objects.present?
|
142
177
|
validators_objects.each do |validator|
|
143
178
|
case validator.type
|
144
179
|
when :acceptance
|
145
180
|
raise DisaparateValidatorError
|
181
|
+
when :exclusion
|
182
|
+
begin
|
183
|
+
time = Faker::Time.between(30.days.ago, Date.today)
|
184
|
+
end while validator.options[:in].include?(time)
|
185
|
+
return time
|
146
186
|
else
|
147
187
|
raise validator.inspect
|
148
188
|
end
|
data/lib/colonization/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colonization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denys Dvoriashyn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,14 +81,15 @@ files:
|
|
81
81
|
- bin/setup
|
82
82
|
- colonization.gemspec
|
83
83
|
- lib/colonization.rb
|
84
|
-
- lib/colonization/colonizer.rb
|
85
84
|
- lib/colonization/configuration.rb
|
86
85
|
- lib/colonization/investigator.rb
|
87
|
-
- lib/colonization/
|
88
|
-
- lib/colonization/model_field.rb
|
89
|
-
- lib/colonization/model_instaces_repository.rb
|
90
|
-
- lib/colonization/model_relation.rb
|
86
|
+
- lib/colonization/model_instances_repository.rb
|
91
87
|
- lib/colonization/ship.rb
|
88
|
+
- lib/colonization/strategies/default/colonizer.rb
|
89
|
+
- lib/colonization/strategies/default/model_relation.rb
|
90
|
+
- lib/colonization/strategies/fixed/colonizer.rb
|
91
|
+
- lib/colonization/strategies/fixed/model_relation.rb
|
92
|
+
- lib/colonization/strategies/model_field.rb
|
92
93
|
- lib/colonization/validators/active_model/absence_validator.rb
|
93
94
|
- lib/colonization/validators/active_model/acceptance_validator.rb
|
94
95
|
- lib/colonization/validators/active_model/confirmation_validator.rb
|
@@ -1,111 +0,0 @@
|
|
1
|
-
require 'colonization/model_field'
|
2
|
-
require 'colonization/model_relation'
|
3
|
-
require 'colonization/configuration'
|
4
|
-
|
5
|
-
class Colonization::Colonizer
|
6
|
-
|
7
|
-
attr_accessor :model
|
8
|
-
|
9
|
-
def initialize(model)
|
10
|
-
@model = model
|
11
|
-
end
|
12
|
-
|
13
|
-
def print_action
|
14
|
-
puts "colonize #{model}"
|
15
|
-
end
|
16
|
-
|
17
|
-
def colonize(times = nil)
|
18
|
-
res = []
|
19
|
-
(times || model_instances_number).times do
|
20
|
-
res << model.create!(params_with_values)
|
21
|
-
end
|
22
|
-
res
|
23
|
-
end
|
24
|
-
|
25
|
-
def clear_table
|
26
|
-
model.destroy_all if clear_table?
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def params_with_values
|
32
|
-
res = {}
|
33
|
-
|
34
|
-
model_fields_objects.each do |field_object|
|
35
|
-
res[field_object.name] = field_object.value
|
36
|
-
end
|
37
|
-
|
38
|
-
model_relation_objects.each do |field_object|
|
39
|
-
res[field_object.name] = field_object.value
|
40
|
-
end
|
41
|
-
|
42
|
-
res
|
43
|
-
end
|
44
|
-
|
45
|
-
def model_fields
|
46
|
-
model.column_names - unnecessary_fields
|
47
|
-
end
|
48
|
-
|
49
|
-
def model_relations_strings
|
50
|
-
all_relations.map do |relation|
|
51
|
-
"#{relation.name}_id"
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def all_relations
|
56
|
-
model.reflect_on_all_associations
|
57
|
-
end
|
58
|
-
|
59
|
-
def belongs_to_relations
|
60
|
-
model.reflect_on_all_associations(:belongs_to) || []
|
61
|
-
end
|
62
|
-
|
63
|
-
def has_many_relations
|
64
|
-
model.reflect_on_all_associations(:has_many) || []
|
65
|
-
end
|
66
|
-
|
67
|
-
def unnecessary_fields
|
68
|
-
["created_at", "updated_at", "id"] + model_relations_strings
|
69
|
-
end
|
70
|
-
|
71
|
-
def model_fields_objects
|
72
|
-
res = []
|
73
|
-
model_fields.each do |field|
|
74
|
-
res << Colonization::ModelField.new(model, field, values_generator, strategy)
|
75
|
-
end
|
76
|
-
res
|
77
|
-
end
|
78
|
-
|
79
|
-
def model_relation_objects
|
80
|
-
res = []
|
81
|
-
needed_relations.each do |reflection|
|
82
|
-
res << Colonization::ModelRelation.new(reflection, strategy)
|
83
|
-
end
|
84
|
-
res
|
85
|
-
end
|
86
|
-
|
87
|
-
def needed_relations
|
88
|
-
strategy == :fixed ? belongs_to_relations : has_many_relations
|
89
|
-
end
|
90
|
-
|
91
|
-
def model_instances_number
|
92
|
-
if clear_table?
|
93
|
-
Colonization.configuration.count_of_instances_to_seed - model.count
|
94
|
-
else
|
95
|
-
Colonization.configuration.count_of_instances_to_seed
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def values_generator
|
100
|
-
nil #TODO config[model][values_generator] || cofig[values_generator]
|
101
|
-
end
|
102
|
-
|
103
|
-
def strategy
|
104
|
-
Colonization.configuration.seed_type
|
105
|
-
end
|
106
|
-
|
107
|
-
def clear_table?
|
108
|
-
Colonization.configuration.clear_table
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'colonization/model_entity'
|
2
|
-
require 'colonization/values_generator'
|
3
|
-
|
4
|
-
class Colonization::ModelField < Colonization::ModelEntity
|
5
|
-
|
6
|
-
attr_reader :model, :field, :strategy
|
7
|
-
|
8
|
-
def initialize(model, field, values_generator, strategy)
|
9
|
-
@model = model
|
10
|
-
@field = field
|
11
|
-
@values_generator = values_generator
|
12
|
-
@strategy = strategy
|
13
|
-
end
|
14
|
-
|
15
|
-
def name
|
16
|
-
field
|
17
|
-
end
|
18
|
-
|
19
|
-
def value
|
20
|
-
values_generator.call
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def values_generator
|
26
|
-
if @values_generator.class == Proc
|
27
|
-
@values_generator
|
28
|
-
else
|
29
|
-
(@values_generator || default_values_generator).new(field_type, validators, strategy)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def default_values_generator
|
34
|
-
Colonization::ValuesGenerator
|
35
|
-
end
|
36
|
-
|
37
|
-
def validators
|
38
|
-
model.validators_on(field)
|
39
|
-
end
|
40
|
-
|
41
|
-
def field_type
|
42
|
-
model.columns_hash[field].type
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'colonization/model_entity'
|
2
|
-
require 'colonization/model_instaces_repository'
|
3
|
-
require 'colonization/colonizer'
|
4
|
-
|
5
|
-
class Colonization::ModelRelation < Colonization::ModelEntity
|
6
|
-
|
7
|
-
attr_accessor :reflection, :strategy
|
8
|
-
|
9
|
-
def initialize(reflection, strategy)
|
10
|
-
@reflection = reflection
|
11
|
-
@strategy = strategy || {}
|
12
|
-
end
|
13
|
-
|
14
|
-
def name
|
15
|
-
reflection.name
|
16
|
-
end
|
17
|
-
|
18
|
-
def value
|
19
|
-
if strategy == :fixed #TODO: refactor this shit
|
20
|
-
generate_relation_value_with_fixed_strategy
|
21
|
-
else
|
22
|
-
generate_relation_value_with_default_strategy
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def generate_relation_value_with_fixed_strategy
|
29
|
-
instance = Colonization::ModelInstancesRepository.new.get_instance(model: reflection_class)
|
30
|
-
unless instance
|
31
|
-
instance = Colonization::Colonizer.new(reflection_class).colonize
|
32
|
-
end
|
33
|
-
instance
|
34
|
-
end
|
35
|
-
|
36
|
-
def generate_relation_value_with_default_strategy
|
37
|
-
found_instances = Colonization::ModelInstancesRepository.new.get_instance_without_relation(
|
38
|
-
model: reflection_class,
|
39
|
-
relation: reflection_owner,
|
40
|
-
limit: Colonization.configuration.seed_index
|
41
|
-
)
|
42
|
-
count_to_colonize = Colonization.configuration.seed_index - (found_instances ? found_instances.count : 0)
|
43
|
-
Colonization::Colonizer.new(reflection_class).colonize(count_to_colonize)
|
44
|
-
end
|
45
|
-
|
46
|
-
def reflection_class
|
47
|
-
reflection.class_name.constantize
|
48
|
-
end
|
49
|
-
|
50
|
-
def reflection_owner
|
51
|
-
reflection.active_record
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|