Module: Planter
- Defined in:
- lib/planter.rb,
lib/planter/config.rb,
lib/planter/seeder.rb,
lib/planter/railtie.rb,
lib/planter/version.rb,
lib/planter/validator.rb,
lib/planter/progress_bar.rb,
lib/planter/seed_context.rb,
lib/planter/csv_data_source.rb,
lib/planter/record_attributes.rb,
lib/planter/adapters/active_record.rb,
lib/generators/planter/seeder_generator.rb,
lib/generators/planter/adapter_generator.rb,
lib/generators/planter/initializer_generator.rb
Overview
The main module for the plugin. It nicely wraps the Planter::Config class
so that you can customize the plugin via an initializer or in the
db/seeds.rb file. This is how you'll specify your list of seeders to use,
along with customizing the seeders_directory and csv_files_directory.
Planter.configure do |config|
config.seeders = %i[users]
config.seeders_directory = 'db/seeds'
config.csv_files_directory = 'db/seed_files'
end
To then seed your application, simply call the seed method from your
db/seeds.rb file (or wherever you need to call it from).
Planter.seed
Defined Under Namespace
Modules: Adapters, Generators, Version Classes: Config, CsvDataSource, ProgressBar, Railtie, RecordAttributes, SeedContext, Seeder, Validator
Constant Summary collapse
Class Method Summary collapse
-
.config ⇒ Planter::Config
The seeder configuration.
-
.configure ⇒ Planter::Config
Quick way of configuring the directories via an initializer.
-
.reset_config ⇒ Planter::Config
Resets the config back to its initial state.
-
.seed ⇒ Object
This is the method to call from your
db/seeds.rb. -
.validate ⇒ Planter::Validator::Result
Validate the configured seed plan without creating records.
Class Method Details
.config ⇒ Planter::Config
The seeder configuration.
38 39 40 |
# File 'lib/planter.rb', line 38 def config @config ||= Planter::Config.new end |
.configure ⇒ Planter::Config
Quick way of configuring the directories via an initializer.
62 63 64 |
# File 'lib/planter.rb', line 62 def configure config.tap { |c| yield c } end |
.reset_config ⇒ Planter::Config
Resets the config back to its initial state.
46 47 48 |
# File 'lib/planter.rb', line 46 def reset_config @config = Planter::Config.new end |
.seed ⇒ Object
This is the method to call from your db/seeds.rb. It calls the seeders
listed in Planter.config.seeders. To call specific seeders at runtime,
you can set the SEEDERS environment variable to a comma-separated list
of seeders, like rails db:seed SEEDERS=users,accounts.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/planter.rb', line 75 def seed seeders = ENV["SEEDERS"]&.split(",") || config.seeders&.map(&:to_s) if seeders.blank? warn "WARNING: Planter.seed was called, but no seeders were specified. Add seeders to config.seeders in config/initializers/planter.rb or set SEEDERS." return end seeders.each do |s| require Rails.root.join(config.seeders_directory, "#{s}_seeder.rb").to_s puts "Seeding #{s}" unless config.quiet "#{s.camelize}Seeder".constantize.new.seed end end |
.validate ⇒ Planter::Validator::Result
Validate the configured seed plan without creating records. This checks seeder files, seeder classes, built-in seeding method configuration, CSV headers where possible, and the configured adapter API.
95 96 97 |
# File 'lib/planter.rb', line 95 def validate Planter::Validator.new.validate end |