seedgen 0.0.3 → 0.0.4
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 +28 -7
- data/lib/seedgen/version.rb +1 -1
- data/lib/seedgen.rb +44 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad6fae3db4e47667a9813436800e355f26037c5ed8d1d4ae5b12681303191c1f
|
4
|
+
data.tar.gz: 7f47ad19983eb6adcef7711f35b9a36a62028486f80a77065b17fa273af704fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 255cf01b72f3c71bb6725300e36dba5955a7bf576f9a9bbd4ebdcd810c4389af9f94f0e75468cb3660ab4559a1230cfca62bd6d9903aa98f4afd2364ad9b7778
|
7
|
+
data.tar.gz: 44b25ebd9032567e23871942c8e6018cb7a784628b9959928ccd117101fcf88ff7a5076fc3192293c9ef9195814dd9940c23c7a5861af0d177bd8638a5bbab66
|
data/README.md
CHANGED
@@ -1,26 +1,28 @@
|
|
1
1
|
# SeedGen
|
2
|
-
Generate seed files based on your schema.
|
2
|
+
Generate seed files based on your Rails models and schema.
|
3
3
|
|
4
4
|
## Installation
|
5
5
|
Add this line to your application's Gemfile:
|
6
6
|
|
7
7
|
```ruby
|
8
|
-
gem "seedgen", "~> 0.0.
|
8
|
+
gem "seedgen", "~> 0.0.4"
|
9
9
|
```
|
10
10
|
|
11
11
|
## Usage
|
12
|
-
SeedGen hooks
|
13
|
-
run:
|
12
|
+
SeedGen hooks into your models to build you a seed file. After installation, run:
|
14
13
|
|
15
14
|
```bash
|
16
15
|
SEED=1 rails db:schema:load
|
17
16
|
```
|
18
17
|
|
19
|
-
The above command generates `db/seedgen.rb`
|
18
|
+
The above command generates `db/seedgen.rb` with a scaffold of all of your models and a line for each that will create the record in your db.
|
19
|
+
SeedGen attempts to create a model tree and will write to the file in the order of Root -> Child. This is so that when you run your seed file,
|
20
|
+
you won't run into any issues with relationships. For example, you may have a `Post` model that `belongs_to: user`. If the seed file attempts to create
|
21
|
+
a `Post` before its associated `User` is created, it'll obviously raise an error and crash.
|
20
22
|
|
21
23
|
Note: You may notice SeedGen missing some of your models. If this is the case, it is because
|
22
24
|
your application needs to eager load its classes for SeedGen to see them.
|
23
|
-
In `config/application.rb`, add:
|
25
|
+
In `config/application.rb`, add the below block in your test env:
|
24
26
|
|
25
27
|
```ruby
|
26
28
|
config.after_initialize do
|
@@ -28,4 +30,23 @@ config.after_initialize do
|
|
28
30
|
end
|
29
31
|
```
|
30
32
|
|
31
|
-
and rerun `SEED=1 rails db:schema:load`.
|
33
|
+
and rerun `SEED=1 rails db:schema:load`.
|
34
|
+
|
35
|
+
## Running the seed file
|
36
|
+
At the moment, `seedgen` doesn't provide you with a rake task to run this seed file. So, for now, you'll have to either 1) copy
|
37
|
+
the contents of `seedgen.rb` into your `seed.rb` file, or create your own rake task in `lib/tasks`. Note: this is a high priority
|
38
|
+
issue.
|
39
|
+
|
40
|
+
## Example output of seeds/seedgen.rb
|
41
|
+
|
42
|
+
Let's imagine you have a database with two tables: `posts` & `users`. SeedGen will create the following order-specific seed file.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# TODO: Add any code that needs to run before data is created.
|
46
|
+
|
47
|
+
User.create!(first_name: 'temgcpr', last_name: 'cuqoghi', email: 'zrcpqhn', password: 'ophpnxy')
|
48
|
+
|
49
|
+
Post.create!(title: 'vurkcff', body: 'morph back-end initiatives', status: 2, user_id: 1)
|
50
|
+
|
51
|
+
# TODO: Add any code that needs to run after data is created.
|
52
|
+
```
|
data/lib/seedgen/version.rb
CHANGED
data/lib/seedgen.rb
CHANGED
@@ -9,23 +9,54 @@ module SeedGen
|
|
9
9
|
|
10
10
|
def self.run
|
11
11
|
@models = Database.models || []
|
12
|
-
@
|
12
|
+
@scaffold = {}
|
13
|
+
@scaffolded_models = []
|
13
14
|
|
15
|
+
build_scaffold
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.build_scaffold
|
14
19
|
@models.each do |model|
|
15
|
-
|
16
|
-
@
|
20
|
+
scaffold_model(model)
|
21
|
+
@scaffolded_models << model
|
17
22
|
end
|
18
23
|
|
19
|
-
|
24
|
+
if database_scaffolded?
|
25
|
+
write_scaffold_to_file
|
26
|
+
end
|
20
27
|
end
|
21
28
|
|
22
|
-
def self.
|
29
|
+
def self.write_scaffold_to_file
|
30
|
+
Dir.mkdir("db/seeds") unless File.exist?("db/seeds")
|
31
|
+
File.open("db/seeds/seedgen.rb", "w") do |file|
|
32
|
+
file.write("# TODO: Add any code that needs to run before data is created.\n\n")
|
33
|
+
|
34
|
+
@scaffold.each do |key, value|
|
35
|
+
create_model = "#{key}.create!("
|
36
|
+
attrs = []
|
37
|
+
value.each do |attr, v|
|
38
|
+
if v.is_a? String
|
39
|
+
attrs << "#{attr}: '#{v}'"
|
40
|
+
else
|
41
|
+
attrs << "#{attr}: #{v}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
create_model += "#{attrs.join(', ')})\n\n"
|
46
|
+
file.write(create_model)
|
47
|
+
end
|
48
|
+
|
49
|
+
file.write("# TODO: Add any code that needs to run after data is created.\n\n")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.scaffold_model(model)
|
23
54
|
parents = parents(model)
|
24
55
|
if parents.empty?
|
25
|
-
|
56
|
+
write_to_scaffold(model)
|
26
57
|
else
|
27
58
|
parents.each do |parent|
|
28
|
-
|
59
|
+
scaffold_model(parent)
|
29
60
|
end
|
30
61
|
end
|
31
62
|
end
|
@@ -47,22 +78,21 @@ module SeedGen
|
|
47
78
|
nonpersisted_parents
|
48
79
|
end
|
49
80
|
|
50
|
-
def self.
|
51
|
-
puts "===================="
|
52
|
-
puts "Creating: #{model}"
|
81
|
+
def self.write_to_scaffold(model)
|
53
82
|
attributes = attrs(model)
|
54
83
|
parents = []
|
84
|
+
|
55
85
|
model.reflect_on_all_associations(:belongs_to).each do |assoc|
|
56
86
|
parents << assoc.name.to_s
|
57
87
|
end
|
58
88
|
|
59
89
|
unless parents.empty?
|
60
90
|
parents.each do |parent|
|
61
|
-
attributes.merge!({ parent
|
91
|
+
attributes.merge!({ "#{parent}_id" => 1 })
|
62
92
|
end
|
63
93
|
end
|
64
94
|
|
65
|
-
model
|
95
|
+
@scaffold[model] = attributes
|
66
96
|
end
|
67
97
|
|
68
98
|
def self.attrs(model)
|
@@ -78,8 +108,7 @@ module SeedGen
|
|
78
108
|
data
|
79
109
|
end
|
80
110
|
|
81
|
-
|
82
|
-
|
83
|
-
@models == @seeded_models
|
111
|
+
def self.database_scaffolded?
|
112
|
+
@models == @scaffolded_models
|
84
113
|
end
|
85
114
|
end
|