Commit 685778b1 by Rocky Duan

added validation for comments if not super comment

parent a7918b01
...@@ -22,8 +22,7 @@ end ...@@ -22,8 +22,7 @@ end
# create a new top-level comment # create a new top-level comment
post '/api/v1/commentables/:commentable_type/:commentable_id/comments' do |commentable_type, commentable_id| post '/api/v1/commentables/:commentable_type/:commentable_id/comments' do |commentable_type, commentable_id|
comment_thread = CommentThread.find_or_create_by_commentable_type_and_commentable_id(commentable_type, commentable_id) comment_thread = CommentThread.find_or_create_by_commentable_type_and_commentable_id(commentable_type, commentable_id)
comment_params = params.select {|key, value| %w{body title user_id course_id}.include? key} comment_params = params.select {|key, value| %w{body title user_id course_id}.include? key}.merge({:comment_thread_id => comment_thread.id})
comment_params.merge :comment_thread_id => comment_thread.id
comment = comment_thread.root_comments.create(comment_params) comment = comment_thread.root_comments.create(comment_params)
if comment.valid? if comment.valid?
comment.to_json comment.to_json
...@@ -49,8 +48,7 @@ post '/api/v1/comments/:comment_id' do |comment_id| ...@@ -49,8 +48,7 @@ post '/api/v1/comments/:comment_id' do |comment_id|
if comment.nil? or comment.is_root? if comment.nil? or comment.is_root?
error 400, {:error => "invalid comment id"}.to_json error 400, {:error => "invalid comment id"}.to_json
else else
comment_params = params.select {|key, value| %w{body title user_id course_id}.include? key} comment_params = params.select {|key, value| %w{body title user_id course_id}.include? key}.merge({:comment_thread_id => comment.comment_thread_id})
comment_params.merge :comment_thread_id => comment.root.comment_thread.id
sub_comment = comment.children.create(comment_params) sub_comment = comment.children.create(comment_params)
if comment.valid? if comment.valid?
comment.to_json comment.to_json
......
...@@ -11,6 +11,11 @@ class Comment < ActiveRecord::Base ...@@ -11,6 +11,11 @@ class Comment < ActiveRecord::Base
belongs_to :comment_thread belongs_to :comment_thread
validates_presence_of :body, :unless => :is_root?
validates_presence_of :user_id, :unless => :is_root?
validates_presence_of :course_id, :unless => :is_root?
validates_presence_of :comment_thread_id
def self.hash_tree(nodes) def self.hash_tree(nodes)
nodes.map do |node, sub_nodes| nodes.map do |node, sub_nodes|
{ {
......
...@@ -168,8 +168,10 @@ describe "app" do ...@@ -168,8 +168,10 @@ describe "app" do
end end
describe "PUT on /api/v1/votes/comments/:comment_id/users/:user_id" do describe "PUT on /api/v1/votes/comments/:comment_id/users/:user_id" do
before :each do before :each do
CommentThread.create! :commentable_type => "questions", :commentable_id => 1 comment_thread = CommentThread.create! :commentable_type => "questions", :commentable_id => 1
CommentThread.first.root_comments.create :body => "top comment", :title => "top", :user_id => 1, :course_id => 1 comment = CommentThread.first.root_comments.create :body => "top comment", :title => "top", :user_id => 1, :course_id => 1
comment.comment_thread = comment_thread
comment.save!
end end
it "votes up on a comment" do it "votes up on a comment" do
comment = CommentThread.first.comments.first comment = CommentThread.first.comments.first
......
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