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

VERSION =

Gem version, semantic.

Version.to_s.freeze

Class Method Summary collapse

Class Method Details

.configPlanter::Config

The seeder configuration.

Returns:



38
39
40
# File 'lib/planter.rb', line 38

def config
  @config ||= Planter::Config.new
end

.configurePlanter::Config

Quick way of configuring the directories via an initializer.

Examples:

require 'planter'
Planter.configure do |config|
  config.seeders = %i[users]
  config.seeders_directory = 'db/seeds'
  config.csv_files_directory = 'db/seed_files'
end

Returns:



62
63
64
# File 'lib/planter.rb', line 62

def configure
  config.tap { |c| yield c }
end

.reset_configPlanter::Config

Resets the config back to its initial state.

Returns:



46
47
48
# File 'lib/planter.rb', line 46

def reset_config
  @config = Planter::Config.new
end

.seedObject

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.

Examples:

# db/seeds.rb, assuming your +configure+ block is in an initializer.
Planter.seed


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

.validatePlanter::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