Commit c4b2472c37dd80340dfa1e5d3ab480594e0e7c5f

Authored by MoisesMachado
1 parent 2ec6c087

ActionItem243: implemented finding communities, products, the :find_in and fixed…

… a bug where searching for people returned enterprises 


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1606 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/search_controller.rb
... ... @@ -21,13 +21,11 @@ class SearchController < ApplicationController
21 21 @filtered_query = remove_stop_words(@query)
22 22  
23 23 @finder ||= @environment
24   -
25   - @results = {
26   - :articles => search(@finder.articles, @filtered_query),
27   - :comments => search(@finder.comments, @filtered_query),
28   - :enterprises => search(@finder.enterprises, @filtered_query),
29   - :people => search(@finder.people, @filtered_query),
30   - }
  24 +
  25 + @results = {}
  26 + [:articles, :comments, :enterprises, :people, :communities, :products].each do |key|
  27 + @results[key] = search(@finder.send(key), @filtered_query) if params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s)
  28 + end
31 29 end
32 30  
33 31 before_filter :load_category, :only => :filter
... ...
app/models/category.rb
... ... @@ -26,8 +26,11 @@ class Category < ActiveRecord::Base
26 26 has_many :comments, :through => :articles
27 27  
28 28 has_many :profile_categorizations
29   - has_many :enterprises, :through => :profile_categorizations, :source => :profile
30   - has_many :people, :through => :profile_categorizations, :source => :profile
  29 + has_many :enterprises, :through => :profile_categorizations, :source => :profile, :class_name => 'Enterprise'
  30 + has_many :people, :through => :profile_categorizations, :source => :profile, :class_name => 'Person'
  31 + has_many :communities, :through => :profile_categorizations, :source => :profile, :class_name => 'Community'
  32 +
  33 + has_many :products, :through => :enterprises
31 34  
32 35 def recent_articles(limit = 10)
33 36 self.articles.recent(limit)
... ...
app/models/environment.rb
... ... @@ -56,6 +56,7 @@ class Environment < ActiveRecord::Base
56 56  
57 57 has_many :organizations
58 58 has_many :enterprises
  59 + has_many :products, :through => :enterprises
59 60 has_many :people
60 61 has_many :communities
61 62  
... ...
test/functional/search_controller_test.rb
... ... @@ -126,16 +126,16 @@ class SearchControllerTest < Test::Unit::TestCase
126 126 should 'list enterprises in a specified category'
127 127  
128 128 should 'find people' do
129   - p1 = create_user('people_1').person; p1.name = 'a beatiful person'; p1.save!
130   - get :index, :query => 'beatiful', :find_in => [ 'people' ]
  129 + p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.save!
  130 + get :index, :query => 'beautiful', :find_in => [ 'people' ]
131 131 assert_includes assigns(:results)[:people], p1
132 132 end
133 133  
134 134 should 'find people in a specific category' do
135 135 c = Category.create!(:name => 'my category', :environment => Environment.default)
136   - p1 = create_user('people_1').person; p1.name = 'a beatiful person'; p1.categories << c; p1.save!
137   - p2 = create_user('people_2').person; p2.name = 'another beatiful person'; p2.save!
138   - get :filter, :category_path => [ 'my-category' ], :query => 'beatiful', :find_in => [ 'people' ]
  136 + p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.categories << c; p1.save!
  137 + p2 = create_user('people_2').person; p2.name = 'another beautiful person'; p2.save!
  138 + get :filter, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'people' ]
139 139 assert_includes assigns(:results)[:people], p1
140 140 assert_not_includes assigns(:results)[:people], p2
141 141 end
... ... @@ -143,13 +143,42 @@ class SearchControllerTest &lt; Test::Unit::TestCase
143 143 # 'assets' menu
144 144 should 'list people in a specified category'
145 145  
146   - should 'find communities'
147   - should 'find communities in a specified category'
  146 + should 'find communities' do
  147 + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default)
  148 + get :index, :query => 'beautiful', :find_in => [ 'communities' ]
  149 + assert_includes assigns(:results)[:communities], c1
  150 + end
  151 +
  152 + should 'find communities in a specified category' do
  153 + c = Category.create!(:name => 'my category', :environment => Environment.default)
  154 + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default)
  155 + c2 = Community.create!(:name => 'another beautiful community', :identifier => 'an_bea_comm', :environment => Environment.default)
  156 + c1.categories << c; c1.save!
  157 + get :filter, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'communities' ]
  158 + assert_includes assigns(:results)[:communities], c1
  159 + assert_not_includes assigns(:results)[:communities], c2
  160 + end
148 161 # 'assets' menu
149 162 should 'list communities in a specified category'
150 163  
151   - should 'find products'
152   - should 'find products in a specific category'
  164 + should 'find products' do
  165 + ent = Enterprise.create!(:name => 'teste', :identifier => 'teste')
  166 + prod = ent.products.create!(:name => 'a beautiful product')
  167 + get 'index', :query => 'beautiful', :find_in => ['products']
  168 + assert_includes assigns(:results)[:products], prod
  169 + end
  170 +
  171 + should 'find products in a specific category' do
  172 + c = Category.create!(:name => 'my category', :environment => Environment.default)
  173 + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1'); ent1.categories << c
  174 + ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2')
  175 + prod1 = ent1.products.create!(:name => 'a beautiful product')
  176 + prod2 = ent2.products.create!(:name => 'another beautiful product')
  177 + get 'filter', :category_path => ['my-category'], :query => 'beautiful', :find_in => ['products']
  178 + assert_includes assigns(:results)[:products], prod1
  179 + assert_not_includes assigns(:results)[:products], prod2
  180 + end
  181 +
153 182 # 'assets' menu
154 183 should 'list products in a specific category'
155 184  
... ...
test/unit/category_test.rb
... ... @@ -295,4 +295,54 @@ class CategoryTest &lt; Test::Unit::TestCase
295 295 assert_equal [p1, p2], c.people
296 296 end
297 297  
  298 + should 'have communities' do
  299 + c = @env.categories.build(:name => 'my category'); c.save!
  300 + c1 = Environment.default.communities.create!(:name => 'testcommunity_1')
  301 + c1.categories << c
  302 + c2 = Environment.default.communities.create!(:name => 'testcommunity_2')
  303 + c2.categories << c
  304 + assert_equal [c1, c2], c.communities
  305 + end
  306 +
  307 + should 'have products through enteprises' do
  308 + c = @env.categories.build(:name => 'my category'); c.save!
  309 + ent1 = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
  310 + ent1.categories << c
  311 + ent2 = Enterprise.create!(:identifier => 'enterprise_2', :name => 'Enterprise one')
  312 + ent2.categories << c
  313 + prod1 = ent1.products.create!(:name => 'test_prod1')
  314 + prod2 = ent2.products.create!(:name => 'test_prod2')
  315 + assert_includes c.products, prod1
  316 + assert_includes c.products, prod2
  317 + end
  318 +
  319 + should 'not have person through communities' do
  320 + c = @env.categories.build(:name => 'my category'); c.save!
  321 + com = Community.create!(:identifier => 'community_1', :name => 'Community one')
  322 + com.categories << c
  323 + person = create_user('test_user').person
  324 + person.categories << c
  325 + assert_includes c.communities, com
  326 + assert_not_includes c.communities, person
  327 + end
  328 +
  329 + should 'not have person through enterprises' do
  330 + c = @env.categories.build(:name => 'my category'); c.save!
  331 + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
  332 + ent.categories << c
  333 + person = create_user('test_user').person
  334 + person.categories << c
  335 + assert_includes c.enterprises, ent
  336 + assert_not_includes c.enterprises, person
  337 + end
  338 +
  339 + should 'not have enterprises through people' do
  340 + c = @env.categories.build(:name => 'my category'); c.save!
  341 + person = create_user('test_user').person
  342 + person.categories << c
  343 + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
  344 + ent.categories << c
  345 + assert_includes c.people, person
  346 + assert_not_includes c.people, ent
  347 + end
298 348 end
... ...
test/unit/environment_test.rb
... ... @@ -277,4 +277,36 @@ class EnvironmentTest &lt; Test::Unit::TestCase
277 277 assert_equivalent [c1,c2], environment.comments
278 278 end
279 279  
  280 + should 'have products through enterprises' do
  281 + env = Environment.default
  282 + e1 = Enterprise.create!(:name => 'test_ent1', :identifier => 'test_ent1')
  283 + p1 = e1.products.create!(:name => 'test_prod1')
  284 +
  285 + assert_includes env.products, p1
  286 + end
  287 +
  288 + should 'not have person through communities' do
  289 + env = Environment.default
  290 + com = Community.create!(:identifier => 'community_1', :name => 'Community one')
  291 + person = create_user('test_user').person
  292 + assert_includes env.communities, com
  293 + assert_not_includes env.communities, person
  294 + end
  295 +
  296 + should 'not have person through enterprises' do
  297 + env = Environment.default
  298 + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
  299 + person = create_user('test_user').person
  300 + assert_includes env.enterprises, ent
  301 + assert_not_includes env.enterprises, person
  302 + end
  303 +
  304 + should 'not have enterprises through people' do
  305 + env = Environment.default
  306 + person = create_user('test_user').person
  307 + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
  308 + assert_includes env.people, person
  309 + assert_not_includes env.people, ent
  310 + end
  311 +
280 312 end
... ...