Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace :db do
- desc "Fill database with fake data"
- task :populate => :environment do
- require 'faker'
- User.find(:all).each{|u| u.destroy}
- max_id = User.maximum("id") || 1
- random_users = lambda {|*args|
- count = args.shift
- User.find(:all, :limit => count, :conditions =>
- {:id => (1..count).map {|i| rand(max_id)}})
- }
- any = lambda{|array| array[rand(array.size)] }
- 1.upto(40000) do
- name = "#{Faker::Name.name}_#{max_id}"
- login = name.downcase.gsub(' ','_').gsub('.','').gsub('\'', '_')
- puts login
- email = Faker::Internet.email.gsub(/@/, "_#{max_id}@")
- user = User.create!(
- :email => email,
- :login => login,
- :name => name,
- :new_password => 'password',
- :new_password_confirmation => 'password'
- )
- user.activate!(true)
- user.friends = random_users[5] if max_id > 40
- max_id += 1
- end if User.count < 10000
- users = User.connection.select_values("SELECT id FROM users").
- map {|id| id.to_i}
- puts "Generated #{User.count} users"
- Guild.all.each do |guild|
- next if guild.blog_editors.count > 0
- guild.blog_editors = random_users[5]
- guild.users = random_users[5000] if guild.users.empty?
- 1.upto(5) do
- guild.clubs.create(:title => Faker::Company.name,
- :access_mode => Club::ACCESS_PUBLIC,
- :visibility_mode => Club::VISIBLE,
- :description => Faker::Company.name)
- guild.clubs.create(:title => Faker::Company.name,
- :access_mode => Club::ACCESS_INVITE,
- :visibility_mode => Club::VISIBLE,
- :description => Faker::Company.name)
- guild.clubs.create(:title => Faker::Company.name,
- :access_mode => Club::ACCESS_PUBLIC,
- :visibility_mode => Club::HIDDEN,
- :description => Faker::Company.name)
- guild.clubs.create(:title => Faker::Company.name,
- :access_mode => Club::ACCESS_INVITE,
- :visibility_mode => Club::HIDDEN,
- :description => Faker::Company.name)
- end if guild.clubs.count < 2
- end
- puts "Assigned guild editors and clubs"
- Club.all.each do |club|
- 40.times do
- fellowship = club.fellowships.build(:user_id => any[users],
- :mode => Fellowship::MODE_ACTIVE)
- fellowship.role = Fellowship::VALID_ROLES[
- rand(Fellowship::VALID_ROLES.size)]
- fellowship.save
- end if club.fellowships.count < 5
- end
- ideas = []
- Genre.all.each do |genre|
- random_users[500].each do |user|
- user.ideas.create!(
- :genre_id => genre.id,
- :name => Faker::Company.name,
- :description => Faker::Name.name,
- :access_mode => rand(Idea::ACCESS_PRIVATE)
- )
- end
- end if Idea.count < 5000
- ideas = Idea.connection.select_values("SELECT id FROM ideas").
- map {|id| id.to_i}
- puts "Created #{ideas.size} ideas"
- assets = []
- 10000.times do
- idea = Idea.find(any[ideas])
- Asset.create(:user_id => idea.user_id,
- :name => Faker::Name.name, :idea_id => idea.id)
- Audio.create(:user_id => idea.user_id,
- :name => Faker::Name.name, :idea_id => idea.id)
- Image.create(:user_id => idea.user_id,
- :name => Faker::Name.name, :idea_id => idea.id)
- Video.create(:user_id => idea.user_id,
- :name => Faker::Name.name, :idea_id => idea.id)
- end if Asset.count < 10000
- assets = Asset.connection.
- select_values("SELECT id FROM assets").map{|id| id.to_i}
- puts "Created #{assets.size} assets"
- 4000.times do |index|
- Project.create!(:idea_id => any[ideas],
- :title => "#{Faker::Company.name} #{index}")
- end if Project.count < 4000
- projects = Project.connection.
- select_values("SELECT id FROM projects").map {|id| id.to_i}
- puts "Created #{projects.size} projects"
- 2000.times do
- project = Project.find(any[projects])
- asset = Video.create! :project_id => project.id,
- :idea_id => project.idea_id, :user_id => project.users.first.id,
- :name => Faker::Name.name
- Clip.create!(:project_id => project.id, :asset_id => asset.id,
- :user_id => asset.user_id, :genre_id => asset.idea.genre_id,
- :title => Faker::Name.name, :description => Faker::Company.name)
- end if Clip.count < 2000
- clips = Clip.connection.select_values("SELECT id FROM clips").
- map {|id| id.to_i}
- puts "Created #{clips.size} clips"
- ClipLog.class_eval do
- define_method(:calculate_rating) {}
- define_method(:update_clip_rating) {}
- end
- ClipLog.delete_all
- Clip.update_all({:is_cataloguing => true, :state => "distributing"})
- clips.each do |clip_id|
- 60.downto(1) do |d|
- aquisitions = rand(40)
- clip_log = ClipLog.new(:catalog_views_count => rand(1000),
- :mobile_views_count => rand(500),
- :aquisitions_count => aquisitions,
- :income => aquisitions*(4+rand(2)),
- :clip_id => clip_id, :created_at => d.days.ago)
- clip_log.save
- end
- end
- Clip.rebuild_rating_cache
- end
- end
Add Comment
Please, Sign In to add comment