Guest User

Untitled

a guest
Jun 3rd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. namespace :db do
  2.  
  3. desc "Fill database with fake data"
  4. task :populate => :environment do
  5. require 'faker'
  6.  
  7. User.find(:all).each{|u| u.destroy}
  8.  
  9. max_id = User.maximum("id") || 1
  10. random_users = lambda {|*args|
  11. count = args.shift
  12. User.find(:all, :limit => count, :conditions =>
  13. {:id => (1..count).map {|i| rand(max_id)}})
  14. }
  15.  
  16. any = lambda{|array| array[rand(array.size)] }
  17.  
  18. 1.upto(40000) do
  19. name = "#{Faker::Name.name}_#{max_id}"
  20. login = name.downcase.gsub(' ','_').gsub('.','').gsub('\'', '_')
  21. puts login
  22. email = Faker::Internet.email.gsub(/@/, "_#{max_id}@")
  23. user = User.create!(
  24. :email => email,
  25. :login => login,
  26. :name => name,
  27. :new_password => 'password',
  28. :new_password_confirmation => 'password'
  29. )
  30. user.activate!(true)
  31.  
  32. user.friends = random_users[5] if max_id > 40
  33. max_id += 1
  34. end if User.count < 10000
  35.  
  36. users = User.connection.select_values("SELECT id FROM users").
  37. map {|id| id.to_i}
  38.  
  39. puts "Generated #{User.count} users"
  40.  
  41.  
  42. Guild.all.each do |guild|
  43. next if guild.blog_editors.count > 0
  44. guild.blog_editors = random_users[5]
  45. guild.users = random_users[5000] if guild.users.empty?
  46. 1.upto(5) do
  47. guild.clubs.create(:title => Faker::Company.name,
  48. :access_mode => Club::ACCESS_PUBLIC,
  49. :visibility_mode => Club::VISIBLE,
  50. :description => Faker::Company.name)
  51. guild.clubs.create(:title => Faker::Company.name,
  52. :access_mode => Club::ACCESS_INVITE,
  53. :visibility_mode => Club::VISIBLE,
  54. :description => Faker::Company.name)
  55. guild.clubs.create(:title => Faker::Company.name,
  56. :access_mode => Club::ACCESS_PUBLIC,
  57. :visibility_mode => Club::HIDDEN,
  58. :description => Faker::Company.name)
  59. guild.clubs.create(:title => Faker::Company.name,
  60. :access_mode => Club::ACCESS_INVITE,
  61. :visibility_mode => Club::HIDDEN,
  62. :description => Faker::Company.name)
  63. end if guild.clubs.count < 2
  64. end
  65.  
  66. puts "Assigned guild editors and clubs"
  67.  
  68. Club.all.each do |club|
  69. 40.times do
  70. fellowship = club.fellowships.build(:user_id => any[users],
  71. :mode => Fellowship::MODE_ACTIVE)
  72. fellowship.role = Fellowship::VALID_ROLES[
  73. rand(Fellowship::VALID_ROLES.size)]
  74. fellowship.save
  75. end if club.fellowships.count < 5
  76. end
  77.  
  78. ideas = []
  79. Genre.all.each do |genre|
  80. random_users[500].each do |user|
  81. user.ideas.create!(
  82. :genre_id => genre.id,
  83. :name => Faker::Company.name,
  84. :description => Faker::Name.name,
  85. :access_mode => rand(Idea::ACCESS_PRIVATE)
  86. )
  87. end
  88. end if Idea.count < 5000
  89. ideas = Idea.connection.select_values("SELECT id FROM ideas").
  90. map {|id| id.to_i}
  91. puts "Created #{ideas.size} ideas"
  92.  
  93. assets = []
  94. 10000.times do
  95. idea = Idea.find(any[ideas])
  96. Asset.create(:user_id => idea.user_id,
  97. :name => Faker::Name.name, :idea_id => idea.id)
  98. Audio.create(:user_id => idea.user_id,
  99. :name => Faker::Name.name, :idea_id => idea.id)
  100. Image.create(:user_id => idea.user_id,
  101. :name => Faker::Name.name, :idea_id => idea.id)
  102. Video.create(:user_id => idea.user_id,
  103. :name => Faker::Name.name, :idea_id => idea.id)
  104. end if Asset.count < 10000
  105. assets = Asset.connection.
  106. select_values("SELECT id FROM assets").map{|id| id.to_i}
  107. puts "Created #{assets.size} assets"
  108.  
  109. 4000.times do |index|
  110. Project.create!(:idea_id => any[ideas],
  111. :title => "#{Faker::Company.name} #{index}")
  112. end if Project.count < 4000
  113. projects = Project.connection.
  114. select_values("SELECT id FROM projects").map {|id| id.to_i}
  115.  
  116. puts "Created #{projects.size} projects"
  117.  
  118. 2000.times do
  119. project = Project.find(any[projects])
  120. asset = Video.create! :project_id => project.id,
  121. :idea_id => project.idea_id, :user_id => project.users.first.id,
  122. :name => Faker::Name.name
  123. Clip.create!(:project_id => project.id, :asset_id => asset.id,
  124. :user_id => asset.user_id, :genre_id => asset.idea.genre_id,
  125. :title => Faker::Name.name, :description => Faker::Company.name)
  126. end if Clip.count < 2000
  127. clips = Clip.connection.select_values("SELECT id FROM clips").
  128. map {|id| id.to_i}
  129.  
  130. puts "Created #{clips.size} clips"
  131. ClipLog.class_eval do
  132. define_method(:calculate_rating) {}
  133. define_method(:update_clip_rating) {}
  134. end
  135. ClipLog.delete_all
  136.  
  137. Clip.update_all({:is_cataloguing => true, :state => "distributing"})
  138.  
  139. clips.each do |clip_id|
  140. 60.downto(1) do |d|
  141. aquisitions = rand(40)
  142. clip_log = ClipLog.new(:catalog_views_count => rand(1000),
  143. :mobile_views_count => rand(500),
  144. :aquisitions_count => aquisitions,
  145. :income => aquisitions*(4+rand(2)),
  146. :clip_id => clip_id, :created_at => d.days.ago)
  147. clip_log.save
  148. end
  149. end
  150. Clip.rebuild_rating_cache
  151. end
  152. end
Add Comment
Please, Sign In to add comment