2012-06-21 02:25:35 +04:00
|
|
|
require 'rubygems'
|
2012-07-14 10:31:05 +04:00
|
|
|
require 'mongo'
|
|
|
|
require 'mongoid'
|
2012-06-21 02:25:35 +04:00
|
|
|
require 'yaml'
|
|
|
|
require 'logger'
|
2012-07-15 02:09:10 +04:00
|
|
|
require 'active_support/all'
|
2012-07-14 10:31:05 +04:00
|
|
|
require 'sinatra'
|
|
|
|
require 'mongoid/tree'
|
|
|
|
require 'voteable_mongo'
|
2012-06-21 02:25:35 +04:00
|
|
|
|
|
|
|
desc "Load the environment"
|
|
|
|
task :environment do
|
|
|
|
env = ENV["SINATRA_ENV"] || "development"
|
2012-07-14 10:31:05 +04:00
|
|
|
Sinatra::Base.environment = env
|
|
|
|
Mongoid.load!("config/mongoid.yml")
|
|
|
|
Mongoid.logger.level = Logger::INFO
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
namespace :db do
|
|
|
|
task :seed => :environment do
|
2012-07-14 10:31:05 +04:00
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
require_relative 'models/comment.rb'
|
|
|
|
require_relative 'models/comment_thread.rb'
|
2012-06-22 22:28:15 +04:00
|
|
|
require_relative 'models/user.rb'
|
2012-07-15 02:09:10 +04:00
|
|
|
require_relative 'models/commentable.rb'
|
2012-07-14 10:31:05 +04:00
|
|
|
|
2012-07-15 02:09:10 +04:00
|
|
|
Commentable.delete_all
|
2012-06-21 02:25:35 +04:00
|
|
|
Comment.delete_all
|
|
|
|
CommentThread.delete_all
|
2012-06-22 22:28:15 +04:00
|
|
|
User.delete_all
|
2012-07-14 10:31:05 +04:00
|
|
|
|
|
|
|
beginning_time = Time.now
|
|
|
|
|
2012-06-27 02:59:08 +04:00
|
|
|
level_limit = YAML.load_file("config/application.yml")["level_limit"]
|
2012-06-28 00:54:31 +04:00
|
|
|
|
2012-07-14 10:31:05 +04:00
|
|
|
user = User.create!(external_id: "1")
|
|
|
|
|
|
|
|
def generate_comments(commentable_type, commentable_id, level_limit, user)
|
2012-07-15 02:09:10 +04:00
|
|
|
commentable = Commentable.create!(commentable_type: commentable_type, commentable_id: commentable_id)
|
2012-06-24 04:13:00 +04:00
|
|
|
5.times do
|
2012-07-15 02:09:10 +04:00
|
|
|
comment_thread = commentable.comment_threads.new(
|
2012-07-14 10:31:05 +04:00
|
|
|
commentable_type: commentable_type, commentable_id: commentable_id,
|
|
|
|
body: "This is a post", title: "Post No.#{rand(10)}",
|
|
|
|
course_id: "1")
|
|
|
|
comment_thread.author = user
|
|
|
|
comment_thread.save!
|
|
|
|
3.times do
|
2012-07-15 02:09:10 +04:00
|
|
|
comment = comment_thread.comments.new(body: "top comment", course_id: "1")
|
2012-07-14 10:31:05 +04:00
|
|
|
comment.author = user
|
|
|
|
comment.endorsed = [true, false].sample
|
|
|
|
comment.save!
|
|
|
|
end
|
2012-07-15 02:09:10 +04:00
|
|
|
10.times do
|
2012-07-14 10:31:05 +04:00
|
|
|
comment = Comment.where(comment_thread_id: comment_thread.id).reject{|c| c.depth >= level_limit}.sample
|
2012-07-15 02:09:10 +04:00
|
|
|
sub_comment = comment.children.new(body: "comment body", course_id: "1")
|
2012-07-14 10:31:05 +04:00
|
|
|
sub_comment.author = user
|
|
|
|
sub_comment.endorsed = [true, false].sample
|
|
|
|
sub_comment.save!
|
|
|
|
end
|
|
|
|
puts "Generating a comment thread for #{commentable_type} No.#{commentable_id}"
|
2012-06-24 04:13:00 +04:00
|
|
|
end
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
2012-06-28 00:54:31 +04:00
|
|
|
|
2012-07-14 10:31:05 +04:00
|
|
|
generate_comments("questions" , 1, level_limit, user)
|
|
|
|
generate_comments("questions" , 2, level_limit, user)
|
|
|
|
generate_comments("courses" , 1, level_limit, user)
|
|
|
|
generate_comments("lectures" , 1, level_limit, user)
|
|
|
|
generate_comments("lectures" , 2, level_limit, user)
|
2012-07-15 02:09:10 +04:00
|
|
|
|
2012-07-14 10:31:05 +04:00
|
|
|
puts "voting"
|
|
|
|
users = []
|
2012-07-15 02:09:10 +04:00
|
|
|
(1..10).each do |id|
|
2012-07-14 10:31:05 +04:00
|
|
|
users << User.find_or_create_by(external_id: id.to_s)
|
|
|
|
end
|
|
|
|
|
2012-07-15 02:09:10 +04:00
|
|
|
CommentThread.all.each do |c|
|
|
|
|
(0...10).each do |i|
|
|
|
|
users[i].vote(c, [:up, :down].sample)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-14 10:31:05 +04:00
|
|
|
Comment.all.each do |c|
|
2012-07-15 02:09:10 +04:00
|
|
|
(0...10).each do |i|
|
2012-07-14 10:31:05 +04:00
|
|
|
users[i].vote(c, [:up, :down].sample)
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
end
|
2012-07-14 10:31:05 +04:00
|
|
|
|
|
|
|
end_time = Time.now
|
|
|
|
|
|
|
|
puts "Number of comments generated: #{Comment.count}"
|
|
|
|
puts "Number of comment threads generated: #{CommentThread.count}"
|
|
|
|
|
|
|
|
puts "Time elapsed #{(end_time - beginning_time)*1000} milliseconds"
|
|
|
|
|
2012-06-21 02:25:35 +04:00
|
|
|
end
|
|
|
|
end
|