Commit 774887b4 by jmclaus

Make thread_type editable.

parent 07866196
...@@ -52,7 +52,7 @@ end ...@@ -52,7 +52,7 @@ end
put "#{APIPREFIX}/threads/:thread_id" do |thread_id| put "#{APIPREFIX}/threads/:thread_id" do |thread_id|
filter_blocked_content params["body"] filter_blocked_content params["body"]
thread.update_attributes(params.slice(*%w[title body closed commentable_id group_id])) thread.update_attributes(params.slice(*%w[title body closed commentable_id group_id thread_type]))
if thread.errors.any? if thread.errors.any?
error 400, thread.errors.full_messages.to_json error 400, thread.errors.full_messages.to_json
......
...@@ -53,7 +53,7 @@ class CommentThread < Content ...@@ -53,7 +53,7 @@ class CommentThread < Content
has_many :comments, dependent: :destroy#, autosave: true# Use destroy to envoke callback on the top-level comments TODO async has_many :comments, dependent: :destroy#, autosave: true# Use destroy to envoke callback on the top-level comments TODO async
has_many :activities, autosave: true has_many :activities, autosave: true
attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :anonymous_to_peers, :closed attr_accessible :title, :body, :course_id, :commentable_id, :anonymous, :anonymous_to_peers, :closed, :thread_type
validates_presence_of :thread_type validates_presence_of :thread_type
validates_presence_of :title validates_presence_of :title
...@@ -64,6 +64,7 @@ class CommentThread < Content ...@@ -64,6 +64,7 @@ class CommentThread < Content
before_create :set_last_activity_at before_create :set_last_activity_at
before_update :set_last_activity_at, :unless => lambda { closed_changed? } before_update :set_last_activity_at, :unless => lambda { closed_changed? }
after_update :clear_endorsements
before_destroy :destroy_subscriptions before_destroy :destroy_subscriptions
...@@ -141,6 +142,18 @@ private ...@@ -141,6 +142,18 @@ private
self.last_activity_at = Time.now.utc unless last_activity_at_changed? self.last_activity_at = Time.now.utc unless last_activity_at_changed?
end end
def clear_endorsements
if self.thread_type_changed?
# We use 'set' instead of 'update_attributes' because the Comment model has a 'before_update' callback that sets
# the last activity time on the thread. Therefore the callbacks would be mutually recursive and we end up with a
# 'SystemStackError'. The 'set' method skips callbacks and therefore bypasses this issue.
self.comments.each do |comment|
comment.set :endorsed, false
comment.set :endorsement, nil
end
end
end
def destroy_subscriptions def destroy_subscriptions
subscriptions.delete_all subscriptions.delete_all
end end
......
...@@ -556,12 +556,20 @@ describe "app" do ...@@ -556,12 +556,20 @@ describe "app" do
it "update information of comment thread" do it "update information of comment thread" do
thread = CommentThread.first thread = CommentThread.first
put "/api/v1/threads/#{thread.id}", body: "new body", title: "new title", commentable_id: "new_commentable_id" comment = thread.comments.first
comment.endorsed = true
comment.endorsement = {:user_id => "42", :time => DateTime.now}
comment.save
put "/api/v1/threads/#{thread.id}", body: "new body", title: "new title", commentable_id: "new_commentable_id", thread_type: "question"
last_response.should be_ok last_response.should be_ok
changed_thread = CommentThread.find(thread.id) changed_thread = CommentThread.find(thread.id)
changed_thread.body.should == "new body" changed_thread.body.should == "new body"
changed_thread.title.should == "new title" changed_thread.title.should == "new title"
changed_thread.commentable_id.should == "new_commentable_id" changed_thread.commentable_id.should == "new_commentable_id"
changed_thread.thread_type.should == "question"
comment.reload
comment.endorsed.should == false
comment.endorsement.should == nil
check_thread_result_json(nil, changed_thread, parse(last_response.body)) check_thread_result_json(nil, changed_thread, parse(last_response.body))
end end
it "returns 400 when the thread does not exist" do it "returns 400 when the thread does not exist" 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