Commit e66e02bf227bfd9d2f5a73c991652a108ea1a728

Authored by MoisesMachado
1 parent 12588c61

ActionItem252: added suport for counting how many item are categorized by a category


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1765 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/category.rb
@@ -44,5 +44,8 @@ class Category < ActiveRecord::Base @@ -44,5 +44,8 @@ class Category < ActiveRecord::Base
44 self.articles.most_commented(limit) 44 self.articles.most_commented(limit)
45 end 45 end
46 46
  47 + def total_items
  48 + articles.count + comments.count + enterprises.count + people.count + communities.count + products.count
  49 + end
47 50
48 end 51 end
app/models/category_finder.rb
@@ -20,7 +20,7 @@ class CategoryFinder @@ -20,7 +20,7 @@ class CategoryFinder
20 end 20 end
21 21
22 def count(asset) 22 def count(asset)
23 - asset_class(asset).count(:all, options_for_find(asset_class(asset))) 23 + asset_class(asset).count(:all, options_for_find(asset_class(asset), :select => "#{asset_table(asset)}.id"))
24 end 24 end
25 25
26 def most_commented_articles(limit=10) 26 def most_commented_articles(limit=10)
@@ -30,7 +30,11 @@ class CategoryFinder @@ -30,7 +30,11 @@ class CategoryFinder
30 protected 30 protected
31 31
32 def find_in_categorized(klass, query, options={}) 32 def find_in_categorized(klass, query, options={})
33 - klass.find_by_contents(query, {}, options_for_find(klass, options)).uniq 33 + if query.nil?
  34 + klass.find(:all, options_for_find(klass, options))
  35 + else
  36 + klass.find_by_contents(query, {}, options_for_find(klass, options)).uniq
  37 + end
34 end 38 end
35 39
36 def options_for_find(klass, options={}) 40 def options_for_find(klass, options={})
test/unit/category_finder_test.rb
@@ -23,6 +23,22 @@ class CategoryFinderTest < ActiveSupport::TestCase @@ -23,6 +23,22 @@ class CategoryFinderTest < ActiveSupport::TestCase
23 assert_includes list, art1 23 assert_includes list, art1
24 assert_not_includes list, art2 24 assert_not_includes list, art2
25 end 25 end
  26 +
  27 + should 'search with query for articles in a specific category' do
  28 + person = create_user('teste').person
  29 +
  30 + # in category
  31 + art1 = person.articles.build(:name => 'an article to be found')
  32 + art1.categories << @category
  33 + art1.save!
  34 +
  35 + # not in category
  36 + art2 = person.articles.build(:name => 'another article to be found')
  37 + art2.save!
  38 +
  39 + assert_includes @finder.find('articles', 'found'), art1
  40 + assert_not_includes @finder.find('articles','found'), art2
  41 + end
26 42
27 should 'search for comments in a specific category' do 43 should 'search for comments in a specific category' do
28 person = create_user('teste').person 44 person = create_user('teste').person
test/unit/category_test.rb
@@ -346,6 +346,27 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -346,6 +346,27 @@ class CategoryTest &lt; Test::Unit::TestCase
346 assert_not_includes c.people, ent 346 assert_not_includes c.people, ent
347 end 347 end
348 348
  349 + should 'report the total items in this category' do
  350 + @category = Category.create!(:name => 'my category', :environment => @env)
  351 + # in category
  352 + person1 = create_user('test1').person; person1.categories << @category; person1.save!
  353 + art1 = person1.articles.build(:name => 'an article to be counted'); art1.categories << @category; art1.save!
  354 + comment1 = art1.comments.build(:title => 'comment to be counted', :body => 'hfyfyh', :author => person1); comment1.save!
  355 + ent1 = Enterprise.create!(:name => 'test2', :identifier => 'test2', :categories => [@category])
  356 + com1 = Community.create!(:name => 'test3', :identifier => 'test3', :categories => [@category])
  357 + prod1 = Product.create!(:name => 'test4', :enterprise => ent1)
  358 +
  359 + # not in category
  360 + person2 = create_user('test5').person
  361 + art2 = person2.articles.build(:name => 'an article not to be counted'); art2.save!
  362 + comment2 = art2.comments.build(:title => 'comment not to be counted', :body => 'hfh', :author => person2); comment2.save!
  363 + ent2 = Enterprise.create!(:name => 'test6', :identifier => 'test6')
  364 + com2 = Community.create!(:name => 'test7', :identifier => 'test7')
  365 + prod2 = Product.create!(:name => 'test8', :enterprise => ent2)
  366 +
  367 + assert_equal 6, @category.total_items
  368 + end
  369 +
349 # NOT YET 370 # NOT YET
350 #should 'list people that are categorized in children categories' do 371 #should 'list people that are categorized in children categories' do
351 # c1 = @env.categories.create!(:name => 'top category') 372 # c1 = @env.categories.create!(:name => 'top category')