From 5a8294ce60700a0fea36f07c492da3f28cb1c0ed Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Wed, 6 Jul 2011 12:52:07 +0000 Subject: [PATCH] Removed environment_finder and search popup --- app/controllers/public/search_controller.rb | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------- app/models/environment_finder.rb | 117 --------------------------------------------------------------------------------------------------------------------- app/views/search/popup.rhtml | 9 --------- 3 files changed, 57 insertions(+), 206 deletions(-) delete mode 100644 app/models/environment_finder.rb delete mode 100644 app/views/search/popup.rhtml diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index eeefc0a..95dbb74 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -8,7 +8,6 @@ class SearchController < PublicController include SearchHelper before_filter :load_category - before_filter :check_search_whole_site before_filter :load_search_assets no_design_blocks @@ -159,7 +158,7 @@ class SearchController < PublicController @order = [] @names = {} - where_to_search.select { |key,description| @searching[key] }.each do |key, description| + @enabled_searchs.select { |key,description| @searching[key] }.each do |key, description| send(key) @order << key @names[key] = getterm(description) @@ -182,7 +181,6 @@ class SearchController < PublicController # view the summary of one category def category_index - @searching @results = {} @order = [] @names = {} @@ -203,13 +201,37 @@ class SearchController < PublicController @facets = {} end + def tags + @tags_cache_key = "tags_env_#{environment.id.to_s}" + if is_cache_expired?(@tags_cache_key, true) + @tags = environment.tag_counts + end + end + + def tag + @tag = params[:tag] + @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_env_#{environment.id.to_s}_page_#{params[:npage]}" + if is_cache_expired?(@tag_cache_key, true) + @tagged = environment.articles.find_tagged_with(@tag).paginate(:per_page => 10, :page => params[:npage]) + end + end + + def events_by_day + @selected_day = build_date(params[:year], params[:month], params[:day]) + if params[:category_id] and Category.exists?(params[:category_id]) + @events_of_the_day = environment.events.by_day(@selected_day).in_category(Category.find(params[:category_id])) + else + @events_of_the_day = environment.events.by_day(@selected_day) + end + render :partial => 'events/events_by_day' + end + + ####################################################### protected def recent(asset, limit = nil) options = {:page => 1, :per_page => limit, :order => 'created_at DESC, id DESC'} - @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) - if asset == :events finder_method = 'find' options.delete(:page) @@ -233,66 +255,6 @@ class SearchController < PublicController Event.find(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options)) end - - attr_reader :category - attr_reader :category_id - - def load_category - unless params[:category_path].blank? - path = params[:category_path].join('/') - @category = environment.categories.find_by_path(path) - if @category.nil? - render_not_found(path) - end - @category_id = @category.id - end - end - - def where_to_search - [ - [ :articles, N_('Articles') ], - [ :enterprises, N_('Enterprises') ], - [ :people, N_('People') ], - [ :communities, N_('Communities') ], - [ :products, N_('Products') ], - [ :events, N_('Events') ] - ].select {|key, name| !environment.enabled?('disable_asset_' + key.to_s) } - end - - ####################################################### - - def tags - @tags_cache_key = "tags_env_#{environment.id.to_s}" - if is_cache_expired?(@tags_cache_key, true) - @tags = environment.tag_counts - end - end - - def tag - @tag = params[:tag] - @tag_cache_key = "tag_#{CGI.escape(@tag.to_s)}_env_#{environment.id.to_s}_page_#{params[:npage]}" - if is_cache_expired?(@tag_cache_key, true) - @tagged = environment.articles.find_tagged_with(@tag).paginate(:per_page => 10, :page => params[:npage]) - end - end - - ####################################################### - - def popup - @regions = Region.find(:all).select{|r|r.lat && r.lng} - render :action => 'popup', :layout => false - end - - def events_by_day - @selected_day = build_date(params[:year], params[:month], params[:day]) - if params[:category_id] and Category.exists?(params[:category_id]) - @events_of_the_day = environment.events.by_day(@selected_day).in_category(Category.find(params[:category_id])) - else - @events_of_the_day = environment.events.by_day(@selected_day) - end - render :partial => 'events/events_by_day' - end - def current_events(year, month, options={}) options.delete(:page) options.delete(:per_page) @@ -302,6 +264,11 @@ class SearchController < PublicController Event.find(:all, {:include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }}.merge(options)) end + FILTERS = %w( + more_recent + more_active + more_popular + ) def filter if FILTERS.include?(params[:filter]) params[:filter] @@ -323,26 +290,36 @@ class SearchController < PublicController }[str] || str end - def load_search_assets - @search_in = where_to_search - @searching = {} - @search_in.each do |key, name| - @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) + attr_reader :category + attr_reader :category_id + + def load_category + unless params[:category_path].blank? + path = params[:category_path].join('/') + @category = environment.categories.find_by_path(path) + if @category.nil? + render_not_found(path) + end + @category_id = @category.id end end - def check_search_whole_site - if params[:search_whole_site_yes] or params[:search_whole_site] == 'yes' - redirect_to params.merge(:category_path => [], :search_whole_site => nil, :search_whole_site_yes => nil) + def load_search_assets + @enabled_searchs = [ + [ :articles, N_('Articles') ], + [ :enterprises, N_('Enterprises') ], + [ :people, N_('People') ], + [ :communities, N_('Communities') ], + [ :products, N_('Products') ], + [ :events, N_('Events') ] + ].select {|key, name| !environment.enabled?('disable_asset_' + key.to_s) } + + @searching = {} + @enabled_searchs.each do |key, name| + @searching[key] = params[:action] == 'index' || params[:action] == key.to_s end end - FILTERS = %w( - more_recent - more_active - more_popular - ) - def paginate_options(asset, limit, page) result = { :per_page => limit, :page => page } end @@ -367,7 +344,7 @@ class SearchController < PublicController if params[:display] == 'map' MAP_SEARCH_LIMIT else - (searching.size == 1) ? SINGLE_SEARCH_LIMIT : MULTIPLE_SEARCH_LIMIT + (searching.size <= 1) ? SINGLE_SEARCH_LIMIT : MULTIPLE_SEARCH_LIMIT end end diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb deleted file mode 100644 index 92ff518..0000000 --- a/app/models/environment_finder.rb +++ /dev/null @@ -1,117 +0,0 @@ -class EnvironmentFinder - - def initialize env - @environment = env - end - - def find(asset, query = nil, options={}, finder_method = 'paginate') - @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) - if @region && options[:within] - options[:origin] = [@region.lat, @region.lng] - else - options.delete(:within) - end - - product_category = options.delete(:product_category) - - date_range = options.delete(:date_range) - - # FIXME this test is in more than one place - if finder_method == 'paginate' - options = {:page => 1, :per_page => options.delete(:limit)}.merge(options) - end - - if query.blank? - options.delete(:facets) - options.delete(:order_by) - - # FIXME this test is in more than one place - if finder_method == 'paginate' - options = {:order => "#{asset_table(asset)}.name"}.merge(options) - end - if product_category && asset == :products - @environment.send(asset).send(finder_method, :all, options.merge(:include => 'product_categorizations', :conditions => ['product_categorizations.category_id = (?)', product_category.id])) - elsif product_category && asset == :enterprises - @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])) - else - if asset == :events - # Ignore pagination for asset events - options.delete(:per_page) - options.delete(:page) - if date_range - @environment.send(asset).send('find', :all, options.merge(:conditions => [ - 'start_date BETWEEN :start_day AND :end_day OR end_date BETWEEN :start_day AND :end_day', - {:start_day => date_range.first, :end_day => date_range.last} - ])) - else - @environment.send(asset).send('find', :all, options) - end - else - @environment.send(asset).send(finder_method, :all, options) - end - end - else - pg_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} - solr_options = {:facets => options.delete(:facets), :order => options.delete(:order_by)} - - if product_category && asset == :products - # SECURITY no risk of SQL injection, since product_category_ids comes from trusted source - 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 })) - elsif product_category && asset == :enterprises - 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})")) - else - ret = @environment.send(asset).find_by_contents(query, pg_options, solr_options, options) - end - if solr_options[:facets].nil? - ret[:results] - else - ret - end - end - end - - def recent(asset, limit = nil) - find(asset, nil, :limit => limit) - end - - def product_categories_count(asset, product_categories_ids, objects_ids=nil) - conditions = ['product_categorizations.category_id in (?)', product_categories_ids] - - if asset == :products - if objects_ids - conditions[0] += ' and product_categorizations.product_id in (?)' - conditions << objects_ids - end - 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 ) - elsif asset == :enterprises - if objects_ids - conditions[0] += ' and products.enterprise_id in (?)' - conditions << objects_ids - end - ProductCategory.find( - :all, - :select => 'categories.id, count(distinct products.enterprise_id) as total', - :joins => 'inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id)', - :group => 'categories.id', - :conditions => conditions - ) - else - raise ArgumentError, 'only products and enterprises supported' - end.inject({}) do |results,pc| - results[pc.id]= pc.total.to_i - results - end - - end - - protected - - def asset_class(asset) - asset.to_s.singularize.camelize.constantize - end - - def asset_table(asset) - asset_class(asset).table_name - end - -end diff --git a/app/views/search/popup.rhtml b/app/views/search/popup.rhtml deleted file mode 100644 index a221dca..0000000 --- a/app/views/search/popup.rhtml +++ /dev/null @@ -1,9 +0,0 @@ -
- -<%= render :partial => 'search_form', :locals => { :form_title => _('Search %s') % @environment.name } %> - - - -
-- libgit2 0.21.2