Commit 2dded1a2 by Rocky Duan

include depth information

parent 0c9a488c
......@@ -27,15 +27,16 @@ namespace :db do
Vote.delete_all
User.delete_all
depth_limit = YAML.load_file("config/application.yml")["depth_limit"]
comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => 1
5.times do
comment_thread.root_comments.create :body => "top comment", :title => "top #{rand(10)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id
end
10.times do
comment = Comment.all.reject{|c| c.is_root? or c.depth - 1 >= depth_limit}.sample
comment.children.create :body => "comment body", :title => "comment title #{rand(50)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id
(1..3).each do |question_id|
comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => question_id
5.times do
comment_thread.root_comments.create :body => "top comment", :title => "top #{rand(10)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id
end
10.times do
comment = Comment.all.reject{|c| c.is_root? or c.depth - 1 >= depth_limit or c.comment_thread_id != comment_thread.id}.sample
comment.children.create :body => "comment body", :title => "comment title #{rand(50)}", :user_id => 1, :course_id => 1, :comment_thread_id => comment_thread.id
end
end
Comment.all.reject{|c| c.is_root?}.each do |c|
(1..20).each do |id|
user = User.find_or_create_by_id(id)
......
......@@ -18,7 +18,7 @@ class Comment < ActiveRecord::Base
validates_presence_of :comment_thread_id
def self.hash_tree(nodes)
nodes.map {|node, sub_nodes| node.to_hash.merge(:children => hash_tree(sub_nodes).compact)}
nodes.map {|node, sub_nodes| node.to_hash.merge(:children => hash_tree(sub_nodes).compact, :depth => node.depth)}
end
def to_hash_tree
......@@ -26,7 +26,7 @@ class Comment < ActiveRecord::Base
end
def to_hash
attributes.merge(:votes => {:up => votes_for, :down => votes_against})
attributes.merge(:votes => {:up => votes_for, :down => votes_against, :plusminus => plusminus})
end
def to_json
......
......@@ -91,11 +91,13 @@ describe "app" do
c["created_at"].should_not be_nil
c["updated_at"].should_not be_nil
c["children"].length.should == 2
c["depth"].should == 1
children = c["children"].reject{|comment| comment["title"] != "comment title 0"}.first
children.should_not be_nil
children["id"].should == sub_comment[0].id
children["created_at"].should_not be_nil
children["updated_at"].should_not be_nil
children["depth"].should == 2
end
end
describe "GET on /api/v1/comments/:comment_id" do
......
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