From d50b5e1fd8503ff6300fa34a6f34ae12a1ed7835 Mon Sep 17 00:00:00 2001
From: Kuba Brecka <kuba.brecka@gmail.com>
Date: Sun, 31 Mar 2013 13:22:05 +0200
Subject: [PATCH] make sure Category.topic_count is consistent with week/year
 stats

---
 app/models/category.rb       | 4 +++-
 spec/models/category_spec.rb | 8 ++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/app/models/category.rb b/app/models/category.rb
index d1c2f8ae6..9a23f10ac 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -61,11 +61,13 @@ class Category < ActiveRecord::Base
                .where("categories.topic_id <> topics.id")
                .visible
 
+    topic_count = topics.to_sql
     topics_year = topics.created_since(1.year.ago).to_sql
     topics_month = topics.created_since(1.month.ago).to_sql
     topics_week = topics.created_since(1.week.ago).to_sql
 
-    Category.update_all("topics_year = (#{topics_year}),
+    Category.update_all("topic_count = (#{topic_count}),
+                         topics_year = (#{topics_year}),
                          topics_month = (#{topics_month}),
                          topics_week = (#{topics_week})")
   end
diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb
index 7cf935e45..b70cb9c9c 100644
--- a/spec/models/category_spec.rb
+++ b/spec/models/category_spec.rb
@@ -177,6 +177,10 @@ describe Category do
       it 'updates topics_year' do
         @category.topics_year.should == 1
       end
+
+      it 'updates topic_count' do
+        @category.topic_count.should == 1
+      end
     end
 
     context 'with deleted topics' do
@@ -198,6 +202,10 @@ describe Category do
       it 'does not count deleted topics for topics_year' do
         @category.topics_year.should == 0
       end
+
+      it 'does not count deleted topics for topic_count' do
+        @category.topic_count.should == 0
+      end
     end
   end
 end