Recalculate last reply of thread on message deletion #968

This commit is contained in:
Felix Schäfer 2012-05-19 11:38:40 +02:00
parent 43723385c6
commit 87f68e58ad
2 changed files with 18 additions and 0 deletions

View File

@ -80,9 +80,15 @@ class Message < ActiveRecord::Base
end
def after_destroy
parent.reset_last_reply_id! if parent
board.reset_counters!
end
def reset_last_reply_id!
clid = children.present? ? children.last.id : nil
self.update_attribute(:last_reply_id, clid)
end
def sticky=(arg)
write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
end

View File

@ -101,6 +101,18 @@ class MessageTest < ActiveSupport::TestCase
# Watchers removed
end
def test_destroy_last_reply
message = Message.find(4)
last_reply = message.last_reply
penultimate_reply = message.children[-2]
assert last_reply.destroy
message.reload
assert_equal penultimate_reply, message.last_reply
end
def test_destroy_reply
message = Message.find(5)
board = message.board