Module: Planter

Defined in:
lib/planter.rb,
lib/planter/config.rb,
lib/planter/seeder.rb,
lib/planter/railtie.rb,
lib/planter/version.rb,
lib/generators/planter/seeder_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: Generators, Version Classes: Config, Railtie, Seeder

Constant Summary collapse

VERSION =

Gem version, semantic.

Version.to_s.freeze

Class Method Summary collapse

Class Method Details

.configPlanter::Config

The seeder configuration.

Returns:



31
32
33
# File 'lib/planter.rb', line 31

def self.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:



55
56
57
# File 'lib/planter.rb', line 55

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

.reset_configPlanter::Config

Resets the config back to its initial state.

Returns:



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

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

.seedObject

This is the method to call from your db/seeds.rb. It callse the seeders listed in Planter.config.seeders. To call specific seeders at runtime, you can set the SEEDERS environmental 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

Raises:

  • (RuntimeError)


68
69
70
71
72
73
74
75
76
77
# File 'lib/planter.rb', line 68

def self.seed
  seeders = ENV['SEEDERS']&.split(',') || config.seeders&.map(&:to_s)
  raise RuntimeError, 'No seeders specified' if seeders.blank?

  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