Commit a762ff5861aab493b9577f647b47a64d0006f022

Authored by MoisesMachado
1 parent 8398fa80

ActionItem129: fixed the finder so it can properly find the enterprises by its products categories


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1932 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/environment_finder.rb
... ... @@ -18,6 +18,8 @@ class EnvironmentFinder
18 18 if query.blank?
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 + 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'} ) )
21 23 else
22 24 @environment.send(asset).find( :all, options.merge( {:order => 'created_at desc, id desc'} ) )
23 25 end
... ... @@ -25,6 +27,8 @@ class EnvironmentFinder
25 27 if product_category && asset == :products
26 28 # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source
27 29 @environment.send(asset).find_by_contents(query, {}, options.merge({:conditions => 'product_category_id in (%s)' % product_category_ids.join(',') }))
  30 + elsif product_category && asset == :enterprises
  31 + @environment.send(asset).find_by_contents(query + " +extra_data_for_index:#{product_category.name}", {}, options)
28 32 else
29 33 @environment.send(asset).find_by_contents(query, {}, options)
30 34 end
... ...
app/views/search/products.rhtml
... ... @@ -13,7 +13,7 @@
13 13 <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %>
14 14  
15 15 <% if @found_product_categories %>
16   - <div id="product-categories-menu">
  16 + <div id="product-categories-menu" class="product-search-filter">
17 17 <ul>
18 18 <% @found_product_categories.each do |cat, hits| %>
19 19 <li><%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %> </li>
... ...
public/stylesheets/search.css
... ... @@ -75,7 +75,9 @@
75 75 padding-top: 0px;
76 76 margin-bottom: -20px;
77 77 }
  78 +.product-search-filter {
78 79  
  80 +}
79 81 .map-or-list-search-results {
80 82 margin-left: 2em;
81 83 margin-right: 2em;
... ...
test/unit/environment_finder_test.rb
... ... @@ -191,4 +191,21 @@ class EnvironmentFinderTest &lt; ActiveSupport::TestCase
191 191 assert ents.index(ent2) < ents.index(ent1), "expected #{ents.index(ent2)} be smaller than #{ents.index(ent1)}"
192 192 end
193 193  
  194 + should 'find enterprises by its products categories' do
  195 + finder = EnvironmentFinder.new(Environment.default)
  196 +
  197 + pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default)
  198 + pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default)
  199 +
  200 + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1')
  201 + ent1.products.create!(:name => 'test product 1', :product_category => pc1)
  202 + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2')
  203 + ent2.products.create!(:name => 'test product 2', :product_category => pc2)
  204 +
  205 + ents = finder.find(:enterprises, nil, :product_category => pc1)
  206 +
  207 + assert_includes ents, ent1
  208 + assert_not_includes ents, ent2
  209 + end
  210 +
194 211 end
... ...