Advertisement
SilverMysth

Untitled

Jun 1st, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. danbooru@linux:~/danbooru$ cat db/seeds.rb
  2. require 'set'
  3. require 'timecop'
  4.  
  5. CurrentUser.ip_addr = "127.0.0.1"
  6. Delayed::Worker.delay_jobs = false
  7. $used_names = Set.new([""])
  8.  
  9. def rand_string(n, unique = false)
  10. string = ""
  11.  
  12. n = rand(n) + 1
  13.  
  14. while $used_names.include?(string)
  15. consonants = "bcdfghjklmnpqrstvwxz".scan(/./)
  16. vowels = "aeiouy".scan(/./)
  17. string = ""
  18. n.times do
  19. string << consonants[rand(consonants.size)]
  20. string << vowels[rand(vowels.size)]
  21. end
  22. return string unless unique
  23. end
  24.  
  25. $used_names.add(string)
  26. string
  27. end
  28.  
  29. def rand_sentence(n, unique = false)
  30. (0..n).map {rand_string(n, unique)}.join(" ") + "."
  31. end
  32.  
  33. def rand_paragraph(n, unique = false)
  34. (0..n).map {rand_sentence(n, unique)}.join(" ")
  35. end
  36.  
  37. def rand_document(n, unique = false)
  38. (0..n).map {rand_paragraph(n, unique)}.join("\n\n")
  39. end
  40.  
  41. if User.count == 0
  42. puts "Creating users"
  43.  
  44. Timecop.travel(1.month.ago) do
  45. user = User.create(
  46. :name => "admin",
  47. :password => "password1",
  48. :password_confirmation => "password1"
  49. )
  50. end
  51.  
  52. 0.upto(10) do |i|
  53. User.create(
  54. :name => rand_string(8, true),
  55. :password => "password1",
  56. :password_confirmation => "password1"
  57. )
  58. end
  59. $used_names = Set.new([""])
  60. else
  61. puts "Skipping users"
  62. user = User.find_by_name("albert")
  63. end
  64.  
  65. CurrentUser.user = User.admins.first
  66.  
  67. if Upload.count == 0
  68. puts "Creating uploads"
  69. 1.upto(100) do |i|
  70. color1 = rand(4096).to_s(16)
  71. color2 = rand(4096).to_s(16)
  72. width = rand(2000) + 100
  73. height = rand(2000) + 100
  74. url = "http://ipsumimage.appspot.com/#{width}x#{height}"
  75. tags = rand_sentence(12).scan(/[a-z]+/).join(" ")
  76.  
  77. Upload.create!(:source => url, :content_type => "image/gif", :rating => "q", :tag_string => tags, :server => Socket.gethostname)
  78. end
  79. else
  80. puts "Skipping uploads"
  81. end
  82.  
  83. if Post.count == 0
  84. puts "Creating posts"
  85. Upload.all.each do |upload|
  86. upload.process!
  87. end
  88. if Post.count == 0
  89. raise "Uploads failed conversion"
  90. end
  91. else
  92. puts "Skipping posts"
  93. end
  94.  
  95. if Comment.count == 0
  96. puts "Creating comments"
  97. Post.all.each do |post|
  98. rand(10).times do
  99. Comment.create(:post_id => post.id, :body => rand_paragraph(6))
  100. end
  101. end
  102. else
  103. puts "Skipping comments"
  104. end
  105.  
  106. if Note.count == 0
  107. puts "Creating notes"
  108. Post.order("random()").limit(10).each do |post|
  109. rand(5).times do
  110. note = Note.create(:post_id => post.id, :x => rand(post.image_width), :y => rand(post.image_height), :width => 100, :height => 100, :body => Time.now.to_f.to_s)
  111.  
  112. rand(10).times do |i|
  113. note.update_attributes(:body => rand_sentence(6))
  114. end
  115. end
  116. end
  117. else
  118. puts "Skipping notes"
  119. end
  120.  
  121. if Artist.count == 0
  122. puts "Creating artists"
  123. 20.times do |i|
  124. Artist.create(:name => rand_string(9, true))
  125. end
  126. $used_names = Set.new([""])
  127. else
  128. puts "Skipping artists"
  129. end
  130.  
  131. if TagAlias.count == 0
  132. puts "Creating tag aliases"
  133.  
  134. 20.times do |i|
  135. TagAlias.create(:antecedent_name => rand_string(9, true), :consequent_name => rand_string(9, true))
  136. end
  137. $used_names = Set.new([""])
  138. else
  139. puts "Skipping tag aliases"
  140. end
  141.  
  142. if TagImplication.count == 0
  143. puts "Creating tag implictions"
  144.  
  145. 20.times do |i|
  146. TagImplication.create(:antecedent_name => rand_string(9, true), :consequent_name => rand_string(9, true))
  147. end
  148. $used_names = Set.new([""])
  149. else
  150. puts "Skipping tag implications"
  151. end
  152.  
  153. if Pool.count == 0
  154. puts "Creating pools"
  155.  
  156. 1.upto(20) do |i|
  157. pool = Pool.create(:name => rand_string(9, true))
  158. rand(33).times do |j|
  159. pool.add!(Post.order("random()").first)
  160. end
  161. end
  162. $used_names = Set.new([""])
  163. end
  164.  
  165. if Favorite.count == 0
  166. puts "Creating favorites"
  167.  
  168. Post.order("random()").limit(50).each do |post|
  169. user = User.order("random()").first
  170. post.add_favorite!(user)
  171. post.add_favorite!(CurrentUser.user)
  172. end
  173. else
  174. puts "Skipping favorites"
  175. end
  176.  
  177. if ForumTopic.count == 0
  178. puts "Creating forum posts"
  179.  
  180. 20.times do |i|
  181. topic = ForumTopic.create(:title => rand_sentence(6))
  182.  
  183. rand(100).times do |j|
  184. post = ForumPost.create(:topic_id => topic.id, :body => rand_document(6))
  185. end
  186. end
  187. end
  188.  
  189. if TagSubscription.count == 0
  190. puts "Creating tag subscriptions"
  191. TagSubscription.create(:name => "0", :tag_query => Tag.order("random()").first.name)
  192. 1.upto(50) do |i|
  193. CurrentUser.user = User.order("random()").first
  194. TagSubscription.create(:name => i.to_s, :tag_query => Tag.order("random()").first.name)
  195. end
  196. else
  197. puts "Skipping tag subscriptions"
  198. end
  199.  
  200. danbooru@linux:~/danbooru$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement