planter 0.0.7 → 0.0.8

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: 42b61a78b5034eadac54f7e5a570073851b4a9a6fc20c4fae71a1e49fb8d3df1
4
- data.tar.gz: c02538d3a776a89b221b135c4aa2d355a278df35e514d53872f200556bc720dd
3
+ metadata.gz: cefea9dd4547f81df2480f10a92ed64738475a21db8237dac4599a060a80cf19
4
+ data.tar.gz: 660ecdca12dc88752d353a39e0a6fc0bef2582b7203628002e23f6d246d49589
5
5
  SHA512:
6
- metadata.gz: 119f4f0a790efd00eb4e537734ab51e9416b5017f0891a07698d291b44604b2a81e7ea73d1a8893da1e3f4c24b4790a4a18a352388240d9ed9de32a1f19206b5
7
- data.tar.gz: 3c066efbe8b1325bec3218de0840bf91688c6ed1da29e06446756603f370620d5a57af4afc57313fa42171ba977f1dab9cbe73a9578fcb3379e2433935c609b3
6
+ metadata.gz: 486bf0a34ac192c6c1295a1b628d25b5e685a83d92700f99291ef35e2ea58b30f5db976057ae87e03a76d48ee29bceacfcde5e5d1018488976f2304f079aafd1
7
+ data.tar.gz: 3ab0665aa4c97a0a7f405bc93604256a90c2c7e88745d5ded6f908fc17f914fb76cc883f69c5a709fd5210902bc5039f80a294cc26548a8eead99695a1dda529
data/README.md CHANGED
@@ -7,13 +7,12 @@
7
7
 
8
8
  Seeds for Rails applications can get complicated fast, and Rails doesn't provide
9
9
  much for assisting with this process. This plugin seeks to rectify that by
10
- providing easy ways to seed specific tables by hooking into the existing `rails
11
- db:seed` task.
10
+ providing easy ways to seed specific tables.
12
11
 
13
12
  Features include:
14
13
 
15
14
  - Seed tables from CSV files, an array of hashes, or custom methods.
16
- - Call specific seeders with `rails db:seed SEEDERS=users,addresses`.
15
+ - Call specific seeders with `rails planter:seed SEEDERS=users,addresses`.
17
16
  - Control the number of records being created.
18
17
  - Seed associations.
19
18
 
@@ -41,30 +40,69 @@ $ gem install planter
41
40
  ## Usage
42
41
  Let's assume you'd like to seed your `users` table.
43
42
 
44
- To get started, simply add the following to your `db/seeds.rb` file. Note that
45
- the `config.seeders` should be an array of the seeders to use. They should be in
46
- the correct order to successfully seed the tables when considering associations.
43
+ To get started, run `rails generate planter:initializer`, which will create
44
+ `config/initializers/planter.rb` with the following contents.
47
45
 
48
46
  ```ruby
49
47
  require 'planter'
50
48
 
51
49
  Planter.configure do |config|
52
- config.seeders = %i[ users ]
50
+ ##
51
+ # The list of seeders. These files are stored in the
52
+ # config.seeders_directory, which can be changed below. When a new
53
+ # seeeder is generated, it will be appended to the bottom of this
54
+ # list. If the order is incorrect, you'll need to adjust the it.
55
+ # Just be sure to keep the ending bracket on its own line, or the
56
+ # generator won't know where to put new elements.
57
+ config.seeders = %i[
58
+ ]
59
+
60
+ ##
61
+ # The directory where the seeder files are kept.
62
+ # config.seeders_directory = 'db/seeds'
63
+
64
+ ##
65
+ # The directory where CSVs are kept.
66
+ # config.csv_files_directory = 'db/seed_files'
53
67
  end
68
+ ```
69
+
70
+ By default, a `planter:seed` task is provided for seeding your application. This
71
+ allows you to use `db:seed` for other purposes. If you want Planter to hook
72
+ into the existing `db:seed` task, simply add the following to `db/seeds.rb`.
54
73
 
74
+ ```ruby
55
75
  Planter.seed
56
76
  ```
57
77
 
