Commit a885775dc584a70845fc82d1a401122fc1e4ea36

Authored by MoisesMachado
1 parent 7311bf84

ActionItem510: fixed the query for product categories in enterprises


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2123 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/category_finder.rb
... ... @@ -17,12 +17,6 @@ class CategoryFinder
17 17 options.delete(:within)
18 18 end
19 19  
20   - # FIXME: can break if more things is added in the extra_data_for_index ferret field in enterprise
21   - # this searches for enterprise using its products categories criteria
22   - if options[:product_category] && asset.to_s == 'enterprises'
23   - query = query.blank? ? "extra_data_for_index:#{options[:product_category].name}" : query + " +extra_data_for_index:#{options[:product_category].name}"
24   - end
25   -
26 20 if query.blank?
27 21 asset_class(asset).find(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options)))
28 22 else
... ... @@ -76,7 +70,13 @@ class CategoryFinder
76 70 end
77 71 when 'Article', 'Event'
78 72 {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options)
79   - when 'Person', 'Community', 'Enterprise'
  73 + when 'Enterprise'
  74 + if prod_cat_ids
  75 + {: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)
  76 + else
  77 + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options)
  78 + end
  79 + when 'Person', 'Community'
80 80 {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options)
81 81 else
82 82 raise "unreconized class #{klass.name}"
... ...
app/models/environment_finder.rb
... ... @@ -19,7 +19,7 @@ class EnvironmentFinder
19 19 if product_category && asset == :products
20 20 @environment.send(asset).find(:all, options.merge({:order => 'created_at desc, id desc', :conditions => ['product_category_id in (?)', product_category_ids]}))
21 21 elsif product_category && asset == :enterprises
22   - @environment.send(asset).find_by_contents("extra_data_for_index:#{product_category.name}", {}, options.merge( {:order => 'created_at desc, id desc'} ) )
  22 + @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]))
23 23 else
24 24 @environment.send(asset).find( :all, options.merge( {:order => 'created_at desc, id desc'} ) )
25 25 end
... ...
test/functional/search_controller_test.rb
... ... @@ -851,28 +851,32 @@ class SearchControllerTest < Test::Unit::TestCase
851 851 assert_not_includes assigns(:categories_menu).map(&:first), cat2
852 852 end
853 853  
854   - should 'display enteprises only within a product category when specified' do
  854 + should 'display only enterprises in the product category when its specified' do
855 855 prod_cat = ProductCategory.create!(:name => 'prod cat test', :environment => Environment.default)
856   - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  856 + ent1 = Enterprise.create!(:name => 'test ent 1', :identifier => 'test_ent1')
  857 + p = prod_cat.products.create!(:name => 'prod test 1', :enterprise => ent1)
857 858  
858   - p = prod_cat.products.create!(:name => 'prod test 1', :enterprise => ent)
  859 + ent2 = Enterprise.create!(:name => 'test ent 2', :identifier => 'test_ent2')
859 860  
860 861 get :index, :find_in => 'enterprises', :product_category => prod_cat.id
861 862  
862   - assert_includes assigns(:results)[:enterprises], ent
  863 + assert_includes assigns(:results)[:enterprises], ent1
  864 + assert_not_includes assigns(:results)[:enterprises], ent2
863 865 end
864 866  
865 867 should 'display enterprises properly in conjuntion with a category' do
866 868 cat = Category.create(:name => 'cat', :environment => Environment.default)
867 869 prod_cat1 = ProductCategory.create!(:name => 'prod cat test 1', :environment => Environment.default)
868 870 prod_cat2 = ProductCategory.create!(:name => 'prod cat test 2', :environment => Environment.default, :parent => prod_cat1)
869   - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent', :category_ids => [cat.id])
  871 + ent1 = Enterprise.create!(:name => 'test ent 1', :identifier => 'test_ent1', :category_ids => [cat.id])
  872 + p = prod_cat2.products.create!(:name => 'prod test 1', :enterprise => ent1)
870 873  
871   - p = prod_cat2.products.create!(:name => 'prod test 1', :enterprise => ent)
  874 + ent2 = Enterprise.create!(:name => 'test ent 2', :identifier => 'test_ent2', :category_ids => [cat.id])
872 875  
873 876 get :index, :find_in => 'enterprises', :category_path => cat.path.split('/'), :product_category => prod_cat1.id
874 877  
875   - assert_includes assigns(:results)[:enterprises], ent
  878 + assert_includes assigns(:results)[:enterprises], ent1
  879 + assert_not_includes assigns(:results)[:enterprises], ent2
876 880 end
877 881  
878 882 should 'display only top level product categories that has enterprises when no product category filter is specified' do
... ...
test/unit/environment_finder_test.rb
... ... @@ -253,5 +253,38 @@ class EnvironmentFinderTest < ActiveSupport::TestCase
253 253 assert_includes ents, ent1
254 254 assert_not_includes ents, ent2
255 255 end
  256 +
  257 + should 'find enterprises by its products categories with query' do
  258 + finder = EnvironmentFinder.new(Environment.default)
  259 +
  260 + pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default)
  261 + pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default)
  262 +
  263 + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1')
  264 + ent1.products.create!(:name => 'test product 1', :product_category => pc1)
  265 + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2')
  266 + ent2.products.create!(:name => 'test product 2', :product_category => pc2)
  267 +
  268 + ents = finder.find(:enterprises, 'test', :product_category => pc1)
  269 +
  270 + assert_includes ents, ent1
  271 + assert_not_includes ents, ent2
  272 + end
  273 +
  274 + should 'find enterprises by a product category with name with spaces' do
  275 + finder = EnvironmentFinder.new(Environment.default)
  276 +
  277 + pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default)
  278 + pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default)
256 279  
  280 + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1')
  281 + ent1.products.create!(:name => 'test product 1', :product_category => pc1)
  282 + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2')
  283 + ent2.products.create!(:name => 'test product 2', :product_category => pc2)
  284 +
  285 + ents = finder.find(:enterprises, 'test', :product_category => pc1)
  286 +
  287 + assert_includes ents, ent1
  288 + assert_not_includes ents, ent2
  289 + end
257 290 end
... ...