diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 7107262..69868ca 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -82,6 +82,16 @@ class SearchController < ApplicationController #nothins, just to enable end + def calculate_find_options(asset, limit, product_category, region, radius) + + result = { :limit => limit, :product_category => product_category} + if [:enterprises, :people].include?(asset) && region + result.merge!(:within => radius, :region => region.id) + end + + result + end + public include SearchHelper @@ -118,20 +128,15 @@ 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) ? LIST_LIMIT : nil # apply limit to all searches - limit = nil +# limit = nil @results = {} @names = {} - SEARCH_IN.each do |key, description| - if @searching[key] - if [:enterprises, :people].include?(key) && @region - @results[key] = @finder.find(key, @filtered_query, :within => params[:radius], :region => @region.id, :product_category => @product_category, :limit => limit) - else - @results[key] = @finder.find(key, @filtered_query, :product_category => @product_category, :limit => limit) - end - end + + SEARCH_IN.select { |key,description| @searching[key] }.each do |key, description| + @results[key] = @finder.find(key, @filtered_query, calculate_find_options(key, limit, @product_category, @region, params[:radius])) @names[key] = gettext(description) end @@ -150,21 +155,9 @@ class SearchController < ApplicationController def products @results[:products].uniq! -# if !(@category || @product_category || @region || (!@query.blank?)) -# # not searching, no menu -# return -# end - - @categories = @results[:products].map(&:product_category).compact - @counts = @categories.uniq.inject({}) do |h, cat| - h[cat.id] = [cat, 0] - h - end - - @categories.each do |cat| - cat.hierarchy.each do |each_cat| - @counts[each_cat.id][1] += 1 if @counts[each_cat.id] - end + @categories = ProductCategory.menu_categories(@product_category) + @categories.map do |cat| + [cat, @finder.count(:products, @filtered_query, calculate_find_options(:products, nil, cat, @region, params[:radius]))] end @found_product_categories = @counts.values.sort_by{|v|v[0].full_name} diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 25053f9..3e7e6b6 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -32,8 +32,12 @@ class CategoryFinder asset_class(asset).find(:all, options_for_find_by_initial(asset_class(asset), initial)) end - def count(asset) - asset_class(asset).count(:all, options_for_find(asset_class(asset), :select => "#{asset_table(asset)}.id")) + def count(asset, query='', options={}) + if query.blank? + find(asset, query, options).size + else + find(asset, query, options).total_hits + end end def most_commented_articles(limit=10) diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index f31a638..ae7a421 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -43,8 +43,13 @@ class EnvironmentFinder @environment.send(asset).find_by_initial(initial) end - def count(asset) - @environment.send(asset).count + def count(asset, query = '', options = {}) + if query.blank? + # SLOW + find(asset, query, options).size + else + find(asset, query, options).total_hits + end end end diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index 58fd6a4..a4cef2b 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -157,6 +157,60 @@ class CategoryFinderTest < ActiveSupport::TestCase ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) assert_equal count+1, @finder.count('enterprises') end + + should 'count people' do + count = @finder.count('people') + p = create_user('testinguser').person + p.category_ids = [@category.id] + p.save! + + assert_equal count+1, @finder.count('people') + end + should 'count products' do + count = @finder.count('products') + + ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) + ent.products.create!(:name => 'test prodduct') + + assert_equal count+1, @finder.count('products') + end + should 'count articles' do + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') + + count = @finder.count('articles') + ent1.articles.create!(:name => 'teste1', :category_ids => [@category.id]) + + assert_equal count+1, @finder.count('articles') + end + should 'count events' do + count = @finder.count('events') + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') + + Event.create!(:name => 'teste2', :profile => ent1, :start_date => Date.today, :category_ids => [@category.id]) + assert_equal count+1, @finder.count('events') + end + + should 'count enterprises with query and options' do + options = mock + results = mock + + @finder.expects(:find).with('people', 'my query', options).returns(results) + + results.expects(:total_hits).returns(99) + + assert_equal 99, @finder.count('people', 'my query', options) + end + + should 'count enterprises without query but with options' do + options = mock + results = mock + + @finder.expects(:find).with('people', nil, options).returns(results) + + results.expects(:size).returns(99) + + assert_equal 99, @finder.count('people', nil, options) + end should 'not list more people than limit' do p1 = create_user('test1').person; p1.add_category(@category) diff --git a/test/unit/environment_finder_test.rb b/test/unit/environment_finder_test.rb index c656d68..fe72d46 100644 --- a/test/unit/environment_finder_test.rb +++ b/test/unit/environment_finder_test.rb @@ -51,12 +51,58 @@ class EnvironmentFinderTest < ActiveSupport::TestCase assert_not_includes recent, ent1 # older end - should 'count entrprises' do + should 'count enterprises' do finder = EnvironmentFinder.new(Environment.default) count = finder.count('enterprises') - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') + Enterprise.create!(:name => 'teste1', :identifier => 'teste1') assert_equal count+1, finder.count('enterprises') end + should 'count people' do + finder = EnvironmentFinder.new(Environment.default) + count = finder.count('people') + create_user('testinguser') + assert_equal count+1, finder.count('people') + end + should 'count products' do + finder = EnvironmentFinder.new(Environment.default) + count = finder.count('products') + + ent = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') + ent.products.create!(:name => 'test prodduct') + + assert_equal count+1, finder.count('products') + end + should 'count articles' do + finder = EnvironmentFinder.new(Environment.default) + + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') + + count = finder.count('articles') + ent1.articles.create!(:name => 'teste1') + + assert_equal count+1, finder.count('articles') + end + should 'count events' do + finder = EnvironmentFinder.new(Environment.default) + count = finder.count('events') + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') + + Event.create!(:name => 'teste2', :profile => ent1, :start_date => Date.today) + assert_equal count+1, finder.count('events') + end + + should 'count enterprises with query and options' do + env = Environment.default + finder = EnvironmentFinder.new(env) + options = mock + results = mock + + finder.expects(:find).with('people', 'my query', options).returns(results) + + results.expects(:total_hits).returns(99) + + assert_equal 99, finder.count('people', 'my query', options) + end should 'find articles by initial' do person = create_user('teste').person -- libgit2 0.21.2