add task for generating seed for a single commentable

This commit is contained in:
Rocky Duan 2012-07-23 21:03:24 -04:00
Родитель 58015e805a
Коммит cc221a113d
1 изменённых файлов: 83 добавлений и 69 удалений

152
Rakefile
Просмотреть файл

@ -84,6 +84,80 @@ namespace :db do
Subscription.delete_all
end
def generate_comments_for(commentable_id)
level_limit = YAML.load_file("config/application.yml")["level_limit"]
thread_seeds = [
{title: "This is really interesting", body: "best I've ever seen!"},
{title: "We can probably make this better", body: "Let's do it"},
{title: "I don't know where to start", body: "Can anyone help me?"},
{title: "I'm here!", body: "Haha I'm the first one who discovered this"},
{title: "I need five threads but I don't know what to put here", body: "So I'll just leave it this way"},
]
comment_body_seeds = [
"dude I don't know what you're talking about",
"hi I'm Jack",
"hi just sent you a message",
"let's discuss this further",
"can't agree more",
"haha",
"lol",
]
users = User.all.to_a
puts "Generating threads and comments for #{commentable_id}..."
threads = []
comments = []
thread_seeds.each do |thread_seed|
comment_thread = CommentThread.new(commentable_id: commentable_id, body: thread_seed[:body], title: thread_seed[:title], course_id: "1")
comment_thread.author = users.sample
comment_thread.save!
threads << comment_thread
3.times do
comment = comment_thread.comments.new(body: comment_body_seeds.sample, course_id: "1")
comment.author = users.sample
comment.endorsed = [true, false].sample
comment.save!
comments << comment
end
10.times do
comment = Comment.where(comment_thread_id: comment_thread.id).reject{|c| c.depth >= level_limit}.sample
sub_comment = comment.children.new(body: comment_body_seeds.sample, course_id: "1")
sub_comment.author = users.sample
sub_comment.endorsed = [true, false].sample
sub_comment.save!
comments << sub_comment
end
end
puts "voting for these threads & comments.."
threads.each do |c|
users.each do |user|
user.vote(c, [:up, :down].sample)
end
end
comments.each do |c|
users.each do |user|
user.vote(c, [:up, :down].sample)
end
end
puts "finished"
end
task :generate_comments, [:commentable_id] => :environment do |t, args|
generate_comments_for(args[:commentable_id])
end
task :seed => :environment do
Comment.delete_all
@ -94,80 +168,20 @@ namespace :db do
beginning_time = Time.now
level_limit = YAML.load_file("config/application.yml")["level_limit"]
users = (1..10).map {|id| User.find_or_create_by(external_id: id.to_s)}
10.times do
users.sample.subscribe(users.sample)
end
THREAD_SEEDS = [
{title: "This is really interesting", body: "best I've ever seen!"},
{title: "We can probably make this better", body: "Let's do it"},
{title: "I don't know where to start", body: "Can anyone help me?"},
{title: "I'm here!", body: "Haha I'm the first one who discovered this"},
{title: "I need five threads but I don't know what to put here", body: "So I'll just leave it this way"},
]
COMMENT_BODY_SEEDS = [
"dude I don't know what you're talking about",
"hi I'm Jack",
"hi just sent you a message",
"let's discuss this further",
"can't agree more",
"haha",
"lol",
]
def generate_comments(commentable_id, level_limit, users)
THREAD_SEEDS.each do |thread_seed|
comment_thread = CommentThread.new(commentable_id: commentable_id, body: thread_seed[:body], title: thread_seed[:title], course_id: "1")
comment_thread.author = users.sample
comment_thread.save!
3.times do
comment = comment_thread.comments.new(body: COMMENT_BODY_SEEDS.sample, course_id: "1")
comment.author = users.sample
comment.endorsed = [true, false].sample
comment.save!
end
10.times do
comment = Comment.where(comment_thread_id: comment_thread.id).reject{|c| c.depth >= level_limit}.sample
sub_comment = comment.children.new(body: COMMENT_BODY_SEEDS.sample, course_id: "1")
sub_comment.author = users.sample
sub_comment.endorsed = [true, false].sample
sub_comment.save!
end
puts "Generating a comment thread for #{commentable_id}"
end
end
generate_comments("question_1", level_limit, users)
generate_comments("question_2", level_limit, users)
generate_comments("course_1", level_limit, users)
generate_comments("lecture_1", level_limit, users)
generate_comments("video_1", level_limit, users)
generate_comments("video_2", level_limit, users)
generate_comments("lab_1", level_limit, users)
generate_comments("lab_2", level_limit, users)
puts "voting"
users = []
(1..10).each do |id|
users << User.find_or_create_by(external_id: id.to_s)
end
CommentThread.all.each do |c|
(0...10).each do |i|
users[i].vote(c, [:up, :down].sample)
end
end
Comment.all.each do |c|
(0...10).each do |i|
users[i].vote(c, [:up, :down].sample)
end
end
generate_comments_for("question_1")
generate_comments_for("question_2")
generate_comments_for("course_1")
generate_comments_for("lecture_1")
generate_comments_for("video_1")
generate_comments_for("video_2")
generate_comments_for("lab_1")
generate_comments_for("lab_2")
end_time = Time.now