seedgen 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c656032481489823a64542f0f864d64c8874d6156d1f17664f1770b2e945027
4
- data.tar.gz: ff0b902ea7ef8a597732a957d214fa6f80d0a40b1993da7b7e4c994810336866
3
+ metadata.gz: ad6fae3db4e47667a9813436800e355f26037c5ed8d1d4ae5b12681303191c1f
4
+ data.tar.gz: 7f47ad19983eb6adcef7711f35b9a36a62028486f80a77065b17fa273af704fe
5
5
  SHA512:
6
- metadata.gz: cb9c89738160a21995a42560e070c4ed7ee8a5f3ef9173096217a3cefca5dc006ccdf2048a8d19a2e6baea572af896e892ad81c82d448692826558cb414b57a3
7
- data.tar.gz: 988d86cacea68dd32c98415268f1f271fb8c5f5c3e863d44dace229ae02f25fb962b19d01aae5984183bd2e9669ccc1f728fd095a51213601fcb2387371411cb
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.3"
8
+ gem "seedgen", "~> 0.0.4"
9
9
  ```
10
10
 
11
11
  ## Usage
12
- SeedGen hooks in to your models and their relationships to build you a seed file. After installation,
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` and seeds your database with the output of this new file based on your schema/models.
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
+ ```
@@ -1,3 +1,3 @@
1
1
  module SeedGen
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/seedgen.rb CHANGED
@@ -9,23 +9,54 @@ module SeedGen
9
9
 
10
10
  def self.run
11
11
  @models = Database.models || []
12
- @seeded_models = []
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
- seed(model)
16
- @seeded_models << model
20
+ scaffold_model(model)
21
+ @scaffolded_models << model
17
22
  end
18
23
 
19
- puts database_seeded?
24
+ if database_scaffolded?
25
+ write_scaffold_to_file
26
+ end
20
27
  end
21
28
 
22
- def self.seed(model)
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
- create_record(model)
56
+ write_to_scaffold(model)
26
57
  else
27
58
  parents.each do |parent|
28
- seed(parent)
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.create_record(model)
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.to_sym => Object.const_get(parent.capitalize).first })
91
+ attributes.merge!({ "#{parent}_id" => 1 })
62
92
  end
63
93
  end
64
94
 
65
- model.create!(attributes)
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
- # TODO: validate that all records have been created
82
- def self.database_seeded?
83
- @models == @seeded_models
111
+ def self.database_scaffolded?
112
+ @models == @scaffolded_models
84
113
  end
85
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seedgen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Wasson