Module: SeedGen
- Defined in:
- lib/seedgen.rb,
lib/seedgen/faker.rb,
lib/seedgen/railtie.rb,
lib/seedgen/version.rb,
lib/seedgen/seed_file.rb,
lib/seedgen/database/database.rb
Defined Under Namespace
Modules: Database, FakerData, SeedFile
Classes: Railtie
Constant Summary
collapse
- SKIP_ATTRS =
%w[ id created_at updated_at ]
- VERSION =
"0.0.4"
Class Method Summary
collapse
Class Method Details
.attrs(model) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/seedgen.rb', line 98
def self.attrs(model)
data = {}
columns = model.content_columns
columns.each do |column|
unless SKIP_ATTRS.include?(column.name)
data[column.name.to_sym] = FakerData.generate(model, column)
end
end
data
end
|
.build_scaffold ⇒ Object
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/seedgen.rb', line 18
def self.build_scaffold
@models.each do |model|
scaffold_model(model)
@scaffolded_models << model
end
if database_scaffolded?
write_scaffold_to_file
end
end
|
.database_scaffolded? ⇒ Boolean
111
112
113
|
# File 'lib/seedgen.rb', line 111
def self.database_scaffolded?
@models == @scaffolded_models
end
|
.parents(model) ⇒ Object
returns parents that have not been persisted TODO: refactor with filter Ignore this awful code 🤮
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/seedgen.rb', line 67
def self.parents(model)
parents = []
model.reflect_on_all_associations(:belongs_to) do |assoc|
parents << assoc.name.to_s.capitalize
end
nonpersisted_parents = []
parents.each do |parent|
next if parent.active_record.count > 0
nonpersisted_parents << parent.active_record
end
nonpersisted_parents
end
|
.run ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/seedgen.rb', line 10
def self.run
@models = Database.models || []
@scaffold = {}
@scaffolded_models = []
build_scaffold
end
|
.scaffold_model(model) ⇒ Object
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/seedgen.rb', line 53
def self.scaffold_model(model)
parents = parents(model)
if parents.empty?
write_to_scaffold(model)
else
parents.each do |parent|
scaffold_model(parent)
end
end
end
|
.write_scaffold_to_file ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/seedgen.rb', line 29
def self.write_scaffold_to_file
Dir.mkdir("db/seeds") unless File.exist?("db/seeds")
File.open("db/seeds/seedgen.rb", "w") do |file|
file.write("# TODO: Add any code that needs to run before data is created.\n\n")
@scaffold.each do |key, value|
create_model = "#{key}.create!("
attrs = []
value.each do |attr, v|
if v.is_a? String
attrs << "#{attr}: '#{v}'"
else
attrs << "#{attr}: #{v}"
end
end
create_model += "#{attrs.join(', ')})\n\n"
file.write(create_model)
end
file.write("# TODO: Add any code that needs to run after data is created.\n\n")
end
end
|
.write_to_scaffold(model) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/seedgen.rb', line 81
def self.write_to_scaffold(model)
attributes = attrs(model)
parents = []
model.reflect_on_all_associations(:belongs_to).each do |assoc|
parents << assoc.name.to_s
end
unless parents.empty?
parents.each do |parent|
attributes.merge!({ "#{parent}_id" => 1 })
end
end
@scaffold[model] = attributes
end
|