Last active
March 29, 2017 15:38
-
-
Save dbernheisel/c4e6176eaad9801895ca0a6d3e658d3a to your computer and use it in GitHub Desktop.
Seeding for Rails environments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Seed | |
| def self.sprout | |
| require_seed_modules! | |
| puts "\n======= SEEDING FOR ALL ENVIRONMENTS" | |
| seed_from(Seed::Base) | |
| if Rails.env.development? | |
| puts "\n======= SEEDING FOR DEVELOPMENT" | |
| seed_from(Seed::Development) | |
| end | |
| # continue for each environment as necessary | |
| end | |
| def self.require_seed_modules! | |
| Dir["#{Rails.root}/db/seed/*.rb"].each { |s| require s } | |
| end | |
| def self.seed_from(seed_class) | |
| seeds = seed_class.methods.map(&:to_s).select { |method| method.starts_with? 'seed_' } | |
| seeds.each do |seed| | |
| title = seed[4..-1].titleize | |
| print "Seeding #{title}..." | |
| seed_class.send seed | |
| puts "\rSeeded: #{title} " | |
| end | |
| end | |
| end | |
| Seed.sprout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment