diff --git a/app/models/category.rb b/app/models/category.rb index 6ceea24..ff7f32c 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -44,5 +44,8 @@ class Category < ActiveRecord::Base self.articles.most_commented(limit) end + def total_items + articles.count + comments.count + enterprises.count + people.count + communities.count + products.count + end end diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 11c6fe5..a61e5d8 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -20,7 +20,7 @@ class CategoryFinder end def count(asset) - asset_class(asset).count(:all, options_for_find(asset_class(asset))) + asset_class(asset).count(:all, options_for_find(asset_class(asset), :select => "#{asset_table(asset)}.id")) end def most_commented_articles(limit=10) @@ -30,7 +30,11 @@ class CategoryFinder protected def find_in_categorized(klass, query, options={}) - klass.find_by_contents(query, {}, options_for_find(klass, options)).uniq + if query.nil? + klass.find(:all, options_for_find(klass, options)) + else + klass.find_by_contents(query, {}, options_for_find(klass, options)).uniq + end end def options_for_find(klass, options={}) diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index f1e521c..785f989 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -23,6 +23,22 @@ class CategoryFinderTest < ActiveSupport::TestCase assert_includes list, art1 assert_not_includes list, art2 end + + should 'search with query for articles in a specific category' do + person = create_user('teste').person + + # in category + art1 = person.articles.build(:name => 'an article to be found') + art1.categories << @category + art1.save! + + # not in category + art2 = person.articles.build(:name => 'another article to be found') + art2.save! + + assert_includes @finder.find('articles', 'found'), art1 + assert_not_includes @finder.find('articles','found'), art2 + end should 'search for comments in a specific category' do person = create_user('teste').person diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 8d5560e..13dc800 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -346,6 +346,27 @@ class CategoryTest < Test::Unit::TestCase assert_not_includes c.people, ent end + should 'report the total items in this category' do + @category = Category.create!(:name => 'my category', :environment => @env) + # in category + person1 = create_user('test1').person; person1.categories << @category; person1.save! + art1 = person1.articles.build(:name => 'an article to be counted'); art1.categories << @category; art1.save! + comment1 = art1.comments.build(:title => 'comment to be counted', :body => 'hfyfyh', :author => person1); comment1.save! + ent1 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :categories => [@category]) + com1 = Community.create!(:name => 'test3', :identifier => 'test3', :categories => [@category]) + prod1 = Product.create!(:name => 'test4', :enterprise => ent1) + + # not in category + person2 = create_user('test5').person + art2 = person2.articles.build(:name => 'an article not to be counted'); art2.save! + comment2 = art2.comments.build(:title => 'comment not to be counted', :body => 'hfh', :author => person2); comment2.save! + ent2 = Enterprise.create!(:name => 'test6', :identifier => 'test6') + com2 = Community.create!(:name => 'test7', :identifier => 'test7') + prod2 = Product.create!(:name => 'test8', :enterprise => ent2) + + assert_equal 6, @category.total_items + end + # NOT YET #should 'list people that are categorized in children categories' do # c1 = @env.categories.create!(:name => 'top category') -- libgit2 0.21.2