Skip to content

Commit

Permalink
naive implementation of post_count on categories
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Oct 18, 2013
1 parent 9ad01a1 commit 7bf96ee
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
25 changes: 21 additions & 4 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,35 @@ def self.scoped_to_permissions(guardian, permission_types)
# 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 <<SQL
UPDATE categories c
SET topic_count = x.topic_count,
post_count = x.post_count
FROM (#{topics_with_post_count}) x
WHERE x.category_id = c.id AND
(c.topic_count <> 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
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20131017014509_add_post_count_to_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class AddPostCountToCategories < ActiveRecord::Migration
def up
add_column :categories, :post_count, :integer, null: false, default: 0
execute <<SQL
UPDATE categories
SET post_count = (SELECT SUM(posts_count) FROM topics
WHERE category_id = categories.id AND deleted_at IS NULL)
SQL
end

def down
remove_column :categories, :post_count
end
end
2 changes: 1 addition & 1 deletion lib/sql_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def exec(args = {})
16 => :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|
Expand Down
4 changes: 3 additions & 1 deletion spec/models/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@

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
Expand All @@ -265,6 +265,7 @@
@category.topics_month.should == 1
@category.topics_year.should == 1
@category.topic_count.should == 1
@category.post_count.should == 1
end

end
Expand All @@ -282,6 +283,7 @@
@category.topic_count.should == 0
@category.topics_month.should == 0
@category.topics_year.should == 0
@category.post_count.should == 0
end

end
Expand Down

0 comments on commit 7bf96ee

Please sign in to comment.