Commit 635303f0 by Ibrahim Awwal

Update generate_comments_for so that it actually generates multiple levels of…

Update generate_comments_for so that it actually generates multiple levels of comments, up to the maximum nesting level.
parent 17870591
......@@ -63,8 +63,8 @@ namespace :db do
COURSE_ID = "MITx/6.002x/2012_Fall"
def generate_comments_for(commentable_id)
level_limit = YAML.load(application_yaml)["level_limit"]
def generate_comments_for(commentable_id, num_threads=THREADS_PER_COMMENTABLE, num_top_comments=TOP_COMMENTS_PER_THREAD, num_subcomments=ADDITIONAL_COMMENTS_PER_THREAD)
level_limit = CommentService.config["level_limit"]
tag_seeds = [
"artificial-intelligence",
......@@ -83,7 +83,7 @@ namespace :db do
top_comments = []
additional_comments = []
THREADS_PER_COMMENTABLE.times do
num_threads.times do
inner_top_comments = []
comment_thread = CommentThread.new(commentable_id: commentable_id, body: Faker::Lorem.paragraphs.join("\n\n"), title: Faker::Lorem.sentence(6))
......@@ -93,7 +93,7 @@ namespace :db do
comment_thread.save!
threads << comment_thread
users.sample(3).each {|user| user.subscribe(comment_thread)}
(1 + rand(TOP_COMMENTS_PER_THREAD)).times do
(1 + rand(num_top_comments)).times do
comment = comment_thread.comments.new(body: Faker::Lorem.paragraph(2))
comment.author = users.sample
comment.endorsed = [true, false].sample
......@@ -103,15 +103,20 @@ namespace :db do
top_comments << comment
inner_top_comments << comment
end
(1 + rand(ADDITIONAL_COMMENTS_PER_THREAD)).times do
comment = inner_top_comments.sample
previous_level_comments = inner_top_comments
(level_limit-1).times do
current_level_comments = []
(1 + rand(num_subcomments)).times do
comment = previous_level_comments.sample
sub_comment = comment.children.new(body: Faker::Lorem.paragraph(2))
sub_comment.author = users.sample
sub_comment.endorsed = [true, false].sample
sub_comment.comment_thread = comment_thread
sub_comment.course_id = COURSE_ID
sub_comment.save!
additional_comments << sub_comment
current_level_comments << sub_comment
end
previous_level_comments = current_level_comments
end
end
......@@ -126,9 +131,11 @@ namespace :db do
end
task :generate_comments, [:commentable_id] => :environment do |t, args|
generate_comments_for(args[:commentable_id])
task :generate_comments, [:commentable_id, :num_threads, :num_top_comments, :num_subcomments] => :environment do |t, args|
args.with_defaults(:num_threads => THREADS_PER_COMMENTABLE,
:num_top_comments=>TOP_COMMENTS_PER_THREAD,
:num_subcomments=> ADDITIONAL_COMMENTS_PER_THREAD)
generate_comments_for(args[:commentable_id], args[:num_threads], args[:num_top_comments], args[:num_subcomments])
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment