Commit c60af9fbe5d9ea6096eb3eb0efe2d6e389a0a535

Authored by MoisesMachado
1 parent dbe1051e

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
app/controllers/public/search_controller.rb
... ... @@ -98,7 +98,7 @@ class SearchController < ApplicationController
98 98 ]
99 99  
100 100 # TODO don't hardcode like this >:-(
101   - LIST_LIMIT = 20
  101 + LIST_LIMIT = 10
102 102  
103 103 def complete_region
104 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 113 # FIXME name is not unique
114 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 122 @results = {}
117 123 @names = {}
118 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 131 end
124 132 @names[key] = gettext(description)
125 133 end
... ... @@ -138,6 +146,12 @@ class SearchController < ApplicationController
138 146 end
139 147  
140 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 155 @categories = @results[:products].map(&:product_category).compact
142 156 @counts = @categories.uniq.inject({}) do |h, cat|
143 157 h[cat.id] = [cat, 0]
... ... @@ -150,7 +164,7 @@ class SearchController < ApplicationController
150 164 end
151 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 168 end
155 169  
156 170 alias :assets :index
... ...
app/models/category_finder.rb
... ... @@ -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 13 @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
14 14 if @region && options[:within]
15 15 options[:origin] = [@region.lat, @region.lng]
... ... @@ -18,14 +18,14 @@ class CategoryFinder
18 18 end
19 19  
20 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 22 else
23 23 asset_class(asset).find_by_contents(query, {}, options_for_find(asset_class(asset), options)).uniq
24 24 end
25 25 end
26 26  
27 27 def recent(asset, limit = nil)
28   - find(asset, nil, {}, limit)
  28 + find(asset, nil, :limit => limit)
29 29 end
30 30  
31 31 def find_by_initial(asset, initial)
... ... @@ -59,9 +59,9 @@ class CategoryFinder
59 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 60 when 'Product'
61 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 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 65 end
66 66 when 'Article', 'Person', 'Community', 'Enterprise', 'Event'
67 67 {:include => 'categories', :conditions => ['categories.id IN (?)', category_ids]}.merge!(options)
... ...
app/models/environment_finder.rb
... ... @@ -4,7 +4,7 @@ class EnvironmentFinder
4 4 @environment = env
5 5 end
6 6  
7   - def find(asset, query = nil, options={}, limit = nil)
  7 + def find(asset, query = nil, options={})
8 8 @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
9 9 if @region && options[:within]
10 10 options[:origin] = [@region.lat, @region.lng]
... ... @@ -17,9 +17,9 @@ class EnvironmentFinder
17 17  
18 18 if query.blank?
19 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 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 23 end
24 24 else
25 25 if product_category && asset == :products
... ... @@ -32,7 +32,7 @@ class EnvironmentFinder
32 32 end
33 33  
34 34 def recent(asset, limit = nil)
35   - find(asset, nil, {}, limit)
  35 + find(asset, nil, :limit => limit)
36 36 end
37 37  
38 38 def find_by_initial(asset, initial)
... ...
script/extract_sies_data.rb
... ... @@ -74,7 +74,7 @@ categories[#{cat.id}] = cat#{@seq}.id
74 74 @seq += 1
75 75  
76 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 78 end
79 79  
80 80 end
... ... @@ -111,7 +111,7 @@ categories[#{cat.id}] = cat#{@seq}.id
111 111 @r_seqs[city] = @r_seq
112 112 puts <<-EOF
113 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 115 EOF
116 116 @r_seq += 1
117 117 end
... ... @@ -134,7 +134,7 @@ Category.find(:all, :conditions =&gt; &#39;id_mae is null or id_mae = -1&#39;, :limit =&gt; LI
134 134 dumper.dump_category(cat, nil)
135 135 end
136 136  
137   -puts "regions = {}"
  137 +puts "cities = {}"
138 138 City.find(:all, :limit => LIMIT).each do |city|
139 139 dumper.dump_city(city)
140 140 end
... ...
test/functional/search_controller_test.rb
... ... @@ -776,7 +776,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
776 776  
777 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 781 assert_equal 1, assigns(:counts)[cat1.id][1]
782 782 assert_equal nil, assigns(:counts)[cat2.id]
... ... @@ -789,7 +789,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
789 789  
790 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 794 assert_equal 1, assigns(:counts)[cat1.id][1]
795 795 assert_equal nil, assigns(:counts)[cat2.id]
... ... @@ -803,7 +803,7 @@ class SearchControllerTest &lt; Test::Unit::TestCase
803 803 cat1.products.create!(:name => 'prod test 1', :enterprise => ent)
804 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 808 assert_equal 2, assigns(:counts)[cat1.id][1]
809 809 assert_equal 1, assigns(:counts)[cat2.id][1]
... ...