58
- Then, create a directory called `db/seeds`, and create a file called
59
- `db/seeds/users_seeder.rb`. In that file, create the following class. Note the
60
- name of the seeder is the name of the table, plus `Seeder`, and it inherits from
61
- `Planter::Seeder`.
78
+ To create a users seeder, run `rails generate planter:seeder users`. Usually,
79
+ seeders seed a specific table, so it's recommended to name your seeders after
80
+ the table. If you don't, you'll need to manually specify a few things. More on
81
+ that later. This will create a file named `db/seeds/users_seeder.rb` (the
82
+ directory will be created if it doesn't exist) with the following contents.
62
83
 
63
84
  ```ruby
64
85
  class UsersSeeder < Planter::Seeder
86
+ # TODO: Choose a seeding_method. For example:
87
+ # seeding_method :csv
88
+
89
+ # For now, we overload the seed method so no exception will be raised.
90
+ def seed
91
+ end
65
92
  end
66
93
  ```
67
94
 
95
+ This also adds `users` to the `config.seeders` array in our initializer. A few
96
+ things to note.
97
+
98
+ - The seeder will always be appended at the end of the array. If this is not the
99
+ correct order, you'll need to adjust the array manually.
100
+ - When adjusting the array, always keep the closing bracket on its own line, or
101
+ the generator won't know where to put the new seeders.
102
+
103
+ If you want to generate a seeder for every table currently in your database, run
104
+ `rails generate planter:seeder ALL`.
105
+
68
106
  You then need to choose a seeding method, of which there are currently three.
69
107
 
70
108
  ### Seeding from CSV
@@ -107,7 +145,7 @@ test2@example.com,<%= count += 1 %>
107
145
  test2@example.com,<%= count += 1 %>
108
146
  ```
109
147
 
110
- Running `rails db:seed` will now seed your `users` table.
148
+ Running `rails planter:seed` will now seed your `users` table.
111
149
 
112
150
  ## Seeding from a data array
113
151
  If you need dynamic seeds, you can add something similar to the following to
@@ -115,7 +153,7 @@ your seeder class. In this example, we'll use
115
153
  [faker](https://github.com/faker-ruby/faker).
116
154
 
117
155
  ```ruby
118
- require 'faker' # You should really just require this in `db/seeds.rb`.
156
+ require 'faker' # You could just require this in `db/seeds.rb`.
119
157
 
120
158
  class UsersSeeder < Planter::Seeder
121
159
  seeding_method :data_array, number_of_records: 10
@@ -135,7 +173,7 @@ ten elements to create ten records. It's also worth noting that setting an
135
173
  instance variable called `@data` from an `initialize` method would also work, as
136
174
  the `Planter::Seeder` parent class automatically provides `attr_reader :data`.
137
175
 
138
- Running `rails db:seed` should now seed your `users` table.
176
+ Running `rails planter:seed` should now seed your `users` table.
139
177
 
140
178
  You can also seed children records for every existing record of a parent model.
141
179
  For example, to seed an address for every user, you'd need to create an
@@ -180,26 +218,6 @@ class UsersSeeder < Planter::Seeder
180
218
  end
181
219
  ```
182
220
 
183
- ## Customization
184
- You can change the directories of both the seeder files and the CSV files. In
185
- your `configure` block in `db/seeds.rb`, you can add the following. Note that,
186
- in both instances, the path should be relative to `Rails.root`.
187
-
188
- ```ruby
189
- require 'planter'
190
-
191
- Planter.configure do |config|
192
- config.seeders_directory = 'db/seeder_classes'
193
- config.csv_files_directory = 'db/csvs'
194
- config.seeders = %i[
195
- users
196
- addresses
197
- ]
198
- end
199
-
200
- Planter.seed
201
- ```
202
-
203
221
  ## License
204
222
  The gem is available as open source under the terms of the [MIT
205
223
  License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,33 @@
1
+ module Planter
2
+ module Generators
3
+ class InitializerGenerator < Rails::Generators::Base
4
+ desc 'Genrates an initializer for Planter at config/initializers/planter.rb'
5
+
6
+ def create_initializer_file
7
+ create_file 'config/initializers/planter.rb', <<~EOF
8
+ require 'planter'
9
+
10
+ Planter.configure do |config|
11
+ ##
12
+ # The list of seeders. These files are stored in the
13
+ # config.seeders_directory, which can be changed below. When a new
14
+ # seeeder is generated, it will be appended to the bottom of this
15
+ # list. If the order is incorrect, you'll need to adjust the it.
16
+ # Just be sure to keep the ending bracket on its own line, or the
17
+ # generator won't know where to put new elements.
18
+ config.seeders = %i[
19
+ ]
20
+
21
+ ##
22
+ # The directory where the seeder files are kept.
23
+ # config.seeders_directory = 'db/seeds'
24
+
25
+ ##
26
+ # The directory where CSVs are kept.
27
+ # config.csv_files_directory = 'db/seed_files'
28
+ end
29
+ EOF
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ module Planter
2
+ module Generators
3
+ class SeederGenerator < Rails::Generators::Base
4
+ argument :seeder, required: true
5
+
6
+ desc "This generator creates a seeder file at #{::Planter.config.seeders_directory}"
7
+
8
+ def generate_seeders
9
+ seeder == 'ALL' ? tables.each { |t| generate(t) } : generate(seeder)
10
+ end
11
+
12
+ private
13
+
14
+ def generate(seeder)
15
+ empty_directory ::Planter.config.seeders_directory
16
+
17
+ create_file "#{::Planter.config.seeders_directory}/#{seeder}_seeder.rb", <<~EOF
18
+ class #{seeder.camelize}Seeder < Planter::Seeder
19
+ # TODO: Choose a seeding_method. For example:
20
+ # seeding_method :csv
21
+
22
+ # For now, we overload the seed method so no exception will be raised.
23
+ def seed
24
+ end
25
+ end
26
+ EOF
27
+
28
+ inject_into_file 'config/initializers/planter.rb',
29
+ " #{seeder}\n",
30
+ before: /^\s*\]\s*$/
31
+ end
32
+
33
+ def tables
34
+ @tables ||= ActiveRecord::Base.connection.tables.reject do |table|
35
+ %w[ar_internal_metadata schema_migrations].include?(table)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
data/lib/planter.rb CHANGED
@@ -8,68 +8,21 @@ require 'planter/config'
8
8
  require 'planter/seeder'
9
9
 
10
10
  ##
11
- # Class that seeders should inherit from. Seeder files should be in +db/seeds+,
12
- # and named +TABLE_seeder.rb+, where +TABLE+ is the name of the table being
13
- # seeded (I.E. +users_seeder.rb+). The seeder's class name should be the same
14
- # as the file name, but camelized. So, +UsersSeeder+. The directory where the
15
- # seeder files are located can be changed via an initializer.
11
+ # The main module for the plugin. It nicely wraps the +Planter::Config+ class
12
+ # so that you can customize the plugin via an initializer or in the
13
+ # +db/seeds.rb+ file. This is how you'll specify your list of seeders to use,
14
+ # along with customizing the +seeders_directory+ and +csv_files_directory+.
16
15
  #
17
- # The most basic way to seed is to have a CSV file with the same name as the
18
- # table in +db/seed_files/+. So, +users.csv+. This CSV should have the table's
19
- # column names as header. To seed using this method, your class should look
20
- # like the following. Note that +:csv_name+ is not required; it defaults to the
21
- # table name with a +csv+ file extension. The directory where the seed files
22
- # are kept can be changed via an initializer.
23
- # # db/seeds/users_seeder.rb
24
- # require 'planter'
25
- # class UsersSeeder < Planter::Seeder
26
- # seeding_method :csv, csv_name: '/home/me/users.csv'
16
+ # Planter.configure do |config|
17
+ # config.seeders = %i[users]
18
+ # config.seeders_directory = 'db/seeds'
19
+ # config.csv_files_directory = 'db/seed_files'
27
20
  # end
28
21
  #
29
- # Another way to seed is to create records from a data array. To do this, your
30
- # class must implement a +data+ attribute or method, which is an array of
31
- # hashes. Note that this class already provides the +attr_reader+ for this
32
- # attribute, so the most you have to do it create instance variables in your
33
- # constructor. If if you want your data to be different for each new record
34
- # (via Faker, +Array#sample+, etc.), you'll probably want to supply a method
35
- # called data that returns an array of new data each time.
36
- # require 'planter'
37
- # class UsersSeeder < Planter::Seeder
38
- # seeding_method :data_array
39
- # def data
40
- # [{foo: 'bar', baz: 'bar'}]
41
- # end
42
- # end
43
- #
44
- # In both of the above methods, you can specify +parent_model+ and
45
- # +association+. If specified, records will be created via that parent model's
46
- # association. If +association+ is not provided, it will be assumed to be the
47
- # model name, pluralized and snake-cased (implying a +has_many+ relationship).
48
- # For example, if we're seeding the users table, and the model is +User+, the
49
- # association will default to +users+.
50
- # require 'planter'
51
- # class UsersSeeder < Planter::Seeder
52
- # seeding_method :data_array, parent_model: 'Person', association: :users
53
- # def data
54
- # [{foo: 'bar', baz: 'bar'}]
55
- # end
56
- # end
22
+ # To then seed your application, simply call the +seed+ method from your
23
+ # +db/seeds.rb+ file (or wherever you need to call it from).
57
24
  #
58
- # You can also set +number_of_records+ to determine how many times each record
59
- # in the +data+ array will get created. The default is 1. Note that if this
60
- # attribute is set alongside +parent_model+ and +association+,
61
- # +number_of_records+ will be how many records will be created for each record
62
- # in the parent table.
63
- # require 'planter'
64
- # class UsersSeeder < Planter::Seeder
65
- # seeding_method :data_array, number_of_records: 5
66
- # def data
67
- # [{foo: 'bar', baz: 'bar'}]
68
- # end
69
- # end
70
- #
71
- # If you need to seed a different way, put your own custom +seed+ method in
72
- # your seeder class and do whatever needs to be done.
25
+ # Planter.seed
73
26
  module Planter
74
27
  ##
75
28
  # The seeder configuration.
@@ -90,13 +43,14 @@ module Planter
90
43
  ##
91
44
  # Quick way of configuring the directories via an initializer.
92
45
  #
93
- # @return [self]
46
+ # @return [Planter::Config]
94
47
  #
95
48
  # @example
96
- # Planter.configure do |app_seeder|
97
- # app_seeder.seeders = %i[users]
98
- # app_seeder.seeders_directory = 'db/seeds'
99
- # app_seeder.csv_files_directory = 'db/seed_files'
49
+ # require 'planter'
50
+ # Planter.configure do |config|
51
+ # config.seeders = %i[users]
52
+ # config.seeders_directory = 'db/seeds'
53
+ # config.csv_files_directory = 'db/seed_files'
100
54
  # end
101
55
  def self.configure
102
56
  config.tap { |c| yield c }
@@ -104,20 +58,21 @@ module Planter
104
58
 
105
59
  ##
106
60
  # This is the method to call from your +db/seeds.rb+. It callse the seeders
107
- # listed in +Planter.config.seeders+. To call specific seeders at
108
- # runtime, you can set the +SEEDERS+ environmental variable to a
109
- # comma-separated list of seeders.
61
+ # listed in +Planter.config.seeders+. To call specific seeders at runtime,
62
+ # you can set the +SEEDERS+ environmental variable to a comma-separated list
63
+ # of seeders, like +rails db:seed SEEDERS=users,accounts+.
110
64
  #
111
65
  # @example
112
- # rails db:seed SEEDERS=users,accounts
66
+ # # db/seeds.rb, assuming your +configure+ block is in an initializer.
67
+ # Planter.seed
113
68
  def self.seed
114
69
  seeders = ENV['SEEDERS']&.split(',') || config.seeders&.map(&:to_s)
115
- raise RuntimeError, 'No seeders specified; nothing to do' unless seeders&.any?
70
+ raise RuntimeError, 'No seeders specified' unless seeders&.any?
116
71
 
117
- seeders.each do |table|
118
- require Rails.root.join(config.seeders_directory, "#{table}_seeder.rb").to_s
119
- puts "Seeding #{table}" unless config.quiet
120
- "#{table.camelize}Seeder".constantize.new.seed
72
+ seeders.each do |s|
73
+ require Rails.root.join(config.seeders_directory, "#{s}_seeder.rb").to_s
74
+ puts "Seeding #{s}" unless config.quiet
75
+ "#{s.camelize}Seeder".constantize.new.seed
121
76
  end
122
77
  end
123
78
  end
@@ -2,5 +2,13 @@
2
2
 
3
3
  module Planter
4
4
  class Railtie < ::Rails::Railtie # :nodoc:
5
+ rake_tasks do
6
+ load File.join(
7
+ File.expand_path(File.dirname(__FILE__)),
8
+ '..',
9
+ 'tasks',
10
+ 'planter_tasks.rake'
11
+ )
12
+ end
5
13
  end
6
14
  end
@@ -2,7 +2,71 @@
2
2
 
3
3
  module Planter
4
4
  ##
5
- # The class your seeder files should inherit from.
5
+ # Class that seeders should inherit from. Seeder files should be in
6
+ # +db/seeds+, and named +TABLE_seeder.rb+, where +TABLE+ is the name of the
7
+ # table being seeded (I.E. +users_seeder.rb+). If your seeder is named
8
+ # differently than the table, you'll need to specify the table with the
9
+ # +model+ option. The seeder's class name should be the same as the file
10
+ # name, but camelized. So, +UsersSeeder+. The directory where the seeder
11
+ # files are located can be changed via an initializer.
12
+ #
13
+ # The most basic way to seed is to have a CSV file with the same name as the
14
+ # table in +db/seed_files/+. So, +users.csv+. This CSV should have the
15
+ # table's column names as header. To seed using this method, your class
16
+ # should look like the following. Note that +:csv_name+ and +:model+ are only
17
+ # required if your seeder or csv are named differently than the table being
18
+ # seeded. The directory where the seed files are kept can be changed via an
19
+ # initializer.
20
+ # # db/seeds/users_seeder.rb
21
+ # require 'planter'
22
+ # class UsersSeeder < Planter::Seeder
23
+ # seeding_method :csv, csv_name: :users, model: 'User'
24
+ # end
25
+ #
26
+ # Another way to seed is to create records from a data array. To do this,
27
+ # your class must implement a +data+ attribute or method, which is an array
28
+ # of hashes. Note that this class already provides the +attr_reader+ for this
29
+ # attribute, so the most you have to do it create instance variables in your
30
+ # constructor. If if you want your data to be different for each new record
31
+ # (via Faker, +Array#sample+, etc.), you'll probably want to supply a method
32
+ # called data that returns an array of new data each time.
33
+ # require 'planter'
34
+ # class UsersSeeder < Planter::Seeder
35
+ # seeding_method :data_array
36
+ # def data
37
+ # [{foo: 'bar', baz: 'bar'}]
38
+ # end
39
+ # end
40
+ #
41
+ # In both of the above methods, you can specify +parent_model+ and
42
+ # +association+. If specified, records will be created via that parent
43
+ # model's association. If +association+ is not provided, it will be assumed
44
+ # to be the model name, pluralized and snake-cased (implying a +has_many+
45
+ # relationship). For example, if we're seeding the users table, and the
46
+ # model is +User+, the association will default to +users+.
47
+ # require 'planter'
48
+ # class UsersSeeder < Planter::Seeder
49
+ # seeding_method :data_array, parent_model: 'Person', association: :users
50
+ # def data
51
+ # [{foo: 'bar', baz: 'bar'}]
52
+ # end
53
+ # end
54
+ #
55
+ # You can also set +number_of_records+ to determine how many times each
56
+ # record in the +data+ array will get created. The default is 1. Note that if
57
+ # this attribute is set alongside +parent_model+ and +association+,
58
+ # +number_of_records+ will be how many records will be created for each
59
+ # record in the parent table.
60
+ # require 'planter'
61
+ # class UsersSeeder < Planter::Seeder
62
+ # seeding_method :data_array, number_of_records: 5
63
+ # def data
64
+ # [{foo: 'bar', baz: 'bar'}]
65
+ # end
66
+ # end
67
+ #
68
+ # If you need to seed a different way, put your own custom +seed+ method in
69
+ # your seeder class and do whatever needs to be done.
6
70
  class Seeder
7
71
  ##
8
72
  # The allowed seeding methods.
@@ -12,11 +76,10 @@ module Planter
12
76
 
13
77
  ##
14
78
  # Array of hashes used to create records. Your class must set this
15
- # attribute when using +data_hash+ seeding method, although it's probably
79
+ # attribute when using +data_array+ seeding method, although it's probably
16
80
  # more likely that you'll want to define a method that returns a new set of
17
- # data each time (via +Faker+, +Array#sample+, etc.). When using
18
- # +csv+, +data+ will be set to the data within the csv. You can
19
- # override this.
81
+ # data each time (via +Faker+, +Array#sample+, etc.). When using +csv+,
82
+ # +data+ will be set to the data within the csv. You can override this.
20
83
  #
21
84
  # @return [Array]
22
85
  attr_reader :data
@@ -21,7 +21,7 @@ module Planter
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 7
24
+ PATCH = 8
25
25
 
26
26
  ##
27
27
  # Version as +[MAJOR, MINOR, PATCH]+
@@ -1,4 +1,13 @@
1
- # desc "Explaining what the task does"
2
- # task :planter do
3
- # # Task goes here
4
- # end
1
+ require 'planter'
2
+
3
+ namespace :planter do
4
+ desc 'Seed application. Use this to keep planter separate from db:seed'
5
+ task seed: :environment do
6
+ Planter.configure do |config|
7
+ # NOTE: the seed method already looks for ENV['SEEDERS']
8
+ ENV['SEEDERS_DIRECTORY'] && config.seeders_directory = ENV['SEEDERS_DIRECTORY']
9
+ ENV['CSV_FILES_DIRECTORY'] && config.csv_files_directory = ENV['CSV_FILES_DIRECTORY']
10
+ end
11
+ Planter.seed
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Gray
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-24 00:00:00.000000000 Z
11
+ date: 2021-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -41,6 +41,8 @@ files:
41
41
  - LICENSE
42
42
  - README.md
43
43
  - Rakefile
44
+ - lib/generators/planter/initializer_generator.rb
45
+ - lib/generators/planter/seeder_generator.rb
44
46
  - lib/planter.rb
45
47
  - lib/planter/config.rb
46
48
  - lib/planter/railtie.rb