From 7bf96ee69029a6b683fd19c1f6ce27874006cb7e Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 17 Oct 2013 13:23:38 +1100 Subject: [PATCH] naive implementation of post_count on categories --- app/models/category.rb | 25 ++++++++++++++++--- ...1017014509_add_post_count_to_categories.rb | 14 +++++++++++ lib/sql_builder.rb | 2 +- spec/models/category_spec.rb | 4 ++- 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20131017014509_add_post_count_to_categories.rb diff --git a/app/models/category.rb b/app/models/category.rb index 2432b3a1c..df86fe8d0 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -102,18 +102,35 @@ class Category < ActiveRecord::Base # all categories. def self.update_stats topics = Topic - .select("COUNT(*)") + .select("COUNT(*) topic_count") .where("topics.category_id = categories.id") .where("categories.topic_id <> topics.id") .visible - topic_count = topics.to_sql + topics_with_post_count = Topic + .select("topics.category_id, topics.id topic_id, COUNT(*) topic_count, SUM(topics.posts_count) post_count") + .group("topics.category_id, topics.id") + .visible.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("topic_count = (#{topic_count}), - topics_year = (#{topics_year}), + + Category.exec_sql < x.topic_count OR c.post_count <> x.post_count) AND + x.topic_id <> c.topic_id + +SQL + + + # TODO don't update unchanged data + Category.update_all("topics_year = (#{topics_year}), topics_month = (#{topics_month}), topics_week = (#{topics_week})") end diff --git a/db/migrate/20131017014509_add_post_count_to_categories.rb b/db/migrate/20131017014509_add_post_count_to_categories.rb new file mode 100644 index 000000000..34773c24a --- /dev/null +++ b/db/migrate/20131017014509_add_post_count_to_categories.rb @@ -0,0 +1,14 @@ +class AddPostCountToCategories < ActiveRecord::Migration + def up + add_column :categories, :post_count, :integer, null: false, default: 0 + execute < :value_to_boolean } - def map_exec(klass, args = {}) + def map_exec(klass = OpenStruct, args = {}) results = exec(args) setters = results.fields.each_with_index.map do |f, index| diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 75dcb9c46..329f5d0dd 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -255,7 +255,7 @@ describe Category do context 'with regular topics' do before do - @category.topics << Fabricate(:topic, user: @category.user) + create_post(user: @category.user, category: @category.name) Category.update_stats @category.reload end @@ -265,6 +265,7 @@ describe Category do @category.topics_month.should == 1 @category.topics_year.should == 1 @category.topic_count.should == 1 + @category.post_count.should == 1 end end @@ -282,6 +283,7 @@ describe Category do @category.topic_count.should == 0 @category.topics_month.should == 0 @category.topics_year.should == 0 + @category.post_count.should == 0 end end