From a762ff5861aab493b9577f647b47a64d0006f022 Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Sat, 7 Jun 2008 22:26:07 +0000 Subject: [PATCH] ActionItem129: fixed the finder so it can properly find the enterprises by its products categories --- app/models/environment_finder.rb | 4 ++++ app/views/search/products.rhtml | 2 +- public/stylesheets/search.css | 2 ++ test/unit/environment_finder_test.rb | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index 9678789..f31a638 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -18,6 +18,8 @@ class EnvironmentFinder if query.blank? 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'} ) ) else @environment.send(asset).find( :all, options.merge( {:order => 'created_at desc, id desc'} ) ) end @@ -25,6 +27,8 @@ class EnvironmentFinder if product_category && asset == :products # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source @environment.send(asset).find_by_contents(query, {}, options.merge({:conditions => 'product_category_id in (%s)' % product_category_ids.join(',') })) + elsif product_category && asset == :enterprises + @environment.send(asset).find_by_contents(query + " +extra_data_for_index:#{product_category.name}", {}, options) else @environment.send(asset).find_by_contents(query, {}, options) end diff --git a/app/views/search/products.rhtml b/app/views/search/products.rhtml index 3c2949b..3b913e6 100644 --- a/app/views/search/products.rhtml +++ b/app/views/search/products.rhtml @@ -13,7 +13,7 @@ <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> <% if @found_product_categories %> -
+
    <% @found_product_categories.each do |cat, hits| %>
  • <%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %>
  • diff --git a/public/stylesheets/search.css b/public/stylesheets/search.css index ed6f190..b9edbbd 100644 --- a/public/stylesheets/search.css +++ b/public/stylesheets/search.css @@ -75,7 +75,9 @@ padding-top: 0px; margin-bottom: -20px; } +.product-search-filter { +} .map-or-list-search-results { margin-left: 2em; margin-right: 2em; diff --git a/test/unit/environment_finder_test.rb b/test/unit/environment_finder_test.rb index 3cc37d4..c656d68 100644 --- a/test/unit/environment_finder_test.rb +++ b/test/unit/environment_finder_test.rb @@ -191,4 +191,21 @@ class EnvironmentFinderTest < ActiveSupport::TestCase assert ents.index(ent2) < ents.index(ent1), "expected #{ents.index(ent2)} be smaller than #{ents.index(ent1)}" end + should 'find enterprises by its products categories' 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, nil, :product_category => pc1) + + assert_includes ents, ent1 + assert_not_includes ents, ent2 + end + end -- libgit2 0.21.2