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 ...@@ -63,8 +63,8 @@ namespace :db do
COURSE_ID = "MITx/6.002x/2012_Fall" COURSE_ID = "MITx/6.002x/2012_Fall"
def generate_comments_for(commentable_id) 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 = YAML.load(application_yaml)["level_limit"] level_limit = CommentService.config["level_limit"]
tag_seeds = [ tag_seeds = [
"artificial-intelligence", "artificial-intelligence",
...@@ -83,7 +83,7 @@ namespace :db do ...@@ -83,7 +83,7 @@ namespace :db do
top_comments = [] top_comments = []
additional_comments = [] additional_comments = []
THREADS_PER_COMMENTABLE.times do num_threads.times do
inner_top_comments = [] inner_top_comments = []
comment_thread = CommentThread.new(commentable_id: commentable_id, body: Faker::Lorem.paragraphs.join("\n\n"), title: Faker::Lorem.sentence(6)) 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 ...@@ -93,7 +93,7 @@ namespace :db do
comment_thread.save! comment_thread.save!
threads << comment_thread threads << comment_thread
users.sample(3).each {|user| user.subscribe(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 = comment_thread.comments.new(body: Faker::Lorem.paragraph(2))
comment.author = users.sample comment.author = users.sample
comment.endorsed = [true, false].sample comment.endorsed = [true, false].sample
...@@ -103,15 +103,20 @@ namespace :db do ...@@ -103,15 +103,20 @@ namespace :db do
top_comments << comment top_comments << comment
inner_top_comments << comment inner_top_comments << comment
end end
(1 + rand(ADDITIONAL_COMMENTS_PER_THREAD)).times do previous_level_comments = inner_top_comments
comment = inner_top_comments.sample (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 = comment.children.new(body: Faker::Lorem.paragraph(2))
sub_comment.author = users.sample sub_comment.author = users.sample
sub_comment.endorsed = [true, false].sample sub_comment.endorsed = [true, false].sample
sub_comment.comment_thread = comment_thread sub_comment.comment_thread = comment_thread
sub_comment.course_id = COURSE_ID sub_comment.course_id = COURSE_ID
sub_comment.save! sub_comment.save!
additional_comments << sub_comment current_level_comments << sub_comment
end
previous_level_comments = current_level_comments
end end
end end
...@@ -126,9 +131,11 @@ namespace :db do ...@@ -126,9 +131,11 @@ namespace :db do
end end
task :generate_comments, [:commentable_id] => :environment do |t, args| task :generate_comments, [:commentable_id, :num_threads, :num_top_comments, :num_subcomments] => :environment do |t, args|
args.with_defaults(:num_threads => THREADS_PER_COMMENTABLE,
generate_comments_for(args[:commentable_id]) :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 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