diff app/models/message.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
line wrap: on
line diff
--- a/app/models/message.rb	Wed Jan 23 13:11:25 2013 +0000
+++ b/app/models/message.rb	Thu Jun 20 13:14:14 2013 +0100
@@ -1,5 +1,5 @@
 # Redmine - project management software
-# Copyright (C) 2006-2011  Jean-Philippe Lang
+# Copyright (C) 2006-2012  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -41,11 +41,11 @@
   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!
 
-  named_scope :visible, lambda {|*args| { :include => {:board => :project},
+  scope :visible, lambda {|*args| { :include => {:board => :project},
                                           :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
 
   safe_attributes 'subject', 'content'
@@ -63,13 +63,6 @@
     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 @@
     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