From a3bd56ab0500ad9da766bb8d63b3c5924b224fae Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Wed, 2 Jul 2008 19:44:59 +0000 Subject: [PATCH] ActionItem510: putted the product category menu in the enteprise asset view --- app/controllers/public/search_controller.rb | 22 +++++++++++++--------- app/models/category_finder.rb | 10 ++++++++-- app/views/search/enterprises.rhtml | 1 - app/views/search/enterprises.rhtml | 19 +++++++++++++++++++ app/views/search/products.rhtml | 10 +--------- test/functional/search_controller_test.rb | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test/unit/category_finder_test.rb | 30 ++++++++++++++++++++++++++++++ 7 files changed, 133 insertions(+), 21 deletions(-) delete mode 120000 app/views/search/enterprises.rhtml create mode 100644 app/views/search/enterprises.rhtml diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index f797c08..068a3cc 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -73,7 +73,7 @@ class SearchController < ApplicationController #nothing, just to enable end def enterprises - #nothing, just to enable + load_product_categories_menu(:enterprises) end def communities #nothing, just to enable @@ -82,6 +82,17 @@ class SearchController < ApplicationController #nothins, just to enable end + def products + load_product_categories_menu(:products) + end + + def load_product_categories_menu(asset) + @results[asset].uniq! + @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| + [cat, @finder.count(:products, @filtered_query, calculate_find_options(asset, nil, cat, @region, params[:radius]))] + end.select{|cat, hits| hits > 0 } + end + def calculate_find_options(asset, limit, product_category, region, radius) result = { :limit => limit, :product_category => product_category} @@ -128,7 +139,7 @@ class SearchController < ApplicationController number_of_result_assets = @searching.values.select{|v| v}.size # apply limit when searching for only one type of asset - limit = (number_of_result_assets == 1) ? LIST_LIMIT : nil + limit = (number_of_result_assets == 1) ? nil: LIST_LIMIT # apply limit to all searches # limit = nil @@ -153,13 +164,6 @@ class SearchController < ApplicationController render :action => 'index' end - def products - @results[:products].uniq! - @categories_menu = ProductCategory.menu_categories(@product_category, environment).map do |cat| - [cat, @finder.count(:products, @filtered_query, calculate_find_options(:products, nil, cat, @region, params[:radius]))] - end.select{|cat, hits| hits > 0 } - end - alias :assets :index ####################################################### diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 3e7e6b6..0ca264e 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -9,7 +9,7 @@ class CategoryFinder - def find(asset, query = nil, options={}) + def find(asset, query='', options={}) @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) if @region && options[:within] options[:origin] = [@region.lat, @region.lng] @@ -17,9 +17,15 @@ 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 + else asset_class(asset).find_by_contents(query, {}, options_for_find(asset_class(asset), options)).uniq end end diff --git a/app/views/search/enterprises.rhtml b/app/views/search/enterprises.rhtml deleted file mode 120000 index aba8524..0000000 --- a/app/views/search/enterprises.rhtml +++ /dev/null @@ -1 +0,0 @@ -people.rhtml \ No newline at end of file diff --git a/app/views/search/enterprises.rhtml b/app/views/search/enterprises.rhtml new file mode 100644 index 0000000..24713b3 --- /dev/null +++ b/app/views/search/enterprises.rhtml @@ -0,0 +1,19 @@ +

+ <% if !@query.blank? %> + <%=h @category ? (_('Enterprise results for "%{query}" in "%{category}"') % { :query => @query, :category => @category.name}) : (_('Enterprise results for "%s"') % @query) %> + <% else %> + <%=h @category ? (_('Enterprises in "%s"') % @category.name) : _('Enterprise') %> + <% end %> +

+ +<% if @radius && @region %> +

<%=h (_('Within %s km from %s') % [@radius, @region.name]) %>

+<% end %> + +<%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> + +<%= render :partial => 'product_categories_menu', :object => @categories_menu %> + +<%= display_results %> + +
diff --git a/app/views/search/products.rhtml b/app/views/search/products.rhtml index 966f240..938fded 100644 --- a/app/views/search/products.rhtml +++ b/app/views/search/products.rhtml @@ -12,15 +12,7 @@ <%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> -<% if @categories_menu %> -
-
    - <% @categories_menu.each do |cat, hits| %> -
  • <%= link_to (cat.name + "(#{hits})"), params.merge({:product_category => cat.id}) %>
  • - <% end %> -
-
-<% end %> +<%= render :partial => 'product_categories_menu', :object => @categories_menu %>
diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index fff380f..b962bbc 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -838,6 +838,68 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes assigns(:categories_menu).map(&:first), cat12 end + should 'list only product categories with enterprises' do + cat1 = ProductCategory.create!(:name => 'pc test 1', :environment => Environment.default) + cat2 = ProductCategory.create!(:name => 'pc test 2', :environment => Environment.default) + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + + cat1.products.create!(:name => 'prod test 1', :enterprise => ent) + + get :index, :find_in => 'enterprises', :query => 'test' + + assert_includes assigns(:categories_menu).map(&:first), cat1 + assert_not_includes assigns(:categories_menu).map(&:first), cat2 + end + + should 'display enteprises only within a product category when specified' do + prod_cat = ProductCategory.create!(:name => 'prod cat test', :environment => Environment.default) + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + + p = prod_cat.products.create!(:name => 'prod test 1', :enterprise => ent) + + get :index, :find_in => 'enterprises', :product_category => prod_cat.id + + assert_includes assigns(:results)[:enterprises], ent + 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]) + + p = prod_cat2.products.create!(:name => 'prod test 1', :enterprise => ent) + + get :index, :find_in => 'enterprises', :category_path => cat.path.split('/'), :product_category => prod_cat1.id + + assert_includes assigns(:results)[:enterprises], ent + end + + should 'display only top level product categories that has enterprises when no product category filter is specified' do + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default) + cat2 = ProductCategory.create(:name => 'prod cat 2', :environment => Environment.default) + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + p = cat1.products.create!(:name => 'prod test 1', :enterprise => ent) + + get :index, :find_in => 'enterprises' + + assert_includes assigns(:categories_menu).map(&:first), cat1 + assert_not_includes assigns(:categories_menu).map(&:first), cat2 + end + + should 'display children categories that has enterprises when product category filter is selected' do + cat1 = ProductCategory.create(:name => 'prod cat 1', :environment => Environment.default) + cat11 = ProductCategory.create(:name => 'prod cat 11', :environment => Environment.default, :parent => cat1) + cat12 = ProductCategory.create(:name => 'prod cat 12', :environment => Environment.default, :parent => cat1) + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + p = cat11.products.create!(:name => 'prod test 1', :enterprise => ent) + + get :index, :find_in => 'enterprises', :product_category => cat1.id + + assert_includes assigns(:categories_menu).map(&:first), cat11 + assert_not_includes assigns(:categories_menu).map(&:first), cat12 + end + should 'provide calendar for events' do get :index, :find_in => [ 'events' ] assert_equal 0, assigns(:calendar).size % 7 diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index a4cef2b..f8f80fc 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -381,4 +381,34 @@ class CategoryFinderTest < ActiveSupport::TestCase assert_not_includes prods, prod2 end + should 'find enterprises by its products categories without query' do + 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', :category_ids => [@category.id]) + ent1.products.create!(:name => 'test product 1', :product_category => pc1) + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2', :category_ids => [@category.id]) + 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 + + should 'find enterprises by its products categories with query' do + 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', :category_ids => [@category.id]) + ent1.products.create!(:name => 'test product 1', :product_category => pc1) + ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2', :category_ids => [@category.id]) + 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