Commit 3cb0a48872e0d762b77d0dab7262ff357ac615e6

Authored by AntonioTerceiro
1 parent 2944ada6

ActionItem466: adapting CategoryFinder

Searching with the new categorization scheme


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2072 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/category_finder.rb
... ... @@ -2,10 +2,10 @@ class CategoryFinder
2 2  
3 3 def initialize(cat)
4 4 @category = cat
5   - @category_ids = @category.map_traversal(&:id)
  5 + @category_id = @category.id
6 6 end
7 7  
8   - attr_reader :category_ids
  8 + attr_reader :category_id
9 9  
10 10  
11 11  
... ... @@ -43,7 +43,7 @@ class CategoryFinder
43 43 def current_events(year, month)
44 44 range = Event.date_range(year, month)
45 45  
46   - Event.find(:all, :include => :categories, :conditions => { 'categories.id' => category_ids, :start_date => range })
  46 + Event.find(:all, :include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range })
47 47 end
48 48  
49 49 protected
... ... @@ -57,15 +57,17 @@ class CategoryFinder
57 57  
58 58 case klass.name
59 59 when 'Comment'
60   - {:select => 'distinct comments.*', :joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id in (?)', category_ids]}.merge!(options)
  60 + {:select => 'distinct comments.*', :joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options)
61 61 when 'Product'
62 62 if prod_cat_ids
63   - {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and products.product_category_id in (?)', category_ids, prod_cat_ids]}.merge!(options)
  63 + {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and products.product_category_id = (?)', category_id, prod_cat_ids]}.merge!(options)
64 64 else
65   - {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?)', category_ids]}.merge!(options)
  65 + {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options)
66 66 end
67   - when 'Article', 'Person', 'Community', 'Enterprise', 'Event'
68   - {:include => 'categories', :conditions => ['categories.id IN (?)', category_ids]}.merge!(options)
  67 + when 'Article', 'Event'
  68 + {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options)
  69 + when 'Person', 'Community', 'Enterprise'
  70 + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options)
69 71 else
70 72 raise "unreconized class #{klass.name}"
71 73 end
... ... @@ -75,9 +77,11 @@ class CategoryFinder
75 77 # FIXME copy/pasted from options_for_find above !!!
76 78 case klass.name
77 79 when 'Product'
78   - {:select => 'distinct products.*', :joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and (products.name like (?) or products.name like (?))', category_ids, initial + '%', initial.upcase + '%']}
79   - when 'Article', 'Person', 'Community', 'Enterprise'
80   - {:include => 'categories', :conditions => ['categories.id IN (?) and (%s.name like (?) or %s.name like (?))' % [klass.table_name, klass.table_name], category_ids, initial + '%', initial.upcase + '%']}
  80 + {:select => 'distinct products.*', :joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id = (?) and (products.name like (?) or products.name like (?))', category_id, initial + '%', initial.upcase + '%']}
  81 + when 'Article'
  82 + {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => ['articles_categories.category_id = (?) and (%s.name like (?) or %s.name like (?))' % [klass.table_name, klass.table_name], category_id, initial + '%', initial.upcase + '%']}
  83 + when 'Person', 'Community', 'Enterprise'
  84 + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?) and (%s.name like (?) or %s.name like (?))' % [klass.table_name, klass.table_name], category_id, initial + '%', initial.upcase + '%']}
81 85 else
82 86 raise "unreconized class #{klass.name}"
83 87 end
... ...
test/unit/category_finder_test.rb
... ... @@ -83,27 +83,60 @@ class CategoryFinderTest < ActiveSupport::TestCase
83 83 assert_not_includes list, prod2
84 84 end
85 85  
86   - should 'load ids for category full hierarchy' do
87   - c1 = Category.create!(:name => 'parent', :environment => Environment.default)
88   - c2 = Category.create!(:name => 'child 1', :environment => Environment.default, :parent => c1)
89   - c3 = Category.create!(:name => 'grandchild', :environment => Environment.default, :parent => c2)
90   - c4 = Category.create!(:name => 'child 2', :environment => Environment.default, :parent => c1)
91   - c5 = Category.create!(:name => 'grandchild 2', :environment => Environment.default, :parent => c4)
  86 + should 'search people in category hierarchy' do
  87 + parent = Category.create!(:name => 'parent category', :environment => Environment.default)
  88 + child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent)
  89 + p1 = create_user('people_1').person
  90 + p1.name = 'a beautiful person'
  91 + p1.add_category(child)
  92 + p1.save!
92 93  
93   - c1.reload
  94 + parent.reload
94 95  
95   - assert_equivalent [c1,c2,c3,c4,c5].map(&:id), CategoryFinder.new(c1).category_ids
  96 + f = CategoryFinder.new(parent)
  97 + assert_includes f.find(:people, 'beautiful'), p1
96 98 end
97 99  
98   - should 'search in category hierarchy' do
  100 + should 'search article in category hierarchy' do
99 101 parent = Category.create!(:name => 'parent category', :environment => Environment.default)
100 102 child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent)
101   - p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.add_category(child); p1.save!
  103 +
  104 + p1 = create_user('people_1').person
  105 +
  106 + article = p1.articles.create!(:name => 'a beautiful article', :category_ids => [child.id])
102 107  
103 108 parent.reload
104 109  
105 110 f = CategoryFinder.new(parent)
106   - assert_includes f.find(:people, 'beautiful'), p1
  111 + assert_includes f.find(:articles, 'beautiful'), article
  112 + end
  113 +
  114 + should 'find communites by initial in category hierarchy' do
  115 + parent = Category.create!(:name => 'parent category', :environment => Environment.default)
  116 + child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent)
  117 + p1 = create_user('people_1').person
  118 + p1.name = 'person with inner beaity'
  119 + p1.add_category(child)
  120 + p1.save!
  121 +
  122 + parent.reload
  123 +
  124 + f = CategoryFinder.new(parent)
  125 + assert_includes f.find_by_initial(:people, 'p'), p1
  126 + end
  127 +
  128 + should 'find articles by initial in category hierarchy' do
  129 + parent = Category.create!(:name => 'parent category', :environment => Environment.default)
  130 + child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent)
  131 +
  132 + p1 = create_user('people_1').person
  133 +
  134 + article = p1.articles.create!(:name => 'fucking beautiful article', :category_ids => [child.id])
  135 +
  136 + parent.reload
  137 +
  138 + f = CategoryFinder.new(parent)
  139 + assert_includes f.find_by_initial(:articles, 'f'), article
107 140 end
108 141  
109 142 should 'list recent enterprises' do
... ...