Commit a3bd56ab0500ad9da766bb8d63b3c5924b224fae

Authored by MoisesMachado
1 parent 94f57228

ActionItem510: putted the product category menu in the enteprise asset view


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2119 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/search_controller.rb
@@ -73,7 +73,7 @@ class SearchController < ApplicationController @@ -73,7 +73,7 @@ class SearchController < ApplicationController
73 #nothing, just to enable 73 #nothing, just to enable
74 end 74 end
75 def enterprises 75 def enterprises
76 - #nothing, just to enable 76 + load_product_categories_menu(:enterprises)
77 end 77 end
78 def communities 78 def communities
79 #nothing, just to enable 79 #nothing, just to enable
@@ -82,6 +82,17 @@ class SearchController < ApplicationController @@ -82,6 +82,17 @@ class SearchController < ApplicationController
82 #nothins, just to enable 82 #nothins, just to enable
83 end 83 end
84 84
  85 + def products
  86 + load_product_categories_menu(:products)
  87 + end
  88 +
  89 + def load_product_categories_menu(asset)
  90 + @results[asset].uniq!
  91 + @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat|
  92 + [cat, @finder.count(:products, @filtered_query, calculate_find_options(asset, nil, cat, @region, params[:radius]))]
  93 + end.select{|cat, hits| hits > 0 }
  94 + end
  95 +
85 def calculate_find_options(asset, limit, product_category, region, radius) 96 def calculate_find_options(asset, limit, product_category, region, radius)
86 97
87 result = { :limit => limit, :product_category => product_category} 98 result = { :limit => limit, :product_category => product_category}
@@ -128,7 +139,7 @@ class SearchController < ApplicationController @@ -128,7 +139,7 @@ class SearchController < ApplicationController
128 number_of_result_assets = @searching.values.select{|v| v}.size 139 number_of_result_assets = @searching.values.select{|v| v}.size
129 140
130 # apply limit when searching for only one type of asset 141 # apply limit when searching for only one type of asset
131 - limit = (number_of_result_assets == 1) ? LIST_LIMIT : nil 142 + limit = (number_of_result_assets == 1) ? nil: LIST_LIMIT
132 # apply limit to all searches 143 # apply limit to all searches
133 # limit = nil 144 # limit = nil
134 145
@@ -153,13 +164,6 @@ class SearchController < ApplicationController @@ -153,13 +164,6 @@ class SearchController < ApplicationController
153 render :action => 'index' 164 render :action => 'index'
154 end 165 end
155 166
156 - def products  
157 - @results[:products].uniq!  
158 - @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat|  
159 - [cat, @finder.count(:products, @filtered_query, calculate_find_options(:products, nil, cat, @region, params[:radius]))]  
160 - end.select{|cat, hits| hits > 0 }  
161 - end  
162 -  
163 alias :assets :index 167 alias :assets :index
164 168
165 ####################################################### 169 #######################################################
app/models/category_finder.rb
@@ -9,7 +9,7 @@ class CategoryFinder @@ -9,7 +9,7 @@ class CategoryFinder
9 9
10 10
11 11
12 - def find(asset, query = nil, options={}) 12 + def find(asset, query='', options={})
13 @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) 13 @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
14 if @region && options[:within] 14 if @region && options[:within]
15 options[:origin] = [@region.lat, @region.lng] 15 options[:origin] = [@region.lat, @region.lng]
@@ -17,9 +17,15 @@ class CategoryFinder @@ -17,9 +17,15 @@ class CategoryFinder
17 options.delete(:within) 17 options.delete(:within)
18 end 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 +
20 if query.blank? 26 if query.blank?
21 asset_class(asset).find(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) 27 asset_class(asset).find(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options)))
22 - else 28 + else
23 asset_class(asset).find_by_contents(query, {}, options_for_find(asset_class(asset), options)).uniq 29 asset_class(asset).find_by_contents(query, {}, options_for_find(asset_class(asset), options)).uniq
24 end 30 end
25 end 31 end
app/views/search/enterprises.rhtml
@@ -1 +0,0 @@ @@ -1 +0,0 @@
1 -people.rhtml  
2 \ No newline at end of file 0 \ No newline at end of file
app/views/search/enterprises.rhtml 0 → 100644
@@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
  1 +<h1>
  2 + <% if !@query.blank? %>
  3 + <%=h @category ? (_('Enterprise results for "%{query}" in "%{category}"') % { :query => @query, :category => @category.name}) : (_('Enterprise results for "%s"') % @query) %>
  4 + <% else %>
  5 + <%=h @category ? (_('Enterprises in "%s"') % @category.name) : _('Enterprise') %>
  6 + <% end %>
  7 +</h1>
  8 +
  9 +<% if @radius && @region %>
  10 + <h2><%=h (_('Within %s km from %s') % [@radius, @region.name]) %><h2>
  11 +<% end %>
  12 +
  13 +<%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %>
  14 +
  15 +<%= render :partial => 'product_categories_menu', :object => @categories_menu %>
  16 +
  17 +<%= display_results %>
  18 +
  19 +<br style="clear:both" />
