]> source.dussan.org Git - redmine.git/commitdiff
Code cleanup, use array instead of handcrafted update.
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 22 Oct 2014 18:51:00 +0000 (18:51 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 22 Oct 2014 18:51:00 +0000 (18:51 +0000)
git-svn-id: http://svn.redmine.org/redmine/trunk@13485 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/board.rb
test/unit/board_test.rb

index 4a9984a03f1da07508cbc8c9e7f2e4319c23b630..9777771a5eb4735806994dad2a0f6d7548707bae 100644 (file)
@@ -63,10 +63,10 @@ class Board < ActiveRecord::Base
   # Updates topics_count, messages_count and last_message_id attributes for +board_id+
   def self.reset_counters!(board_id)
     board_id = board_id.to_i
-    where(["id = ?", board_id]).
-      update_all("topics_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id} AND parent_id IS NULL)," +
-               " messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id})," +
-               " last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=#{board_id})")
+    Board.where(:id => board_id).
+      update_all(["topics_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=:id AND parent_id IS NULL)," +
+               " messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=:id)," +
+               " last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=:id)", :id => board_id])
   end
 
   def self.board_tree(boards, parent_id=nil, level=0)
index 480977c9a8bcb6773bcebc1506a856fbe3fabce6..a14cdd6e8c95ca454a68768fc66f4d6b083b2a60 100644 (file)
@@ -113,4 +113,13 @@ class BoardTest < ActiveSupport::TestCase
     assert_nil child.parent
     assert_nil child.parent_id
   end
+
+  def test_reset_counters_should_update_attributes
+    Board.where(:id => 1).update_all(:topics_count => 0, :messages_count => 0, :last_message_id => 0)
+    Board.reset_counters!(1)
+    board = Board.find(1)
+    assert_equal board.topics.count, board.topics_count
+    assert_equal board.messages.count, board.messages_count
+    assert_equal board.messages.order("id DESC").first.id, board.last_message_id
+  end
 end