validates_length_of :subject, :maximum => 255
validate :cannot_reply_to_locked_topic, :on => :create
- after_create :add_author_as_watcher, :update_parent_last_reply
+ after_create :add_author_as_watcher, :reset_counters!
after_update :update_messages_board
- after_destroy :reset_board_counters
+ after_destroy :reset_counters!
scope :visible, lambda {|*args| { :include => {:board => :project},
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
errors.add :base, 'Topic is locked' if root.locked? && self != root
end
- def update_parent_last_reply
- if parent
- parent.reload.update_attribute(:last_reply_id, self.id)
- end
- board.reset_counters!
- end
-
def update_messages_board
if board_id_changed?
Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
end
end
- def reset_board_counters
+ def reset_counters!
+ if parent && parent.id
+ Message.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
+ end
board.reset_counters!
end
assert_equal messages_count - 1, board.messages_count
end
+ def test_destroying_last_reply_should_update_topic_last_reply_id
+ topic = Message.find(4)
+ assert_equal 6, topic.last_reply_id
+
+ assert_difference 'Message.count', -1 do
+ Message.find(6).destroy
+ end
+ assert_equal 5, topic.reload.last_reply_id
+
+ assert_difference 'Message.count', -1 do
+ Message.find(5).destroy
+ end
+ assert_nil topic.reload.last_reply_id
+ end
+
def test_editable_by
message = Message.find(6)
author = message.author