app/views/search/products.rhtml
@@ -12,15 +12,7 @@ @@ -12,15 +12,7 @@
12 12
13 <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> 13 <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %>
14 14
15 -<% if @categories_menu %>  
16 - <div id="product-categories-menu" class="product-search-filter">  
17 - <ul>  
18 - <% @categories_menu.each do |cat, hits| %>  
19 - <li><%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %> </li>  
20 - <% end %>  
21 - </ul>  
22 - </div>  
23 -<% end %> 15 +<%= render :partial => 'product_categories_menu', :object => @categories_menu %>
24 16
25 <div id="search-results" class="only-one-result-box"> 17 <div id="search-results" class="only-one-result-box">
26 <div class="search-results-products search-results-box"> 18 <div class="search-results-products search-results-box">
test/functional/search_controller_test.rb
@@ -838,6 +838,68 @@ class SearchControllerTest &lt; Test::Unit::TestCase @@ -838,6 +838,68 @@ class SearchControllerTest &lt; Test::Unit::TestCase
838 assert_not_includes assigns(:categories_menu).map(&:first), cat12 838 assert_not_includes assigns(:categories_menu).map(&:first), cat12
839 end 839 end
840 840
  841 + should 'list only product categories with enterprises' do
  842 + cat1 = ProductCategory.create!(:name => 'pc test 1', :environment => Environment.default)
  843 + cat2 = ProductCategory.create!(:name => 'pc test 2', :environment => Environment.default)
  844 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  845 +
  846 + cat1.products.create!(:name => 'prod test 1', :enterprise => ent)
  847 +
  848 + get :index, :find_in => 'enterprises', :query => 'test'
  849 +
  850 + assert_includes assigns(:categories_menu).map(&:first), cat1
  851 + assert_not_includes assigns(:categories_menu).map(&:first), cat2
  852 + end
  853 +
  854 + should 'display enteprises only within a product category when specified' do
  855 + prod_cat = ProductCategory.create!(:name => 'prod cat test', :environment => Environment.default)
  856 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  857 +
  858 + p = prod_cat.products.create!(:name => 'prod test 1', :enterprise => ent)
  859 +
  860 + get :index, :find_in => 'enterprises', :product_category => prod_cat.id
  861 +
  862 + assert_includes assigns(:results)[:enterprises], ent
  863 + end
  864 +
  865 + should 'display enterprises properly in conjuntion with a category' do
  866 + cat = Category.create(:name => 'cat', :environment => Environment.default)
  867 + prod_cat1 = ProductCategory.create!(:name => 'prod cat test 1', :environment => Environment.default)
  868 + 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])
  870 +
  871 + p = prod_cat2.products.create!(:name => 'prod test 1', :enterprise => ent)
  872 +
  873 + get :index, :find_in => 'enterprises', :category_path => cat.path.split('/'), :product_category => prod_cat1.id
  874 +
  875 + assert_includes assigns(:results)[:enterprises], ent
  876 + end
  877 +
  878 + should 'display only top level product categories that has enterprises when no product category filter is specified' do
  879 + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default)
  880 + cat2 = ProductCategory.create(:name => 'prod cat 2', :environment => Environment.default)
  881 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  882 + p = cat1.products.create!(:name => 'prod test 1', :enterprise => ent)
  883 +
  884 + get :index, :find_in => 'enterprises'
  885 +
  886 + assert_includes assigns(:categories_menu).map(&:first), cat1
  887 + assert_not_includes assigns(:categories_menu).map(&:first), cat2
  888 + end
  889 +
  890 + should 'display children categories that has enterprises when product category filter is selected' do
  891 + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default)
  892 + cat11 = ProductCategory.create(:name => 'prod cat 11', :environment => Environment.default, :parent => cat1)
  893 + cat12 = ProductCategory.create(:name => 'prod cat 12', :environment => Environment.default, :parent => cat1)
  894 + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
  895 + p = cat11.products.create!(:name => 'prod test 1', :enterprise => ent)
  896 +
  897 + get :index, :find_in => 'enterprises', :product_category => cat1.id
  898 +
  899 + assert_includes assigns(:categories_menu).map(&:first), cat11
  900 + assert_not_includes assigns(:categories_menu).map(&:first), cat12
  901 + end
  902 +
841 should 'provide calendar for events' do 903 should 'provide calendar for events' do
842 get :index, :find_in => [ 'events' ] 904 get :index, :find_in => [ 'events' ]
843 assert_equal 0, assigns(:calendar).size % 7 905 assert_equal 0, assigns(:calendar).size % 7
test/unit/category_finder_test.rb
@@ -381,4 +381,34 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase @@ -381,4 +381,34 @@ class CategoryFinderTest &lt; ActiveSupport::TestCase
381 assert_not_includes prods, prod2 381 assert_not_includes prods, prod2
382 end 382 end
383 383
  384 + should 'find enterprises by its products categories without query' do
  385 + pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default)
  386 + pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default)
  387 +
  388 + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id])
  389 + ent1.products.create!(:name => 'test product 1', :product_category => pc1)
  390 + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2', :category_ids => [@category.id])
  391 + ent2.products.create!(:name => 'test product 2', :product_category => pc2)
  392 +
  393 + ents = @finder.find(:enterprises, nil, :product_category => pc1)
  394 +
  395 + assert_includes ents, ent1
  396 + assert_not_includes ents, ent2
  397 + end
  398 +
  399 + should 'find enterprises by its products categories with query' do
  400 + pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default)
  401 + pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default)
  402 +
  403 + ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id])
  404 + ent1.products.create!(:name => 'test product 1', :product_category => pc1)
  405 + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2', :category_ids => [@category.id])
  406 + ent2.products.create!(:name => 'test product 2', :product_category => pc2)
  407 +
  408 + ents = @finder.find(:enterprises, 'test', :product_category => pc1)
  409 +
  410 + assert_includes ents, ent1
  411 + assert_not_includes ents, ent2
  412 + end
  413 +
384 end 414 end