diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 0ca264e..fcb3abc 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -17,12 +17,6 @@ class CategoryFinder options.delete(:within) end - # FIXME: can break if more things is added in the extra_data_for_index ferret field in enterprise - # this searches for enterprise using its products categories criteria - if options[:product_category] && asset.to_s == 'enterprises' - query = query.blank? ? "extra_data_for_index:#{options[:product_category].name}" : query + " +extra_data_for_index:#{options[:product_category].name}" - end - if query.blank? asset_class(asset).find(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) else @@ -76,7 +70,13 @@ class CategoryFinder end when 'Article', 'Event' {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options) - when 'Person', 'Community', 'Enterprise' + when 'Enterprise' + if prod_cat_ids + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id) inner join products on (products.enterprise_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?) and products.product_category_id in (?)', category_id, prod_cat_ids]}.merge!(options) + else + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) + end + when 'Person', 'Community' {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) else raise "unreconized class #{klass.name}" diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index ae7a421..cab280f 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -19,7 +19,7 @@ class EnvironmentFinder if product_category && asset == :products @environment.send(asset).find(:all, options.merge({:order => 'created_at desc, id desc', :conditions => ['product_category_id in (?)', product_category_ids]})) elsif product_category && asset == :enterprises - @environment.send(asset).find_by_contents("extra_data_for_index:#{product_category.name}", {}, options.merge( {:order => 'created_at desc, id desc'} ) ) + @environment.send(asset).find(:all, options.merge(:order => 'profiles.created_at desc, profiles.id desc', :include => 'products', :conditions => ['products.product_category_id in (?)', product_category_ids])) else @environment.send(asset).find( :all, options.merge( {:order => 'created_at desc, id desc'} ) ) end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index b962bbc..d4e48b8 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -851,28 +851,32 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes assigns(:categories_menu).map(&:first), cat2 end - should 'display enteprises only within a product category when specified' do + should 'display only enterprises in the product category when its specified' do prod_cat = ProductCategory.create!(:name => 'prod cat test', :environment => Environment.default) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + ent1 = Enterprise.create!(:name => 'test ent 1', :identifier => 'test_ent1') + p = prod_cat.products.create!(:name => 'prod test 1', :enterprise => ent1) - p = prod_cat.products.create!(:name => 'prod test 1', :enterprise => ent) + ent2 = Enterprise.create!(:name => 'test ent 2', :identifier => 'test_ent2') get :index, :find_in => 'enterprises', :product_category => prod_cat.id - assert_includes assigns(:results)[:enterprises], ent + assert_includes assigns(:results)[:enterprises], ent1 + assert_not_includes assigns(:results)[:enterprises], ent2 end should 'display enterprises properly in conjuntion with a category' do cat = Category.create(:name => 'cat', :environment => Environment.default) prod_cat1 = ProductCategory.create!(:name => 'prod cat test 1', :environment => Environment.default) prod_cat2 = ProductCategory.create!(:name => 'prod cat test 2', :environment => Environment.default, :parent => prod_cat1) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent', :category_ids => [cat.id]) + ent1 = Enterprise.create!(:name => 'test ent 1', :identifier => 'test_ent1', :category_ids => [cat.id]) + p = prod_cat2.products.create!(:name => 'prod test 1', :enterprise => ent1) - p = prod_cat2.products.create!(:name => 'prod test 1', :enterprise => ent) + ent2 = Enterprise.create!(:name => 'test ent 2', :identifier => 'test_ent2', :category_ids => [cat.id]) get :index, :find_in => 'enterprises', :category_path => cat.path.split('/'), :product_category => prod_cat1.id - assert_includes assigns(:results)[:enterprises], ent + assert_includes assigns(:results)[:enterprises], ent1 + assert_not_includes assigns(:results)[:enterprises], ent2 end should 'display only top level product categories that has enterprises when no product category filter is specified' do diff --git a/test/unit/environment_finder_test.rb b/test/unit/environment_finder_test.rb index fe72d46..642ae4a 100644 --- a/test/unit/environment_finder_test.rb +++ b/test/unit/environment_finder_test.rb @@ -253,5 +253,38 @@ class EnvironmentFinderTest < ActiveSupport::TestCase assert_includes ents, ent1 assert_not_includes ents, ent2 end + + should 'find enterprises by its products categories with query' do + finder = EnvironmentFinder.new(Environment.default) + + pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default) + pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default) + + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent1.products.create!(:name => 'test product 1', :product_category => pc1) + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2.products.create!(:name => 'test product 2', :product_category => pc2) + + ents = finder.find(:enterprises, 'test', :product_category => pc1) + + assert_includes ents, ent1 + assert_not_includes ents, ent2 + end + + should 'find enterprises by a product category with name with spaces' do + finder = EnvironmentFinder.new(Environment.default) + + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent1.products.create!(:name => 'test product 1', :product_category => pc1) + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2.products.create!(:name => 'test product 2', :product_category => pc2) + + ents = finder.find(:enterprises, 'test', :product_category => pc1) + + assert_includes ents, ent1 + assert_not_includes ents, ent2 + end end -- libgit2 0.21.2