diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index f8f68ff..e2e77e9 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -98,7 +98,7 @@ class SearchController < ApplicationController ] # TODO don't hardcode like this >:-( - LIST_LIMIT = 20 + LIST_LIMIT = 10 def complete_region @regions = Region.find(:all, :conditions => [ 'name like ? and lat is not null and lng is not null', '%' + params[:region][:name] + '%' ]) @@ -113,13 +113,21 @@ class SearchController < ApplicationController # FIXME name is not unique @region = Region.find_by_name(params[:region][:name]) if params[:region] + # how many assets we are searching for? + 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 + @results = {} @names = {} SEARCH_IN.each do |key, description| - if [:enterprises, :people].include?(key) && @region - @results[key] = @finder.find(key, @filtered_query, :within => params[:radius], :region => @region.id, :product_category => @product_category) if @searching[key] - else - @results[key] = @finder.find(key, @filtered_query, :product_category => @product_category) if @searching[key] + 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 @names[key] = gettext(description) end @@ -138,6 +146,12 @@ class SearchController < ApplicationController end 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] @@ -150,7 +164,7 @@ class SearchController < ApplicationController end end - @cats = @counts.values.sort_by{|v|v[0].full_name} + @found_product_categories = @counts.values.sort_by{|v|v[0].full_name} end alias :assets :index diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 8b6943a..245da36 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={}, limit = nil) + def find(asset, query = nil, options={}) @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) if @region && options[:within] options[:origin] = [@region.lat, @region.lng] @@ -18,14 +18,14 @@ class CategoryFinder end if query.blank? - asset_class(asset).find(:all, options_for_find(asset_class(asset), {:limit => limit, :order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) + asset_class(asset).find(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) else asset_class(asset).find_by_contents(query, {}, options_for_find(asset_class(asset), options)).uniq end end def recent(asset, limit = nil) - find(asset, nil, {}, limit) + find(asset, nil, :limit => limit) end def find_by_initial(asset, initial) @@ -59,9 +59,9 @@ class CategoryFinder {:select => 'distinct comments.*', :joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id in (?)', category_ids]}.merge!(options) when 'Product' if prod_cat_ids - {:select => 'distinct products.*', :joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and products.product_category_id in (?)', category_ids, prod_cat_ids]}.merge!(options) + {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?) and products.product_category_id in (?)', category_ids, prod_cat_ids]}.merge!(options) else - {:select => 'distinct products.*', :joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?)', category_ids]}.merge!(options) + {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?)', category_ids]}.merge!(options) end when 'Article', 'Person', 'Community', 'Enterprise', 'Event' {:include => 'categories', :conditions => ['categories.id IN (?)', category_ids]}.merge!(options) diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index 539478a..9678789 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -4,7 +4,7 @@ class EnvironmentFinder @environment = env end - def find(asset, query = nil, options={}, limit = nil) + def find(asset, query = nil, 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,9 @@ class EnvironmentFinder if query.blank? if product_category && asset == :products - @environment.send(asset).find(:all, options.merge({:limit => limit, :order => 'created_at desc, id desc', :conditions => ['product_category_id in (?)', product_category_ids]})) + @environment.send(asset).find(:all, options.merge({:order => 'created_at desc, id desc', :conditions => ['product_category_id in (?)', product_category_ids]})) else - @environment.send(asset).find( :all, options.merge( {:limit => limit, :order => 'created_at desc, id desc'} ) ) + @environment.send(asset).find( :all, options.merge( {:order => 'created_at desc, id desc'} ) ) end else if product_category && asset == :products @@ -32,7 +32,7 @@ class EnvironmentFinder end def recent(asset, limit = nil) - find(asset, nil, {}, limit) + find(asset, nil, :limit => limit) end def find_by_initial(asset, initial) diff --git a/script/extract_sies_data.rb b/script/extract_sies_data.rb index 4211d03..8189eb5 100644 --- a/script/extract_sies_data.rb +++ b/script/extract_sies_data.rb @@ -74,7 +74,7 @@ categories[#{cat.id}] = cat#{@seq}.id @seq += 1 Category.find(:all, :conditions => { :id_mae => cat.id }).each do |child| - dump_category(child, cat) #if (DUMP_ALL || (@seq <= LIMIT)) + dump_category(child, cat) if (DUMP_ALL || (@seq <= LIMIT)) end end @@ -111,7 +111,7 @@ categories[#{cat.id}] = cat#{@seq}.id @r_seqs[city] = @r_seq puts <<-EOF city#{@r_seq} = new_region(#{city.cidade.inspect}, STATES[#{city.id.to_s[0..1]}], #{city.latitude}, #{city.longitude}) -cities[#{city.id}] = city#{@r_seq}.id +cities[#{city.id}] = city#{@r_seq} EOF @r_seq += 1 end @@ -134,7 +134,7 @@ Category.find(:all, :conditions => 'id_mae is null or id_mae = -1', :limit => LI dumper.dump_category(cat, nil) end -puts "regions = {}" +puts "cities = {}" City.find(:all, :limit => LIMIT).each do |city| dumper.dump_city(city) end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 05b62b1..f44960a 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -776,7 +776,7 @@ class SearchControllerTest < Test::Unit::TestCase cat1.products.create!(:name => 'prod test 1', :enterprise => ent) - get :index, :find_in => 'products' + get :index, :find_in => 'products', :query => 'test' assert_equal 1, assigns(:counts)[cat1.id][1] assert_equal nil, assigns(:counts)[cat2.id] @@ -789,7 +789,7 @@ class SearchControllerTest < Test::Unit::TestCase cat1.products.create!(:name => 'prod test 1', :enterprise => ent) - get :index, :find_in => 'products' + get :index, :find_in => 'products', :query => 'test' assert_equal 1, assigns(:counts)[cat1.id][1] assert_equal nil, assigns(:counts)[cat2.id] @@ -803,7 +803,7 @@ class SearchControllerTest < Test::Unit::TestCase cat1.products.create!(:name => 'prod test 1', :enterprise => ent) cat2.products.create!(:name => 'prod test 2', :enterprise => ent) - get :index, :find_in => 'products' + get :index, :find_in => 'products', :query => 'test' assert_equal 2, assigns(:counts)[cat1.id][1] assert_equal 1, assigns(:counts)[cat2.id][1] -- libgit2 0.21.2