Skip to content

Instantly share code, notes, and snippets.

@mindtonic
Created Aug 24, 2011
Embed
What would you like to do?
Seeder Class Example Files
require 'active_record'
require 'lib/seeder'
namespace :seed do
#
# All
#
task(:all => :environment) do
Seeder.seed
end
#
# Admins
#
task(:admin_users => :environment) do
Seeder.admin_users
end
task(:reset_admin_users => :environment) do
Seeder.truncate_table :users
Seeder.admin_users
end
#
# Users
#
task(:users => :environment) do
Seeder.users
end
task(:reset_users => :environment) do
User.delete_all
Seeder.truncate_table :users
Seeder.users
end
task(:delete_users => :environment) do
User.delete_all
Seeder.truncate_table :users
end
#
# Countries
#
task(:countries => :environment) do
Seeder.countries
end
task(:reset_countries => :environment) do
Seeder.truncate_table :countries
Seeder.countries
end
#
# Topics
#
task(:topics => :environment) do
Seeder.topics
end
task(:reset_topics => :environment) do
Topic.delete_all
Seeder.truncate_table :topics
Seeder.topics
end
#
# Posts
#
task(:posts => :environment) do
Seeder.posts
end
task(:reset_posts => :environment) do
Seeder.truncate_table :posts
Seeder.posts
end
#
# Forum
#
task(:reset_forum => :environment) do
Topic.delete_all
Seeder.truncate_table :topics
Seeder.truncate_table :posts
Seeder.topics
Seeder.posts
end
end
require 'ffaker'
include ActionDispatch::TestProcess
include FileUtils
class Seeder
def seed
puts "===== Adding Seed Data ====="
housekeeping
rebuild_database
countries
admin_users
users
topics
posts
end
#
# Housekeeping
#
def housekeeping
puts "Removing old system (Paperclip) images"
FileUtils.remove_dir('public/system') if File.exists?('public/system')
end
#
# Countries
#
def countries
puts "===== COUNTRIES ====="
puts "Adding Countries"
File.open("fixtures/countries.txt", "r") do |file|
while name = file.gets
country = Country.create(:name => name)
report country
end
end
count_records Country
end
#
# USERS
#
def admin_users
puts "===== ADMIN USERS ====="
spacemonkey = User.create( :email => "spacemonkey@example.com",
:password => "123456",
:password_confirmation => "123456",
:avatar => fixture_file_upload("fixtures/avatars/spacemonkey.jpg", 'image/jpeg'),
:phone => Faker::PhoneNumber.short_phone_number,
:country => Country.random.first )
report(spacemonkey)
spartacus = User.create( :email => "spartacus@creativeallies.com",
:password => "spartacus",
:password_confirmation => "spartacus",
:avatar => fixture_file_upload("fixtures/avatars/spartacus.jpg", 'image/jpeg'),
:phone => Faker::PhoneNumber.short_phone_number,
:country => Country.random.first )
report(spartacus)
count_records User
end
def users
puts "==== RANDOM USERS ===="
50.times do
begin
user = User.create( :email => Faker::Internet.email,
:password => '123456',
:password_confirmation => '123456',
:avatar => fixture_file_upload("fixtures/headshots/#{rand(19)+1}.jpg", 'image/jpeg'),
:phone => Faker::PhoneNumber.short_phone_number,
:country => Country.random.first )
report(user)
rescue => e
puts "Something went wrong => #{e}"
end
end
count_records User
end
#
# FORUM
#
def topics
puts "===== TOPICS ====="
puts "Adding 100 Topics"
50.times do
topic = Topic.create(:title => Faker::Company.bs, :user => User.random.first, :sticky => randbool)
report topic
end
count_records Topic
end
def posts
puts "===== POSTS ====="
puts "Adding 1000 Posts"
3000.times do
post = Post.create(:body => Faker::Lorem.paragraphs.join("\n\n"), :user => User.random.first, :topic => Topic.random.first)
report post
end
count_records Post
end
#
# Utilities
#
def mime_type filepath
`file -Ib #{filepath}`.gsub(/\n/,"").split(';').first
end
def truncate_table(table_name)
ActiveRecord::Base.connection.execute("DELETE FROM #{table_name}")
ActiveRecord::Base.connection.execute("VACUUM")
puts "XXX Truncated Table #{table_name}"
end
def rebuild_database
puts "rewinding database"
`rake db:migrate VERSION=0`
puts "rebuilding database"
`rake db:migrate`
end
def count_records(element, title = false)
puts "### COUNT #{title || element.to_s} -> #{element.count}"
end
def report(element)
case element.class.to_s
when "User"
puts "Added User #{element.email} from #{element.country.name}"
when "Country"
puts "Added Country #{element.name}"
when "Topic"
puts "Added Topic #{element.title}"
when "Post"
puts "Added Post to Topic #{element.topic.title} by #{element.user.email}"
end
end
def randbool
rand(2) == 1 ? true : false
end
#
# Method Missing
#
def self.method_missing(name, *args, &block)
Seeder.new.send(name, *args, &block)
end
end
require 'lib/seeder'
Seeder.seed
@philmill
Copy link

philmill commented Aug 24, 2011

Jay, I dig the organization, but how does Seeder.seed execute when seed is defined as an instance method?

@mindtonic
Copy link
Author

mindtonic commented Aug 24, 2011

@philmill
Copy link

philmill commented Aug 24, 2011

Ahh yes, the ole Object.send method. Thanks for posting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment