From c4b2472c37dd80340dfa1e5d3ab480594e0e7c5f Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Wed, 26 Mar 2008 16:42:50 +0000 Subject: [PATCH] ActionItem243: implemented finding communities, products, the :find_in and fixed a bug where searching for people returned enterprises --- app/controllers/public/search_controller.rb | 12 +++++------- app/models/category.rb | 7 +++++-- app/models/environment.rb | 1 + test/functional/search_controller_test.rb | 47 ++++++++++++++++++++++++++++++++++++++--------- test/unit/category_test.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/environment_test.rb | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 18 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 766a7a3..2eb8810 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -21,13 +21,11 @@ class SearchController < ApplicationController @filtered_query = remove_stop_words(@query) @finder ||= @environment - - @results = { - :articles => search(@finder.articles, @filtered_query), - :comments => search(@finder.comments, @filtered_query), - :enterprises => search(@finder.enterprises, @filtered_query), - :people => search(@finder.people, @filtered_query), - } + + @results = {} + [:articles, :comments, :enterprises, :people, :communities, :products].each do |key| + @results[key] = search(@finder.send(key), @filtered_query) if params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s) + end end before_filter :load_category, :only => :filter diff --git a/app/models/category.rb b/app/models/category.rb index 87ee9ac..f8413f0 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -26,8 +26,11 @@ class Category < ActiveRecord::Base has_many :comments, :through => :articles has_many :profile_categorizations - has_many :enterprises, :through => :profile_categorizations, :source => :profile - has_many :people, :through => :profile_categorizations, :source => :profile + has_many :enterprises, :through => :profile_categorizations, :source => :profile, :class_name => 'Enterprise' + has_many :people, :through => :profile_categorizations, :source => :profile, :class_name => 'Person' + has_many :communities, :through => :profile_categorizations, :source => :profile, :class_name => 'Community' + + has_many :products, :through => :enterprises def recent_articles(limit = 10) self.articles.recent(limit) diff --git a/app/models/environment.rb b/app/models/environment.rb index 79e8f82..3459575 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -56,6 +56,7 @@ class Environment < ActiveRecord::Base has_many :organizations has_many :enterprises + has_many :products, :through => :enterprises has_many :people has_many :communities diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index fcf04a4..0c2b181 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -126,16 +126,16 @@ class SearchControllerTest < Test::Unit::TestCase should 'list enterprises in a specified category' should 'find people' do - p1 = create_user('people_1').person; p1.name = 'a beatiful person'; p1.save! - get :index, :query => 'beatiful', :find_in => [ 'people' ] + p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.save! + get :index, :query => 'beautiful', :find_in => [ 'people' ] assert_includes assigns(:results)[:people], p1 end should 'find people in a specific category' do c = Category.create!(:name => 'my category', :environment => Environment.default) - p1 = create_user('people_1').person; p1.name = 'a beatiful person'; p1.categories << c; p1.save! - p2 = create_user('people_2').person; p2.name = 'another beatiful person'; p2.save! - get :filter, :category_path => [ 'my-category' ], :query => 'beatiful', :find_in => [ 'people' ] + p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.categories << c; p1.save! + p2 = create_user('people_2').person; p2.name = 'another beautiful person'; p2.save! + get :filter, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'people' ] assert_includes assigns(:results)[:people], p1 assert_not_includes assigns(:results)[:people], p2 end @@ -143,13 +143,42 @@ class SearchControllerTest < Test::Unit::TestCase # 'assets' menu should 'list people in a specified category' - should 'find communities' - should 'find communities in a specified category' + should 'find communities' do + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) + get :index, :query => 'beautiful', :find_in => [ 'communities' ] + assert_includes assigns(:results)[:communities], c1 + end + + should 'find communities in a specified category' do + c = Category.create!(:name => 'my category', :environment => Environment.default) + c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) + c2 = Community.create!(:name => 'another beautiful community', :identifier => 'an_bea_comm', :environment => Environment.default) + c1.categories << c; c1.save! + get :filter, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'communities' ] + assert_includes assigns(:results)[:communities], c1 + assert_not_includes assigns(:results)[:communities], c2 + end # 'assets' menu should 'list communities in a specified category' - should 'find products' - should 'find products in a specific category' + should 'find products' do + ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') + prod = ent.products.create!(:name => 'a beautiful product') + get 'index', :query => 'beautiful', :find_in => ['products'] + assert_includes assigns(:results)[:products], prod + end + + should 'find products in a specific category' do + c = Category.create!(:name => 'my category', :environment => Environment.default) + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1'); ent1.categories << c + ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') + prod1 = ent1.products.create!(:name => 'a beautiful product') + prod2 = ent2.products.create!(:name => 'another beautiful product') + get 'filter', :category_path => ['my-category'], :query => 'beautiful', :find_in => ['products'] + assert_includes assigns(:results)[:products], prod1 + assert_not_includes assigns(:results)[:products], prod2 + end + # 'assets' menu should 'list products in a specific category' diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 408c1b5..f33db6c 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -295,4 +295,54 @@ class CategoryTest < Test::Unit::TestCase assert_equal [p1, p2], c.people end + should 'have communities' do + c = @env.categories.build(:name => 'my category'); c.save! + c1 = Environment.default.communities.create!(:name => 'testcommunity_1') + c1.categories << c + c2 = Environment.default.communities.create!(:name => 'testcommunity_2') + c2.categories << c + assert_equal [c1, c2], c.communities + end + + should 'have products through enteprises' do + c = @env.categories.build(:name => 'my category'); c.save! + ent1 = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') + ent1.categories << c + ent2 = Enterprise.create!(:identifier => 'enterprise_2', :name => 'Enterprise one') + ent2.categories << c + prod1 = ent1.products.create!(:name => 'test_prod1') + prod2 = ent2.products.create!(:name => 'test_prod2') + assert_includes c.products, prod1 + assert_includes c.products, prod2 + end + + should 'not have person through communities' do + c = @env.categories.build(:name => 'my category'); c.save! + com = Community.create!(:identifier => 'community_1', :name => 'Community one') + com.categories << c + person = create_user('test_user').person + person.categories << c + assert_includes c.communities, com + assert_not_includes c.communities, person + end + + should 'not have person through enterprises' do + c = @env.categories.build(:name => 'my category'); c.save! + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') + ent.categories << c + person = create_user('test_user').person + person.categories << c + assert_includes c.enterprises, ent + assert_not_includes c.enterprises, person + end + + should 'not have enterprises through people' do + c = @env.categories.build(:name => 'my category'); c.save! + person = create_user('test_user').person + person.categories << c + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') + ent.categories << c + assert_includes c.people, person + assert_not_includes c.people, ent + end end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index f6c980e..56e0329 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -277,4 +277,36 @@ class EnvironmentTest < Test::Unit::TestCase assert_equivalent [c1,c2], environment.comments end + should 'have products through enterprises' do + env = Environment.default + e1 = Enterprise.create!(:name => 'test_ent1', :identifier => 'test_ent1') + p1 = e1.products.create!(:name => 'test_prod1') + + assert_includes env.products, p1 + end + + should 'not have person through communities' do + env = Environment.default + com = Community.create!(:identifier => 'community_1', :name => 'Community one') + person = create_user('test_user').person + assert_includes env.communities, com + assert_not_includes env.communities, person + end + + should 'not have person through enterprises' do + env = Environment.default + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') + person = create_user('test_user').person + assert_includes env.enterprises, ent + assert_not_includes env.enterprises, person + end + + should 'not have enterprises through people' do + env = Environment.default + person = create_user('test_user').person + ent = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') + assert_includes env.people, person + assert_not_includes env.people, ent + end + end -- libgit2 0.21.2