summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/message.rb16
-rw-r--r--test/unit/message_test.rb15
2 files changed, 21 insertions, 10 deletions
diff --git a/app/models/message.rb b/app/models/message.rb
index b3dd73c25..80fff2cf9 100644
--- a/app/models/message.rb
+++ b/app/models/message.rb
@@ -41,9 +41,9 @@ class Message < ActiveRecord::Base
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) } }
@@ -63,13 +63,6 @@ class Message < ActiveRecord::Base
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])
@@ -78,7 +71,10 @@ class Message < ActiveRecord::Base
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
diff --git a/test/unit/message_test.rb b/test/unit/message_test.rb
index 8be6de31a..00b7351ee 100644
--- a/test/unit/message_test.rb
+++ b/test/unit/message_test.rb
@@ -133,6 +133,21 @@ class MessageTest < ActiveSupport::TestCase
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