Commit eb63d2149a4a469f62ffd0126dee4ee0a2431de2
1 parent
10fd71d9
Exists in
master
and in
29 other branches
Removed category_finder and put it on search controller
Showing
2 changed files
with
62 additions
and
147 deletions
Show diff stats
app/controllers/public/search_controller.rb
1 | 1 | class SearchController < PublicController |
2 | 2 | |
3 | + MAP_SEARCH_LIMIT = 2000 | |
4 | + SINGLE_SEARCH_LIMIT = 20 | |
5 | + MULTIPLE_SEARCH_LIMIT = 6 | |
6 | + | |
3 | 7 | helper TagsHelper |
4 | 8 | include SearchHelper |
5 | 9 | |
... | ... | @@ -178,13 +182,15 @@ class SearchController < PublicController |
178 | 182 | |
179 | 183 | # view the summary of one category |
180 | 184 | def category_index |
185 | + @searching | |
181 | 186 | @results = {} |
182 | 187 | @order = [] |
183 | 188 | @names = {} |
189 | + limit = MULTIPLE_SEARCH_LIMIT | |
184 | 190 | [ |
185 | - [ :people, _('People'), recent('people') ], | |
186 | - [ :enterprises, __('Enterprises'), recent('enterprises') ], | |
187 | - [ :products, _('Products'), recent('products') ], | |
191 | + [ :people, _('People'), recent('people', limit) ], | |
192 | + [ :enterprises, __('Enterprises'), recent('enterprises', limit) ], | |
193 | + [ :products, _('Products'), recent('products', limit) ], | |
188 | 194 | [ :events, _('Upcoming events'), upcoming_events({:per_page => limit}) ], |
189 | 195 | [ :communities, __('Communities'), recent('communities', limit) ], |
190 | 196 | [ :most_commented_articles, _('Most commented articles'), most_commented_articles(limit) ], |
... | ... | @@ -200,12 +206,24 @@ class SearchController < PublicController |
200 | 206 | protected |
201 | 207 | |
202 | 208 | def recent(asset, limit = nil) |
203 | - find(asset, nil, :limit => limit, :order => 'created_at DESC, id DESC') | |
209 | + options = {:page => 1, :per_page => limit, :order => 'created_at DESC, id DESC'} | |
210 | + | |
211 | + @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) | |
212 | + | |
213 | + if asset == :events | |
214 | + finder_method = 'find' | |
215 | + options.delete(:page) | |
216 | + options.delete(:per_page) | |
217 | + else | |
218 | + finder_method = 'paginate' | |
219 | + end | |
220 | + | |
221 | + asset_class(asset).send(finder_method, :all, category_options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options))) | |
204 | 222 | end |
205 | 223 | |
206 | 224 | def most_commented_articles(limit=10, options={}) |
207 | 225 | options = {:page => 1, :per_page => limit, :order => 'comments_count DESC'}.merge(options) |
208 | - Article.paginate(:all, options_for_find(Article, options)) | |
226 | + Article.paginate(:all, category_options_for_find(Article, options)) | |
209 | 227 | end |
210 | 228 | |
211 | 229 | def upcoming_events(options = {}) |
... | ... | @@ -347,9 +365,46 @@ class SearchController < PublicController |
347 | 365 | def limit |
348 | 366 | searching = @searching.values.select{|v|v} |
349 | 367 | if params[:display] == 'map' |
350 | - 2000 | |
368 | + MAP_SEARCH_LIMIT | |
369 | + else | |
370 | + (searching.size == 1) ? SINGLE_SEARCH_LIMIT : MULTIPLE_SEARCH_LIMIT | |
371 | + end | |
372 | + end | |
373 | + | |
374 | + def category_options_for_find(klass, options={}, date_range = nil) | |
375 | + if defined? options[:product_category] | |
376 | + prod_cat = options.delete(:product_category) | |
377 | + end | |
378 | + | |
379 | + case klass.name | |
380 | + when 'Comment' | |
381 | + {:joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options) | |
382 | + when 'Product' | |
383 | + if prod_cat | |
384 | + {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['categories_profiles.category_id = (?) and product_categorizations.category_id = (?)', category_id, prod_cat.id]}.merge!(options) | |
385 | + else | |
386 | + {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) | |
387 | + end | |
388 | + when 'Article', 'TextArticle' | |
389 | + {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options) | |
390 | + when 'Event' | |
391 | + conditions = | |
392 | + if date_range | |
393 | + ['articles_categories.category_id = (:category_id) and (start_date BETWEEN :start_day AND :end_day OR end_date BETWEEN :start_day AND :end_day)', {:category_id => category_id, :start_day => date_range.first, :end_day => date_range.last} ] | |
394 | + else | |
395 | + ['articles_categories.category_id = (?) ', category_id ] | |
396 | + end | |
397 | + {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => conditions}.merge!(options) | |
398 | + when 'Enterprise' | |
399 | + if prod_cat | |
400 | + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id) inner join products on (products.enterprise_id = profiles.id) inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['categories_profiles.category_id = (?) and product_categorizations.category_id = (?)', category_id, prod_cat.id]}.merge!(options) | |
401 | + else | |
402 | + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) | |
403 | + end | |
404 | + when 'Person', 'Community' | |
405 | + {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) | |
351 | 406 | else |
352 | - (searching.size == 1) ? 20 : 6 | |
407 | + raise "unreconized class #{klass.name}" | |
353 | 408 | end |
354 | 409 | end |
355 | 410 | ... | ... |
app/models/category_finder.rb
... | ... | @@ -1,140 +0,0 @@ |
1 | -class CategoryFinder | |
2 | - | |
3 | - def initialize(cat) | |
4 | - @category = cat | |
5 | - @category_id = @category.id | |
6 | - end | |
7 | - | |
8 | - attr_reader :category_id | |
9 | - | |
10 | - def find(asset, query='', options={}) | |
11 | - @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) | |
12 | - if @region && options[:within] | |
13 | - options[:origin] = [@region.lat, @region.lng] | |
14 | - else | |
15 | - options.delete(:within) | |
16 | - end | |
17 | - | |
18 | - date_range = options.delete(:date_range) | |
19 | - | |
20 | - options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) | |
21 | - | |
22 | - if asset == :events | |
23 | - finder_method = 'find' | |
24 | - options.delete(:page) | |
25 | - options.delete(:per_page) | |
26 | - else | |
27 | - finder_method = 'paginate' | |
28 | - end | |
29 | - | |
30 | - if query.blank? | |
31 | - options.delete(:facets) | |
32 | - options.delete(:order_by) | |
33 | - | |
34 | - asset_class(asset).send(finder_method, :all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range)) | |
35 | - else | |
36 | - pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} | |
37 | - solr_options = {:facets => options.delete(:facets), :order => options.delete(:order_by)} | |
38 | - | |
39 | - asset_class(asset).find_by_contents(query, pg_options, solr_options, options_for_find(asset_class(asset), options, date_range))[:results] | |
40 | - end | |
41 | - end | |
42 | - | |
43 | - def current_events(year, month, options={}) | |
44 | - options.delete(:page) | |
45 | - options.delete(:per_page) | |
46 | - | |
47 | - range = Event.date_range(year, month) | |
48 | - | |
49 | - Event.find(:all, {:include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }}.merge(options)) | |
50 | - end | |
51 | - | |
52 | - def upcoming_events(options = {}) | |
53 | - options.delete(:page) | |
54 | - options.delete(:per_page) | |
55 | - | |
56 | - Event.find(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options)) | |
57 | - end | |
58 | - | |
59 | - def product_categories_count(asset, product_categories_ids, objects_ids=nil) | |
60 | - conditions = [ "product_categorizations.category_id in (?) and #{ProfileCategorization.table_name}.category_id = ?", product_categories_ids, category_id] | |
61 | - | |
62 | - if asset == :products | |
63 | - if objects_ids | |
64 | - conditions[0] += ' and product_categorizations.product_id in (?)' | |
65 | - conditions << objects_ids | |
66 | - end | |
67 | - ProductCategory.find( | |
68 | - :all, | |
69 | - :select => 'categories.id, count(*) as total', | |
70 | - :joins => "inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id) inner join #{ProfileCategorization.table_name} on (#{ProfileCategorization.table_name}.profile_id = products.enterprise_id)", | |
71 | - :group => 'categories.id', | |
72 | - :conditions => conditions | |
73 | - ) | |
74 | - elsif asset == :enterprises | |
75 | - if objects_ids | |
76 | - conditions[0] += ' and products.enterprise_id in (?)' | |
77 | - conditions << objects_ids | |
78 | - end | |
79 | - ProductCategory.find( | |
80 | - :all, | |
81 | - :select => 'categories.id, count(distinct products.enterprise_id) as total', | |
82 | - :joins => "inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id) inner join #{ProfileCategorization.table_name} on (#{ProfileCategorization.table_name}.profile_id = products.enterprise_id)", | |
83 | - :group => 'categories.id', | |
84 | - :conditions => conditions | |
85 | - ) | |
86 | - else | |
87 | - raise ArgumentError, 'only products and enterprises supported' | |
88 | - end.inject({}) do |results,pc| | |
89 | - results[pc.id]= pc.total.to_i | |
90 | - results | |
91 | - end | |
92 | - end | |
93 | - | |
94 | - protected | |
95 | - | |
96 | - def options_for_find(klass, options={}, date_range = nil) | |
97 | - if defined? options[:product_category] | |
98 | - prod_cat = options.delete(:product_category) | |
99 | - end | |
100 | - | |
101 | - case klass.name | |
102 | - when 'Comment' | |
103 | - {:joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options) | |
104 | - when 'Product' | |
105 | - if prod_cat | |
106 | - {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['categories_profiles.category_id = (?) and product_categorizations.category_id = (?)', category_id, prod_cat.id]}.merge!(options) | |
107 | - else | |
108 | - {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) | |
109 | - end | |
110 | - when 'Article', 'TextArticle' | |
111 | - {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options) | |
112 | - when 'Event' | |
113 | - conditions = | |
114 | - if date_range | |
115 | - ['articles_categories.category_id = (:category_id) and (start_date BETWEEN :start_day AND :end_day OR end_date BETWEEN :start_day AND :end_day)', {:category_id => category_id, :start_day => date_range.first, :end_day => date_range.last} ] | |
116 | - else | |
117 | - ['articles_categories.category_id = (?) ', category_id ] | |
118 | - end | |
119 | - {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => conditions}.merge!(options) | |
120 | - when 'Enterprise' | |
121 | - if prod_cat | |
122 | - {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id) inner join products on (products.enterprise_id = profiles.id) inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['categories_profiles.category_id = (?) and product_categorizations.category_id = (?)', category_id, prod_cat.id]}.merge!(options) | |
123 | - else | |
124 | - {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) | |
125 | - end | |
126 | - when 'Person', 'Community' | |
127 | - {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options) | |
128 | - else | |
129 | - raise "unreconized class #{klass.name}" | |
130 | - end | |
131 | - end | |
132 | - | |
133 | - def asset_class(asset) | |
134 | - asset.to_s.singularize.camelize.constantize | |
135 | - end | |
136 | - | |
137 | - def asset_table(asset) | |
138 | - asset_class(asset).table_name | |
139 | - end | |
140 | -end |