From b9c7e3b74a22614b453b7e97637222a5978fddaf Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 25 May 2015 17:42:16 -0400 Subject: [PATCH] FIX: categories page counts were very wrong for categories with sub-categories --- .../category_detailed_serializer.rb | 2 +- .../category_detailed_serializer_spec.rb | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 spec/serializers/category_detailed_serializer_spec.rb diff --git a/app/serializers/category_detailed_serializer.rb b/app/serializers/category_detailed_serializer.rb index b1d68d1bebe..16b028ca066 100644 --- a/app/serializers/category_detailed_serializer.rb +++ b/app/serializers/category_detailed_serializer.rb @@ -74,7 +74,7 @@ class CategoryDetailedSerializer < BasicCategorySerializer def count_with_subcategories(method) count = object.send(method) || 0 object.subcategories.each do |category| - count += (object.send(method) || 0) + count += (category.send(method) || 0) end count end diff --git a/spec/serializers/category_detailed_serializer_spec.rb b/spec/serializers/category_detailed_serializer_spec.rb new file mode 100644 index 00000000000..67c14af3705 --- /dev/null +++ b/spec/serializers/category_detailed_serializer_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' +require_dependency 'category' + +describe CategoryDetailedSerializer do + + describe "counts" do + it "works for categories with no subcategories" do + no_subcats = Fabricate(:category, topics_year: 10, topics_month: 5, topics_day: 2, posts_year: 13, posts_month: 7, posts_day: 3) + json = CategoryDetailedSerializer.new(no_subcats, scope: Guardian.new, root: false).as_json + json[:topics_year].should == 10 + json[:topics_month].should == 5 + json[:topics_day].should == 2 + json[:posts_year].should == 13 + json[:posts_month].should == 7 + json[:posts_day].should == 3 + end + + it "includes counts from subcategories" do + parent = Fabricate(:category, topics_year: 10, topics_month: 5, topics_day: 2, posts_year: 13, posts_month: 7, posts_day: 3) + subcategory = Fabricate(:category, parent_category_id: parent.id, topics_year: 1, topics_month: 1, topics_day: 1, posts_year: 1, posts_month: 1, posts_day: 1) + json = CategoryDetailedSerializer.new(parent, scope: Guardian.new, root: false).as_json + json[:topics_year].should == 11 + json[:topics_month].should == 6 + json[:topics_day].should == 3 + json[:posts_year].should == 14 + json[:posts_month].should == 8 + json[:posts_day].should == 4 + end + end + +end