Commit 73a39949 by Rocky Duan

return id instead of _id to hide mongo implementation detail

parent 6bb64416
...@@ -47,7 +47,8 @@ class Comment < Content ...@@ -47,7 +47,8 @@ class Comment < Content
if params[:recursive] if params[:recursive]
self.class.hash_tree(subtree(sort: sort_by_parent_and_time)).first self.class.hash_tree(subtree(sort: sort_by_parent_and_time)).first
else else
as_document.slice(*%w[body course_id endorsed _id created_at updated_at]). as_document.slice(*%w[body course_id endorsed created_at updated_at]).
merge("id" => _id).
merge("user_id" => author.id). merge("user_id" => author.id).
merge("votes" => votes.slice(*%w[count up_count down_count point])) merge("votes" => votes.slice(*%w[count up_count down_count point]))
end end
......
...@@ -37,7 +37,8 @@ class CommentThread < Content ...@@ -37,7 +37,8 @@ class CommentThread < Content
end end
def to_hash(params={}) def to_hash(params={})
doc = as_document.slice(*%w[title body course_id _id created_at updated_at]). doc = as_document.slice(*%w[title body course_id created_at updated_at]).
merge("id" => _id).
merge("user_id" => (author.id if author)). merge("user_id" => (author.id if author)).
merge("votes" => votes.slice(*%w[count up_count down_count point])) merge("votes" => votes.slice(*%w[count up_count down_count point]))
if params[:recursive] if params[:recursive]
......
...@@ -17,6 +17,6 @@ class Notification ...@@ -17,6 +17,6 @@ class Notification
has_and_belongs_to_many :receivers, class_name: "User", inverse_of: :notifications, autosave: true has_and_belongs_to_many :receivers, class_name: "User", inverse_of: :notifications, autosave: true
def to_hash(params={}) def to_hash(params={})
as_document.slice(*%w[_id notification_type info actor target]) as_document.slice(*%w[notification_type info actor target]).merge("id" => _id)
end end
end end
...@@ -9,7 +9,7 @@ class Subscription ...@@ -9,7 +9,7 @@ class Subscription
index [[:subscriber_id, Mongo::ASCENDING], [:source_id, Mongo::ASCENDING], [:source_type, Mongo::ASCENDING]] index [[:subscriber_id, Mongo::ASCENDING], [:source_id, Mongo::ASCENDING], [:source_type, Mongo::ASCENDING]]
def to_hash def to_hash
as_document as_document.slice(*%w[subscriber_id source_id source_type])
end end
def subscriber def subscriber
......
...@@ -199,9 +199,9 @@ describe "app" do ...@@ -199,9 +199,9 @@ describe "app" do
it "create a comment to the comment thread" do it "create a comment to the comment thread" do
thread = CommentThread.first.to_hash(recursive: true) thread = CommentThread.first.to_hash(recursive: true)
user = User.first user = User.first
post "/api/v1/threads/#{thread["_id"]}/comments", body: "new comment", course_id: "1", user_id: User.first.id post "/api/v1/threads/#{thread["id"]}/comments", body: "new comment", course_id: "1", user_id: User.first.id
last_response.should be_ok last_response.should be_ok
changed_thread = CommentThread.find(thread["_id"]).to_hash(recursive: true) changed_thread = CommentThread.find(thread["id"]).to_hash(recursive: true)
changed_thread["children"].length.should == thread["children"].length + 1 changed_thread["children"].length.should == thread["children"].length + 1
comment = changed_thread["children"].select{|c| c["body"] == "new comment"}.first comment = changed_thread["children"].select{|c| c["body"] == "new comment"}.first
comment.should_not be_nil comment.should_not be_nil
...@@ -211,7 +211,7 @@ describe "app" do ...@@ -211,7 +211,7 @@ describe "app" do
describe "DELETE /api/v1/threads/:thread_id" do describe "DELETE /api/v1/threads/:thread_id" do
it "delete the comment thread and its comments" do it "delete the comment thread and its comments" do
thread = CommentThread.first.to_hash thread = CommentThread.first.to_hash
delete "/api/v1/threads/#{thread['_id']}" delete "/api/v1/threads/#{thread['id']}"
last_response.should be_ok last_response.should be_ok
CommentThread.where(title: thread["title"]).first.should be_nil CommentThread.where(title: thread["title"]).first.should be_nil
end end
...@@ -228,7 +228,7 @@ describe "app" do ...@@ -228,7 +228,7 @@ describe "app" do
retrieved = parse last_response.body retrieved = parse last_response.body
retrieved["body"].should == comment.body retrieved["body"].should == comment.body
retrieved["endorsed"].should == comment.endorsed retrieved["endorsed"].should == comment.endorsed
retrieved["_id"].should == comment.id.to_s retrieved["id"].should == comment.id.to_s
retrieved["children"].should be_nil retrieved["children"].should be_nil
retrieved["votes"]["point"].should == comment.votes_point retrieved["votes"]["point"].should == comment.votes_point
end end
...@@ -239,7 +239,7 @@ describe "app" do ...@@ -239,7 +239,7 @@ describe "app" do
retrieved = parse last_response.body retrieved = parse last_response.body
retrieved["body"].should == comment.body retrieved["body"].should == comment.body
retrieved["endorsed"].should == comment.endorsed retrieved["endorsed"].should == comment.endorsed
retrieved["_id"].should == comment.id.to_s retrieved["id"].should == comment.id.to_s
retrieved["votes"]["point"].should == comment.votes_point retrieved["votes"]["point"].should == comment.votes_point
retrieved["children"].length.should == comment.children.length retrieved["children"].length.should == comment.children.length
retrieved["children"].select{|c| c["body"] == comment.children.first.body}.first.should_not be_nil retrieved["children"].select{|c| c["body"] == comment.children.first.body}.first.should_not be_nil
...@@ -259,9 +259,9 @@ describe "app" do ...@@ -259,9 +259,9 @@ describe "app" do
it "create a sub comment to the comment" do it "create a sub comment to the comment" do
comment = Comment.first.to_hash(recursive: true) comment = Comment.first.to_hash(recursive: true)
user = User.first user = User.first
post "/api/v1/comments/#{comment["_id"]}", body: "new comment", course_id: "1", user_id: User.first.id post "/api/v1/comments/#{comment["id"]}", body: "new comment", course_id: "1", user_id: User.first.id
last_response.should be_ok last_response.should be_ok
changed_comment = Comment.find(comment["_id"]).to_hash(recursive: true) changed_comment = Comment.find(comment["id"]).to_hash(recursive: true)
changed_comment["children"].length.should == comment["children"].length + 1 changed_comment["children"].length.should == comment["children"].length + 1
subcomment = changed_comment["children"].select{|c| c["body"] == "new comment"}.first subcomment = changed_comment["children"].select{|c| c["body"] == "new comment"}.first
subcomment.should_not be_nil subcomment.should_not be_nil
......
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