Commit 5a8294ce60700a0fea36f07c492da3f28cb1c0ed
1 parent
9395097d
Exists in
master
and in
22 other branches
Removed environment_finder and search popup
Showing
3 changed files
with
57 additions
and
206 deletions
Show diff stats
app/controllers/public/search_controller.rb
| @@ -8,7 +8,6 @@ class SearchController < PublicController | @@ -8,7 +8,6 @@ class SearchController < PublicController | ||
| 8 | include SearchHelper | 8 | include SearchHelper |
| 9 | 9 | ||
| 10 | before_filter :load_category | 10 | before_filter :load_category |
| 11 | - before_filter :check_search_whole_site | ||
| 12 | before_filter :load_search_assets | 11 | before_filter :load_search_assets |
| 13 | 12 | ||
| 14 | no_design_blocks | 13 | no_design_blocks |
| @@ -159,7 +158,7 @@ class SearchController < PublicController | @@ -159,7 +158,7 @@ class SearchController < PublicController | ||
| 159 | @order = [] | 158 | @order = [] |
| 160 | @names = {} | 159 | @names = {} |
| 161 | 160 | ||
| 162 | - where_to_search.select { |key,description| @searching[key] }.each do |key, description| | 161 | + @enabled_searchs.select { |key,description| @searching[key] }.each do |key, description| |
| 163 | send(key) | 162 | send(key) |
| 164 | @order << key | 163 | @order << key |
| 165 | @names[key] = getterm(description) | 164 | @names[key] = getterm(description) |
| @@ -182,7 +181,6 @@ class SearchController < PublicController | @@ -182,7 +181,6 @@ class SearchController < PublicController | ||
| 182 | 181 | ||
| 183 | # view the summary of one category | 182 | # view the summary of one category |
| 184 | def category_index | 183 | def category_index |
| 185 | - @searching | ||
| 186 | @results = {} | 184 | @results = {} |
| 187 | @order = [] | 185 | @order = [] |
| 188 | @names = {} | 186 | @names = {} |
| @@ -203,13 +201,37 @@ class SearchController < PublicController | @@ -203,13 +201,37 @@ class SearchController < PublicController | ||
| 203 | @facets = {} | 201 | @facets = {} |
| 204 | end | 202 | end |
| 205 | 203 | ||
| 204 | + def tags | ||
| 205 | + @tags_cache_key = "tags_env_#{environment.id.to_s}" | ||
| 206 | + if is_cache_expired?(@tags_cache_key, true) | ||
| 207 | + @tags = environment.tag_counts | ||
| 208 | + end | ||
| 209 | + end | ||
| 210 | + | ||
| 211 | + def tag | ||
| 212 | + @tag = params[:tag] | ||
| 213 | + @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_env_#{environment.id.to_s}_page_#{params[:npage]}" | ||
| 214 | + if is_cache_expired?(@tag_cache_key, true) | ||
| 215 | + @tagged = environment.articles.find_tagged_with(@tag).paginate(:per_page => 10, :page => params[:npage]) | ||
| 216 | + end | ||
| 217 | + end | ||
| 218 | + | ||
| 219 | + def events_by_day | ||
| 220 | + @selected_day = build_date(params[:year], params[:month], params[:day]) | ||
| 221 | + if params[:category_id] and Category.exists?(params[:category_id]) | ||
| 222 | + @events_of_the_day = environment.events.by_day(@selected_day).in_category(Category.find(params[:category_id])) | ||
| 223 | + else | ||
| 224 | + @events_of_the_day = environment.events.by_day(@selected_day) | ||
| 225 | + end | ||
| 226 | + render :partial => 'events/events_by_day' | ||
| 227 | + end | ||
| 228 | + | ||
| 229 | + ####################################################### | ||
| 206 | protected | 230 | protected |
| 207 | 231 | ||
| 208 | def recent(asset, limit = nil) | 232 | def recent(asset, limit = nil) |
| 209 | options = {:page => 1, :per_page => limit, :order => 'created_at DESC, id DESC'} | 233 | options = {:page => 1, :per_page => limit, :order => 'created_at DESC, id DESC'} |
| 210 | 234 | ||
| 211 | - @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) | ||
| 212 | - | ||
| 213 | if asset == :events | 235 | if asset == :events |
| 214 | finder_method = 'find' | 236 | finder_method = 'find' |
| 215 | options.delete(:page) | 237 | options.delete(:page) |
| @@ -233,66 +255,6 @@ class SearchController < PublicController | @@ -233,66 +255,6 @@ class SearchController < PublicController | ||
| 233 | Event.find(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options)) | 255 | Event.find(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options)) |
| 234 | end | 256 | end |
| 235 | 257 | ||
| 236 | - | ||
| 237 | - attr_reader :category | ||
| 238 | - attr_reader :category_id | ||
| 239 | - | ||
| 240 | - def load_category | ||
| 241 | - unless params[:category_path].blank? | ||
| 242 | - path = params[:category_path].join('/') | ||
| 243 | - @category = environment.categories.find_by_path(path) | ||
| 244 | - if @category.nil? | ||
| 245 | - render_not_found(path) | ||
| 246 | - end | ||
| 247 | - @category_id = @category.id | ||
| 248 | - end | ||
| 249 | - end | ||
| 250 | - | ||
| 251 | - def where_to_search | ||
| 252 | - [ | ||
| 253 | - [ :articles, N_('Articles') ], | ||
| 254 | - [ :enterprises, N_('Enterprises') ], | ||
| 255 | - [ :people, N_('People') ], | ||
| 256 | - [ :communities, N_('Communities') ], | ||
| 257 | - [ :products, N_('Products') ], | ||
| 258 | - [ :events, N_('Events') ] | ||
| 259 | - ].select {|key, name| !environment.enabled?('disable_asset_' + key.to_s) } | ||
| 260 | - end | ||
| 261 | - | ||
| 262 | - ####################################################### | ||
| 263 | - | ||
| 264 | - def tags | ||
| 265 | - @tags_cache_key = "tags_env_#{environment.id.to_s}" | ||
| 266 | - if is_cache_expired?(@tags_cache_key, true) | ||
| 267 | - @tags = environment.tag_counts | ||
| 268 | - end | ||
| 269 | - end | ||
| 270 | - | ||
| 271 | - def tag | ||
| 272 | - @tag = params[:tag] | ||
| 273 | - @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_env_#{environment.id.to_s}_page_#{params[:npage]}" | ||
| 274 | - if is_cache_expired?(@tag_cache_key, true) | ||
| 275 | - @tagged = environment.articles.find_tagged_with(@tag).paginate(:per_page => 10, :page => params[:npage]) | ||
| 276 | - end | ||
| 277 | - end | ||
| 278 | - | ||
| 279 | - ####################################################### | ||
| 280 | - | ||
| 281 | - def popup | ||
| 282 | - @regions = Region.find(:all).select{|r|r.lat && r.lng} | ||
| 283 | - render :action => 'popup', :layout => false | ||
| 284 | - end | ||
| 285 | - | ||
| 286 | - def events_by_day | ||
| 287 | - @selected_day = build_date(params[:year], params[:month], params[:day]) | ||
| 288 | - if params[:category_id] and Category.exists?(params[:category_id]) | ||
| 289 | - @events_of_the_day = environment.events.by_day(@selected_day).in_category(Category.find(params[:category_id])) | ||
| 290 | - else | ||
| 291 | - @events_of_the_day = environment.events.by_day(@selected_day) | ||
| 292 | - end | ||
| 293 | - render :partial => 'events/events_by_day' | ||
| 294 | - end | ||
| 295 | - | ||
| 296 | def current_events(year, month, options={}) | 258 | def current_events(year, month, options={}) |
| 297 | options.delete(:page) | 259 | options.delete(:page) |
| 298 | options.delete(:per_page) | 260 | options.delete(:per_page) |
| @@ -302,6 +264,11 @@ class SearchController < PublicController | @@ -302,6 +264,11 @@ class SearchController < PublicController | ||
| 302 | Event.find(:all, {:include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }}.merge(options)) | 264 | Event.find(:all, {:include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }}.merge(options)) |
| 303 | end | 265 | end |
| 304 | 266 | ||
| 267 | + FILTERS = %w( | ||
| 268 | + more_recent | ||
| 269 | + more_active | ||
| 270 | + more_popular | ||
| 271 | + ) | ||
| 305 | def filter | 272 | def filter |
| 306 | if FILTERS.include?(params[:filter]) | 273 | if FILTERS.include?(params[:filter]) |
| 307 | params[:filter] | 274 | params[:filter] |
| @@ -323,26 +290,36 @@ class SearchController < PublicController | @@ -323,26 +290,36 @@ class SearchController < PublicController | ||
| 323 | }[str] || str | 290 | }[str] || str |
| 324 | end | 291 | end |
| 325 | 292 | ||
| 326 | - def load_search_assets | ||
| 327 | - @search_in = where_to_search | ||
| 328 | - @searching = {} | ||
| 329 | - @search_in.each do |key, name| | ||
| 330 | - @searching[key] = (params[:asset].blank? && (params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s))) || (params[:asset] == key.to_s) | 293 | + attr_reader :category |
| 294 | + attr_reader :category_id | ||
| 295 | + | ||
| 296 | + def load_category | ||
| 297 | + unless params[:category_path].blank? | ||
| 298 | + path = params[:category_path].join('/') | ||
| 299 | + @category = environment.categories.find_by_path(path) | ||
| 300 | + if @category.nil? | ||
| 301 | + render_not_found(path) | ||
| 302 | + end | ||
| 303 | + @category_id = @category.id | ||
| 331 | end | 304 | end |
| 332 | end | 305 | end |
| 333 | 306 | ||
| 334 | - def check_search_whole_site | ||
| 335 | - if params[:search_whole_site_yes] or params[:search_whole_site] == 'yes' | ||
| 336 | - redirect_to params.merge(:category_path => [], :search_whole_site => nil, :search_whole_site_yes => nil) | 307 | + def load_search_assets |
| 308 | + @enabled_searchs = [ | ||
| 309 | + [ :articles, N_('Articles') ], | ||
| 310 | + [ :enterprises, N_('Enterprises') ], | ||
| 311 | + [ :people, N_('People') ], | ||
| 312 | + [ :communities, N_('Communities') ], | ||
| 313 | + [ :products, N_('Products') ], | ||
| 314 | + [ :events, N_('Events') ] | ||
| 315 | + ].select {|key, name| !environment.enabled?('disable_asset_' + key.to_s) } | ||
| 316 | + | ||
| 317 | + @searching = {} | ||
| 318 | + @enabled_searchs.each do |key, name| | ||
| 319 | + @searching[key] = params[:action] == 'index' || params[:action] == key.to_s | ||
| 337 | end | 320 | end |
| 338 | end | 321 | end |
| 339 | 322 | ||
| 340 | - FILTERS = %w( | ||
| 341 | - more_recent | ||
| 342 | - more_active | ||
| 343 | - more_popular | ||
| 344 | - ) | ||
| 345 | - | ||
| 346 | def paginate_options(asset, limit, page) | 323 | def paginate_options(asset, limit, page) |
| 347 | result = { :per_page => limit, :page => page } | 324 | result = { :per_page => limit, :page => page } |
| 348 | end | 325 | end |
| @@ -367,7 +344,7 @@ class SearchController < PublicController | @@ -367,7 +344,7 @@ class SearchController < PublicController | ||
| 367 | if params[:display] == 'map' | 344 | if params[:display] == 'map' |
| 368 | MAP_SEARCH_LIMIT | 345 | MAP_SEARCH_LIMIT |
| 369 | else | 346 | else |
| 370 | - (searching.size == 1) ? SINGLE_SEARCH_LIMIT : MULTIPLE_SEARCH_LIMIT | 347 | + (searching.size <= 1) ? SINGLE_SEARCH_LIMIT : MULTIPLE_SEARCH_LIMIT |
| 371 | end | 348 | end |
| 372 | end | 349 | end |
| 373 | 350 |
app/models/environment_finder.rb
| @@ -1,117 +0,0 @@ | @@ -1,117 +0,0 @@ | ||
| 1 | -class EnvironmentFinder | ||
| 2 | - | ||
| 3 | - def initialize env | ||
| 4 | - @environment = env | ||
| 5 | - end | ||
| 6 | - | ||
| 7 | - def find(asset, query = nil, options={}, finder_method = 'paginate') | ||
| 8 | - @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) | ||
| 9 | - if @region && options[:within] | ||
| 10 | - options[:origin] = [@region.lat, @region.lng] | ||
| 11 | - else | ||
| 12 | - options.delete(:within) | ||
| 13 | - end | ||
| 14 | - | ||
| 15 | - product_category = options.delete(:product_category) | ||
| 16 | - | ||
| 17 | - date_range = options.delete(:date_range) | ||
| 18 | - | ||
| 19 | - # FIXME this test is in more than one place | ||
| 20 | - if finder_method == 'paginate' | ||
| 21 | - options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) | ||
| 22 | - end | ||
| 23 | - | ||
| 24 | - if query.blank? | ||
| 25 | - options.delete(:facets) | ||
| 26 | - options.delete(:order_by) | ||
| 27 | - | ||
| 28 | - # FIXME this test is in more than one place | ||
| 29 | - if finder_method == 'paginate' | ||
| 30 | - options = {:order => "#{asset_table(asset)}.name"}.merge(options) | ||
| 31 | - end | ||
| 32 | - if product_category && asset == :products | ||
| 33 | - @environment.send(asset).send(finder_method, :all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | ||
| 34 | - elsif product_category && asset == :enterprises | ||
| 35 | - @environment.send(asset).send(finder_method, :all, options.merge( :order => 'profiles.name', :joins => 'inner join products on (products.enterprise_id = profiles.id) inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) | ||
| 36 | - else | ||
| 37 | - if asset == :events | ||
| 38 | - # Ignore pagination for asset events | ||
| 39 | - options.delete(:per_page) | ||
| 40 | - options.delete(:page) | ||
| 41 | - if date_range | ||
| 42 | - @environment.send(asset).send('find', :all, options.merge(:conditions => [ | ||
| 43 | - 'start_date BETWEEN :start_day AND :end_day OR end_date BETWEEN :start_day AND :end_day', | ||
| 44 | - {:start_day => date_range.first, :end_day => date_range.last} | ||
| 45 | - ])) | ||
| 46 | - else | ||
| 47 | - @environment.send(asset).send('find', :all, options) | ||
| 48 | - end | ||
| 49 | - else | ||
| 50 | - @environment.send(asset).send(finder_method, :all, options) | ||
| 51 | - end | ||
| 52 | - end | ||
| 53 | - else | ||
| 54 | - pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} | ||
| 55 | - solr_options = {:facets => options.delete(:facets), :order => options.delete(:order_by)} | ||
| 56 | - | ||
| 57 | - if product_category && asset == :products | ||
| 58 | - # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source | ||
| 59 | - ret = @environment.send(asset).find_by_contents(query, pg_options, solr_options, options.merge({:include => 'product_categorizations', :conditions => 'product_categorizations.category_id = (%s)' % product_category.id })) | ||
| 60 | - elsif product_category && asset == :enterprises | ||
| 61 | - ret = @environment.send(asset).find_by_contents(query, pg_options, solr_options, options.merge(:joins => 'inner join product_categorizations on (product_categorizations.product_id = products.id)', :include => 'products', :conditions => "product_categorizations.category_id = (#{product_category.id})")) | ||
| 62 | - else | ||
| 63 | - ret = @environment.send(asset).find_by_contents(query, pg_options, solr_options, options) | ||
| 64 | - end | ||
| 65 | - if solr_options[:facets].nil? | ||
| 66 | - ret[:results] | ||
| 67 | - else | ||
| 68 | - ret | ||
| 69 | - end | ||
| 70 | - end | ||
| 71 | - end | ||
| 72 | - | ||
| 73 | - def recent(asset, limit = nil) | ||
| 74 | - find(asset, nil, :limit => limit) | ||
| 75 | - end | ||
| 76 | - | ||
| 77 | - def product_categories_count(asset, product_categories_ids, objects_ids=nil) | ||
| 78 | - conditions = ['product_categorizations.category_id in (?)', product_categories_ids] | ||
| 79 | - | ||
| 80 | - if asset == :products | ||
| 81 | - if objects_ids | ||
| 82 | - conditions[0] += ' and product_categorizations.product_id in (?)' | ||
| 83 | - conditions << objects_ids | ||
| 84 | - end | ||
| 85 | - ProductCategory.find(:all, :select => 'categories.id, count(*) as total', :joins => 'inner join product_categorizations on (product_categorizations.category_id = categories.id)', :group => 'categories.id', :conditions => conditions ) | ||
| 86 | - elsif asset == :enterprises | ||
| 87 | - if objects_ids | ||
| 88 | - conditions[0] += ' and products.enterprise_id in (?)' | ||
| 89 | - conditions << objects_ids | ||
| 90 | - end | ||
| 91 | - ProductCategory.find( | ||
| 92 | - :all, | ||
| 93 | - :select => 'categories.id, count(distinct products.enterprise_id) as total', | ||
| 94 | - :joins => 'inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id)', | ||
| 95 | - :group => 'categories.id', | ||
| 96 | - :conditions => conditions | ||
| 97 | - ) | ||
| 98 | - else | ||
| 99 | - raise ArgumentError, 'only products and enterprises supported' | ||
| 100 | - end.inject({}) do |results,pc| | ||
| 101 | - results[pc.id]= pc.total.to_i | ||
| 102 | - results | ||
| 103 | - end | ||
| 104 | - | ||
| 105 | - end | ||
| 106 | - | ||
| 107 | - protected | ||
| 108 | - | ||
| 109 | - def asset_class(asset) | ||
| 110 | - asset.to_s.singularize.camelize.constantize | ||
| 111 | - end | ||
| 112 | - | ||
| 113 | - def asset_table(asset) | ||
| 114 | - asset_class(asset).table_name | ||
| 115 | - end | ||
| 116 | - | ||
| 117 | -end |
app/views/search/popup.rhtml