]> source.dussan.org Git - redmine.git/commitdiff
Fixed that deleting the last reply of a topic does not update last_reply_id.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 7 Jul 2012 15:09:57 +0000 (15:09 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 7 Jul 2012 15:09:57 +0000 (15:09 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9939 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/message.rb
test/unit/message_test.rb

index b3dd73c253dfdc2bc8cd9ee4b64244d466888978..80fff2cf98815150298b8073d63f6902b401c1db 100644 (file)
@@ -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
 
index 8be6de31a0724f2d086f7b624ad42f3fb4316fe1..00b7351ee3d0eb95cccb3eaa6224af8178dd1193 100644 (file)
@@ -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