Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment