Commit c60af9fbe5d9ea6096eb3eb0efe2d6e389a0a535
1 parent
dbe1051e
Exists in
master
and in
29 other branches
ActionItem129: fixed limit check for query in assets
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1917 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
5 changed files
with
35 additions
and
21 deletions
Show diff stats
app/controllers/public/search_controller.rb
@@ -98,7 +98,7 @@ class SearchController < ApplicationController | @@ -98,7 +98,7 @@ class SearchController < ApplicationController | ||
98 | ] | 98 | ] |
99 | 99 | ||
100 | # TODO don't hardcode like this >:-( | 100 | # TODO don't hardcode like this >:-( |
101 | - LIST_LIMIT = 20 | 101 | + LIST_LIMIT = 10 |
102 | 102 | ||
103 | def complete_region | 103 | def complete_region |
104 | @regions = Region.find(:all, :conditions => [ 'name like ? and lat is not null and lng is not null', '%' + params[:region][:name] + '%' ]) | 104 | @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 | @@ -113,13 +113,21 @@ class SearchController < ApplicationController | ||
113 | # FIXME name is not unique | 113 | # FIXME name is not unique |
114 | @region = Region.find_by_name(params[:region][:name]) if params[:region] | 114 | @region = Region.find_by_name(params[:region][:name]) if params[:region] |
115 | 115 | ||
116 | + # how many assets we are searching for? | ||
117 | + number_of_result_assets = @searching.values.select{|v| v}.size | ||
118 | + | ||
119 | + # apply limit when searching for only one type of asset | ||
120 | + limit = (number_of_result_assets == 1) ? LIST_LIMIT : nil | ||
121 | + | ||
116 | @results = {} | 122 | @results = {} |
117 | @names = {} | 123 | @names = {} |
118 | SEARCH_IN.each do |key, description| | 124 | SEARCH_IN.each do |key, description| |
119 | - if [:enterprises, :people].include?(key) && @region | ||
120 | - @results[key] = @finder.find(key, @filtered_query, :within => params[:radius], :region => @region.id, :product_category => @product_category) if @searching[key] | ||
121 | - else | ||
122 | - @results[key] = @finder.find(key, @filtered_query, :product_category => @product_category) if @searching[key] | 125 | + if @searching[key] |
126 | + if [:enterprises, :people].include?(key) && @region | ||
127 | + @results[key] = @finder.find(key, @filtered_query, :within => params[:radius], :region => @region.id, :product_category => @product_category, :limit => limit) | ||
128 | + else | ||
129 | + @results[key] = @finder.find(key, @filtered_query, :product_category => @product_category, :limit => limit) | ||
130 | + end | ||
123 | end | 131 | end |
124 | @names[key] = gettext(description) | 132 | @names[key] = gettext(description) |
125 | end | 133 | end |
@@ -138,6 +146,12 @@ class SearchController < ApplicationController | @@ -138,6 +146,12 @@ class SearchController < ApplicationController | ||
138 | end | 146 | end |
139 | 147 | ||
140 | def products | 148 | def products |
149 | + @results[:products].uniq! | ||
150 | + if !(@category || @product_category || @region || (!@query.blank?)) | ||
151 | + # not searching, no menu | ||
152 | + return | ||
153 | + end | ||
154 | + | ||
141 | @categories = @results[:products].map(&:product_category).compact | 155 | @categories = @results[:products].map(&:product_category).compact |
142 | @counts = @categories.uniq.inject({}) do |h, cat| | 156 | @counts = @categories.uniq.inject({}) do |h, cat| |
143 | h[cat.id] = [cat, 0] | 157 | h[cat.id] = [cat, 0] |
@@ -150,7 +164,7 @@ class SearchController < ApplicationController | @@ -150,7 +164,7 @@ class SearchController < ApplicationController | ||
150 | end | 164 | end |
151 | end | 165 | end |
152 | 166 | ||
153 | - @cats = @counts.values.sort_by{|v|v[0].full_name} | 167 | + @found_product_categories = @counts.values.sort_by{|v|v[0].full_name} |
154 | end | 168 | end |
155 | 169 | ||
156 | alias :assets :index | 170 | alias :assets :index |
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={}, limit = nil) | 12 | + def find(asset, query = nil, 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] |
@@ -18,14 +18,14 @@ class CategoryFinder | @@ -18,14 +18,14 @@ class CategoryFinder | ||
18 | end | 18 | end |
19 | 19 | ||
20 | if query.blank? | 20 | if query.blank? |
21 | - asset_class(asset).find(:all, options_for_find(asset_class(asset), {:limit => limit, :order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) | 21 | + asset_class(asset).find(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) |
22 | else | 22 | else |
23 | asset_class(asset).find_by_contents(query, {}, options_for_find(asset_class(asset), options)).uniq | 23 | asset_class(asset).find_by_contents(query, {}, options_for_find(asset_class(asset), options)).uniq |
24 | end | 24 | end |
25 | end | 25 | end |
26 | 26 | ||
27 | def recent(asset, limit = nil) | 27 | def recent(asset, limit = nil) |
28 | - find(asset, nil, {}, limit) | 28 | + find(asset, nil, :limit => limit) |
29 | end | 29 | end |
30 | 30 | ||
31 | def find_by_initial(asset, initial) | 31 | def find_by_initial(asset, initial) |
@@ -59,9 +59,9 @@ class CategoryFinder | @@ -59,9 +59,9 @@ class CategoryFinder | ||
59 | {: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) | 59 | {: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) |
60 | when 'Product' | 60 | when 'Product' |
61 | if prod_cat_ids | 61 | if prod_cat_ids |
62 | - {: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) | 62 | + {: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) |
63 | else | 63 | else |
64 | - {: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) | 64 | + {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id in (?)', category_ids]}.merge!(options) |
65 | end | 65 | end |
66 | when 'Article', 'Person', 'Community', 'Enterprise', 'Event' | 66 | when 'Article', 'Person', 'Community', 'Enterprise', 'Event' |
67 | {:include => 'categories', :conditions => ['categories.id IN (?)', category_ids]}.merge!(options) | 67 | {:include => 'categories', :conditions => ['categories.id IN (?)', category_ids]}.merge!(options) |
app/models/environment_finder.rb
@@ -4,7 +4,7 @@ class EnvironmentFinder | @@ -4,7 +4,7 @@ class EnvironmentFinder | ||
4 | @environment = env | 4 | @environment = env |
5 | end | 5 | end |
6 | 6 | ||
7 | - def find(asset, query = nil, options={}, limit = nil) | 7 | + def find(asset, query = nil, options={}) |
8 | @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) | 8 | @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) |
9 | if @region && options[:within] | 9 | if @region && options[:within] |
10 | options[:origin] = [@region.lat, @region.lng] | 10 | options[:origin] = [@region.lat, @region.lng] |
@@ -17,9 +17,9 @@ class EnvironmentFinder | @@ -17,9 +17,9 @@ class EnvironmentFinder | ||
17 | 17 | ||
18 | if query.blank? | 18 | if query.blank? |
19 | if product_category && asset == :products | 19 | if product_category && asset == :products |
20 | - @environment.send(asset).find(:all, options.merge({:limit => limit, :order => 'created_at desc, id desc', :conditions => ['product_category_id in (?)', product_category_ids]})) | 20 | + @environment.send(asset).find(:all, options.merge({:order => 'created_at desc, id desc', :conditions => ['product_category_id in (?)', product_category_ids]})) |
21 | else | 21 | else |
22 | - @environment.send(asset).find( :all, options.merge( {:limit => limit, :order => 'created_at desc, id desc'} ) ) | 22 | + @environment.send(asset).find( :all, options.merge( {:order => 'created_at desc, id desc'} ) ) |
23 | end | 23 | end |
24 | else | 24 | else |
25 | if product_category && asset == :products | 25 | if product_category && asset == :products |
@@ -32,7 +32,7 @@ class EnvironmentFinder | @@ -32,7 +32,7 @@ class EnvironmentFinder | ||
32 | end | 32 | end |
33 | 33 | ||
34 | def recent(asset, limit = nil) | 34 | def recent(asset, limit = nil) |
35 | - find(asset, nil, {}, limit) | 35 | + find(asset, nil, :limit => limit) |
36 | end | 36 | end |
37 | 37 | ||
38 | def find_by_initial(asset, initial) | 38 | def find_by_initial(asset, initial) |
script/extract_sies_data.rb
@@ -74,7 +74,7 @@ categories[#{cat.id}] = cat#{@seq}.id | @@ -74,7 +74,7 @@ categories[#{cat.id}] = cat#{@seq}.id | ||
74 | @seq += 1 | 74 | @seq += 1 |
75 | 75 | ||
76 | Category.find(:all, :conditions => { :id_mae => cat.id }).each do |child| | 76 | Category.find(:all, :conditions => { :id_mae => cat.id }).each do |child| |
77 | - dump_category(child, cat) #if (DUMP_ALL || (@seq <= LIMIT)) | 77 | + dump_category(child, cat) if (DUMP_ALL || (@seq <= LIMIT)) |
78 | end | 78 | end |
79 | 79 | ||
80 | end | 80 | end |
@@ -111,7 +111,7 @@ categories[#{cat.id}] = cat#{@seq}.id | @@ -111,7 +111,7 @@ categories[#{cat.id}] = cat#{@seq}.id | ||
111 | @r_seqs[city] = @r_seq | 111 | @r_seqs[city] = @r_seq |
112 | puts <<-EOF | 112 | puts <<-EOF |
113 | city#{@r_seq} = new_region(#{city.cidade.inspect}, STATES[#{city.id.to_s[0..1]}], #{city.latitude}, #{city.longitude}) | 113 | city#{@r_seq} = new_region(#{city.cidade.inspect}, STATES[#{city.id.to_s[0..1]}], #{city.latitude}, #{city.longitude}) |
114 | -cities[#{city.id}] = city#{@r_seq}.id | 114 | +cities[#{city.id}] = city#{@r_seq} |
115 | EOF | 115 | EOF |
116 | @r_seq += 1 | 116 | @r_seq += 1 |
117 | end | 117 | end |
@@ -134,7 +134,7 @@ Category.find(:all, :conditions => 'id_mae is null or id_mae = -1', :limit => LI | @@ -134,7 +134,7 @@ Category.find(:all, :conditions => 'id_mae is null or id_mae = -1', :limit => LI | ||
134 | dumper.dump_category(cat, nil) | 134 | dumper.dump_category(cat, nil) |
135 | end | 135 | end |
136 | 136 | ||
137 | -puts "regions = {}" | 137 | +puts "cities = {}" |
138 | City.find(:all, :limit => LIMIT).each do |city| | 138 | City.find(:all, :limit => LIMIT).each do |city| |
139 | dumper.dump_city(city) | 139 | dumper.dump_city(city) |
140 | end | 140 | end |
test/functional/search_controller_test.rb
@@ -776,7 +776,7 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -776,7 +776,7 @@ class SearchControllerTest < Test::Unit::TestCase | ||
776 | 776 | ||
777 | cat1.products.create!(:name => 'prod test 1', :enterprise => ent) | 777 | cat1.products.create!(:name => 'prod test 1', :enterprise => ent) |
778 | 778 | ||
779 | - get :index, :find_in => 'products' | 779 | + get :index, :find_in => 'products', :query => 'test' |
780 | 780 | ||
781 | assert_equal 1, assigns(:counts)[cat1.id][1] | 781 | assert_equal 1, assigns(:counts)[cat1.id][1] |
782 | assert_equal nil, assigns(:counts)[cat2.id] | 782 | assert_equal nil, assigns(:counts)[cat2.id] |
@@ -789,7 +789,7 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -789,7 +789,7 @@ class SearchControllerTest < Test::Unit::TestCase | ||
789 | 789 | ||
790 | cat1.products.create!(:name => 'prod test 1', :enterprise => ent) | 790 | cat1.products.create!(:name => 'prod test 1', :enterprise => ent) |
791 | 791 | ||
792 | - get :index, :find_in => 'products' | 792 | + get :index, :find_in => 'products', :query => 'test' |
793 | 793 | ||
794 | assert_equal 1, assigns(:counts)[cat1.id][1] | 794 | assert_equal 1, assigns(:counts)[cat1.id][1] |
795 | assert_equal nil, assigns(:counts)[cat2.id] | 795 | assert_equal nil, assigns(:counts)[cat2.id] |
@@ -803,7 +803,7 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -803,7 +803,7 @@ class SearchControllerTest < Test::Unit::TestCase | ||
803 | cat1.products.create!(:name => 'prod test 1', :enterprise => ent) | 803 | cat1.products.create!(:name => 'prod test 1', :enterprise => ent) |
804 | cat2.products.create!(:name => 'prod test 2', :enterprise => ent) | 804 | cat2.products.create!(:name => 'prod test 2', :enterprise => ent) |
805 | 805 | ||
806 | - get :index, :find_in => 'products' | 806 | + get :index, :find_in => 'products', :query => 'test' |
807 | 807 | ||
808 | assert_equal 2, assigns(:counts)[cat1.id][1] | 808 | assert_equal 2, assigns(:counts)[cat1.id][1] |
809 | assert_equal 1, assigns(:counts)[cat2.id][1] | 809 | assert_equal 1, assigns(:counts)[cat2.id][1] |