eBooru/db/seeds.rb

199 lines
4.4 KiB
Ruby
Raw Normal View History

2013-04-16 23:23:02 -04:00
require 'set'
require 'timecop'
2013-04-16 23:23:02 -04:00
2012-09-21 15:41:05 -04:00
CurrentUser.ip_addr = "127.0.0.1"
2013-01-22 14:41:16 -05:00
Delayed::Worker.delay_jobs = false
2013-04-16 23:23:02 -04:00
$used_names = Set.new([""])
def rand_string(n, unique = false)
2013-04-16 23:23:02 -04:00
string = ""
n = rand(n) + 1
while $used_names.include?(string)
consonants = "bcdfghjklmnpqrstvwxz".scan(/./)
vowels = "aeiouy".scan(/./)
string = ""
n.times do
string << consonants[rand(consonants.size)]
string << vowels[rand(vowels.size)]
end
return string unless unique
2013-04-16 23:23:02 -04:00
end
$used_names.add(string)
string
end
def rand_sentence(n, unique = false)
(0..n).map {rand_string(n, unique)}.join(" ") + "."
2013-04-16 23:23:02 -04:00
end
def rand_paragraph(n, unique = false)
(0..n).map {rand_sentence(n, unique)}.join(" ")
2013-04-16 23:23:02 -04:00
end
def rand_document(n, unique = false)
(0..n).map {rand_paragraph(n, unique)}.join("\n\n")
2013-04-16 23:23:02 -04:00
end
2012-09-21 15:41:05 -04:00
2011-07-03 19:12:31 -04:00
if User.count == 0
puts "Creating users"
Timecop.travel(1.month.ago) do
user = User.create(
:name => "admin",
:password => "password1",
:password_confirmation => "password1"
)
end
2013-03-19 08:10:10 -04:00
2013-04-16 23:23:02 -04:00
0.upto(10) do |i|
2011-07-29 18:04:50 -04:00
User.create(
:name => rand_string(8, true),
2013-04-21 14:36:43 -04:00
:password => "password1",
:password_confirmation => "password1"
2011-07-29 18:04:50 -04:00
)
end
$used_names = Set.new([""])
2011-07-03 19:12:31 -04:00
else
puts "Skipping users"
2012-09-21 15:41:05 -04:00
user = User.find_by_name("albert")
2011-07-03 19:12:31 -04:00
end
CurrentUser.user = User.admins.first
2011-07-03 19:12:31 -04:00
if Upload.count == 0
puts "Creating uploads"
1.upto(100) do |i|
2011-09-16 15:39:08 -04:00
color1 = rand(4096).to_s(16)
color2 = rand(4096).to_s(16)
width = rand(2000) + 100
height = rand(2000) + 100
2012-09-21 15:41:05 -04:00
url = "http://ipsumimage.appspot.com/#{width}x#{height}"
tags = rand_sentence(12).scan(/[a-z]+/).join(" ")
2013-03-19 08:10:10 -04:00
Upload.create!(:source => url, :content_type => "image/gif", :rating => "q", :tag_string => tags, :server => Socket.gethostname)
2011-07-03 19:12:31 -04:00
end
else
puts "Skipping uploads"
end
if Post.count == 0
puts "Creating posts"
Upload.all.each do |upload|
upload.process!
end
if Post.count == 0
raise "Uploads failed conversion"
end
2011-07-03 19:12:31 -04:00
else
puts "Skipping posts"
end
if Comment.count == 0
puts "Creating comments"
Post.all.each do |post|
rand(10).times do
2013-04-16 23:23:02 -04:00
Comment.create(:post_id => post.id, :body => rand_paragraph(6))
2011-09-11 16:40:58 -04:00
end
2011-07-03 19:12:31 -04:00
end
else
puts "Skipping comments"
end
if Note.count == 0
puts "Creating notes"
Post.order("random()").limit(10).each do |post|
rand(5).times do
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)
2013-03-19 08:10:10 -04:00
rand(10).times do |i|
2013-04-16 23:23:02 -04:00
note.update_attributes(:body => rand_sentence(6))
end
2011-07-03 19:12:31 -04:00
end
end
else
puts "Skipping notes"
end
if Artist.count == 0
puts "Creating artists"
20.times do |i|
Artist.create(:name => rand_string(9, true))
2011-07-03 19:12:31 -04:00
end
$used_names = Set.new([""])
2011-07-03 19:12:31 -04:00
else
puts "Skipping artists"
end
if TagAlias.count == 0
puts "Creating tag aliases"
2013-03-19 08:10:10 -04:00
20.times do |i|
TagAlias.create(:antecedent_name => rand_string(9, true), :consequent_name => rand_string(9, true))
2011-07-03 19:12:31 -04:00
end
$used_names = Set.new([""])
2011-07-03 19:12:31 -04:00
else
puts "Skipping tag aliases"
end
if TagImplication.count == 0
puts "Creating tag implictions"
20.times do |i|
TagImplication.create(:antecedent_name => rand_string(9, true), :consequent_name => rand_string(9, true))
2011-07-03 19:12:31 -04:00
end
$used_names = Set.new([""])
2011-07-03 19:12:31 -04:00
else
puts "Skipping tag implications"
2011-07-09 03:32:18 -04:00
end
if Pool.count == 0
puts "Creating pools"
2013-03-19 08:10:10 -04:00
2011-07-09 03:32:18 -04:00
1.upto(20) do |i|
pool = Pool.create(:name => rand_string(9, true))
2011-10-16 01:40:42 -04:00
rand(33).times do |j|
2011-07-09 03:32:18 -04:00
pool.add!(Post.order("random()").first)
end
end
$used_names = Set.new([""])
2011-07-09 03:32:18 -04:00
end
2011-07-29 18:04:50 -04:00
if Favorite.count == 0
puts "Creating favorites"
Post.order("random()").limit(50).each do |post|
user = User.order("random()").first
post.add_favorite!(user)
post.add_favorite!(CurrentUser.user)
end
2013-03-19 08:10:10 -04:00
else
2011-07-29 18:04:50 -04:00
puts "Skipping favorites"
end
2011-10-16 01:40:42 -04:00
if ForumTopic.count == 0
puts "Creating forum posts"
2013-03-19 08:10:10 -04:00
20.times do |i|
2013-04-16 23:23:02 -04:00
topic = ForumTopic.create(:title => rand_sentence(6))
2013-03-19 08:10:10 -04:00
2011-10-16 01:40:42 -04:00
rand(100).times do |j|
2013-04-16 23:23:02 -04:00
post = ForumPost.create(:topic_id => topic.id, :body => rand_document(6))
2011-10-16 01:40:42 -04:00
end
end
end
2011-07-29 18:04:50 -04:00
if TagSubscription.count == 0
puts "Creating tag subscriptions"
TagSubscription.create(:name => "0", :tag_query => Tag.order("random()").first.name)
1.upto(50) do |i|
CurrentUser.user = User.order("random()").first
TagSubscription.create(:name => i.to_s, :tag_query => Tag.order("random()").first.name)
end
else
puts "Skipping tag subscriptions"
end