diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 1aa0f98..766a7a3 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -16,29 +16,18 @@ class SearchController < ApplicationController ###################################################### - class Finder - attr_reader :environment - def initialize(env) - @environment = env - end - - def articles - environment.articles - end - - def comments - environment.comments - end - end - def index @query = params[:query] || '' @filtered_query = remove_stop_words(@query) - @finder ||= SearchController::Finder.new(@environment) + @finder ||= @environment - @results = { :articles => search(@finder.articles, @query), - :comments => search(@finder.comments, @query) } + @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), + } end before_filter :load_category, :only => :filter diff --git a/app/models/category.rb b/app/models/category.rb index eb1cae0..87ee9ac 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -26,7 +26,8 @@ class Category < ActiveRecord::Base has_many :comments, :through => :articles has_many :profile_categorizations - has_many :profiles, :through => :profile_categorizations + has_many :enterprises, :through => :profile_categorizations, :source => :profile + has_many :people, :through => :profile_categorizations, :source => :profile def recent_articles(limit = 10) self.articles.recent(limit) diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 8ffc3e7..fcf04a4 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -19,6 +19,13 @@ class SearchControllerTest < Test::Unit::TestCase assert_equal 'carne vaca', assigns('filtered_query') end + should 'search with filtered query' do + @controller.expects(:locale).returns('pt_BR').at_least_once + @controller.expects(:search).with(anything, 'carne vaca').at_least_once + @controller.expects(:search).with(anything, 'a carne da vaca').never + get 'index', :query => 'a carne da vaca' + end + should 'search only in specified types of content' do get :index, :query => 'something not important', :find_in => [ 'articles' ] assert_equal [:articles], assigns(:results).keys @@ -94,24 +101,9 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes assigns(:results)[:comments], comment2 end - - should 'find in environment' do - env = mock - finder = SearchController::Finder.new(env) - assert_same env, finder.environment - end - - should 'delegate to environment in default finder' do - env = mock - articles = mock - finder = SearchController::Finder.new(env) - env.expects(:articles).returns(articles) - assert_same articles, finder.articles - end - should 'find enterprises' do ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') - get 'index', :query => 'teste' + get 'index', :query => 'teste', :find_in => [ 'enterprises' ] assert_includes assigns(:results)[:enterprises], ent end @@ -119,22 +111,34 @@ class SearchControllerTest < Test::Unit::TestCase category = Category.create!(:name => 'my category', :environment => Environment.default) # in category - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test1', :categories => [category]) + ent1 = Enterprise.create!(:name => 'testing enterprise 1', :identifier => 'test1', :categories => [category]) # not in category - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test1') + ent2 = Enterprise.create!(:name => 'testing enterprise 2', :identifier => 'test2') - get :filter, :category_path => [ 'my-category' ], :query => 'test', :find_in => [ 'enterprises' ] + get :filter, :category_path => [ 'my-category' ], :query => 'testing', :find_in => [ 'enterprises' ] - assert_includes ent1, assigns(:results)[:enterprises] - assert_not_includes ent2, assigns(:results)[:enterprises] + assert_includes assigns(:results)[:enterprises], ent1 + assert_not_includes assigns(:results)[:enterprises], ent2 end # 'assets' menu should 'list enterprises in a specified category' - should 'find people' - should 'find people in a specific 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' ] + 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' ] + assert_includes assigns(:results)[:people], p1 + assert_not_includes assigns(:results)[:people], p2 + end # 'assets' menu should 'list people in a specified category' diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 967b372..408c1b5 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -276,14 +276,23 @@ class CategoryTest < Test::Unit::TestCase assert_equivalent [c1, c2, c3], c.comments end - should 'have profiles' do + should 'have enterprises' do c = @env.categories.build(:name => 'my category'); c.save! - person1 = create_user('testuser_one').person - person1.categories << c - person2 = create_user('testuser_two').person - person2.categories << c - assert_includes c.profiles, person1 - assert_includes c.profiles, person2 + ent1 = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one') + ent1.categories << c + ent2 = Enterprise.create!(:identifier => 'enterprise_2', :name => 'Enterprise one') + ent2.categories << c + assert_includes c.enterprises, ent1 + assert_includes c.enterprises, ent2 + end + + should 'have people' do + c = @env.categories.build(:name => 'my category'); c.save! + p1 = create_user('testuser_1').person + p1.categories << c + p2 = create_user('testuser_2').person + p2.categories << c + assert_equal [p1, p2], c.people end end -- libgit2 0.21.2