From b9a0b4d3e38dc31550135c3b26023f5d7d9fd855 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 28 Feb 2013 21:59:36 +0000 Subject: [PATCH] Preparing search filter scheme --- app/controllers/public/search_controller.rb | 46 ++++++++++++---------------------------------- app/helpers/search_helper.rb | 22 ++++++++++++++++++++-- app/models/article.rb | 18 ++++++++---------- app/models/organization.rb | 5 +++++ app/models/person.rb | 5 +++++ app/models/product.rb | 4 ++++ app/models/profile.rb | 4 ++++ test/unit/article_test.rb | 10 +++++----- 8 files changed, 63 insertions(+), 51 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index e8e3f2e..abd216b 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -8,6 +8,7 @@ class SearchController < PublicController before_filter :load_category before_filter :load_search_assets before_filter :load_query + before_filter :load_filter # Backwards compatibility with old URLs def redirect_asset_param @@ -145,8 +146,6 @@ class SearchController < PublicController @order ||= [@asset] params[:display] ||= 'list' @searches ||= {} - @filter = filter - @filter_title = filter_description(@asset, @filter) @query = params[:query] || '' @empty_query = @category.nil? && @query.blank? @@ -166,42 +165,13 @@ class SearchController < PublicController end end - FILTERS = %w( - more_recent - more_active - more_popular - more_comments - ) - def filter - if FILTERS.include?(params[:filter]) - params[:filter] - else - 'more_recent' - end - end - - def filter_description(asset, filter) - { - 'articles_more_recent' => _('More recent contents from network'), - 'articles_more_popular' => _('More viewed contents from network'), - 'articles_more_comments' => _('Most commented contents from network'), - 'people_more_recent' => _('More recent people from network'), - 'people_more_active' => _('More active people from network'), - 'people_more_popular' => _('More popular people from network'), - 'communities_more_recent' => _('More recent communities from network'), - 'communities_more_active' => _('More active communities from network'), - 'communities_more_popular' => _('More popular communities from network'), - 'products_more_recent' => _('Highlights'), - }[asset.to_s + '_' + filter] - end - def load_search_assets - if Searches.keys.include?(params[:action].to_sym) and environment.enabled?("disable_asset_#{params[:action]}") + if SEARCHES.keys.include?(params[:action].to_sym) && environment.enabled?("disable_asset_#{params[:action]}") render_not_found return end - @enabled_searches = Searches.select {|key, name| environment.disabled?("disable_asset_#{params[:action]}") } + @enabled_searches = SEARCHES.select {|key, name| environment.disabled?("disable_asset_#{params[:action]}") } @searching = {} @titles = {} @enabled_searches.each do |key, name| @@ -211,6 +181,14 @@ class SearchController < PublicController @names = @titles if @names.nil? end + def load_filter + @filter = 'more_recent' + if SEARCHES.keys.include?(@asset.to_sym) + available_filters = asset_class(@asset)::SEARCH_FILTERS + @filter = params[:filter] if available_filters.include?(params[:filter]) + end + end + def limit if map_search? MAP_SEARCH_LIMIT @@ -226,7 +204,7 @@ class SearchController < PublicController end def paginate_options(page = params[:page]) - page = 1 if multiple_search? or @display == 'map' + page = 1 if multiple_search? || params[:display] == 'map' { :per_page => limit, :page => page } end diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index 2d51f78..27e1d30 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -5,7 +5,7 @@ module SearchHelper BLOCKS_SEARCH_LIMIT = 24 MULTIPLE_SEARCH_LIMIT = 8 - Searches = ActiveSupport::OrderedHash[ + SEARCHES = ActiveSupport::OrderedHash[ :articles, _('Contents'), :enterprises, _('Enterprises'), :people, _('People'), @@ -22,7 +22,7 @@ module SearchHelper end def map_search? - !multiple_search? and params[:display] == 'map' + !multiple_search? && params[:display] == 'map' end def asset_class(asset) @@ -86,4 +86,22 @@ module SearchHelper end end + def filter_title(asset, filter) + { + 'articles_more_recent' => _('More recent contents from network'), + 'articles_more_popular' => _('More viewed contents from network'), + 'articles_more_comments' => _('Most commented contents from network'), + 'people_more_recent' => _('More recent people from network'), + 'people_more_active' => _('More active people from network'), + 'people_more_popular' => _('More popular people from network'), + 'communities_more_recent' => _('More recent communities from network'), + 'communities_more_active' => _('More active communities from network'), + 'communities_more_popular' => _('More popular communities from network'), + 'enterprises_more_recent' => _('More recent enterprises from network'), + 'enterprises_more_active' => _('More active enterprises from network'), + 'enterprises_more_popular' => _('More popular enterprises from network'), + 'products_more_recent' => _('Highlights'), + }[asset.to_s + '_' + filter].to_s + end + end diff --git a/app/models/article.rb b/app/models/article.rb index c5094d0..ed3b248 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -10,6 +10,12 @@ class Article < ActiveRecord::Base :filename => 1, } + SEARCH_FILTERS = %w[ + more_recent + more_popular + more_comments + ] + track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? } # xss_terminate plugin can't sanitize array fields @@ -198,20 +204,12 @@ class Article < ActiveRecord::Base named_scope :public, :conditions => [ "advertise = ? AND published = ? AND profiles.visible = ? AND profiles.public_profile = ?", true, true, true, true ] - named_scope :more_recent, - :conditions => [ "advertise = ? AND published = ? AND profiles.visible = ? AND profiles.public_profile = ? AND - ((articles.type != ?) OR articles.type is NULL)", - true, true, true, true, 'RssFeed' - ], - :order => 'articles.published_at desc, articles.id desc' - # retrives the most commented articles, sorted by the comment count (largest # first) def self.most_commented(limit) paginate(:order => 'comments_count DESC', :page => 1, :per_page => limit) end - named_scope :more_popular, :order => 'hits DESC' named_scope :relevant_as_recent, :conditions => ["(articles.type != 'UploadedFile' and articles.type != 'RssFeed' and articles.type != 'Blog') OR articles.type is NULL"] def self.recent(limit = nil, extra_conditions = {}, pagination = true) @@ -418,8 +416,8 @@ class Article < ActiveRecord::Base named_scope :images, :conditions => { :is_image => true } named_scope :text_articles, :conditions => [ 'articles.type IN (?)', text_article_types ] + named_scope :more_popular, :order => 'hits DESC' named_scope :more_comments, :order => "comments_count DESC" - named_scope :more_views, :order => "hits DESC" named_scope :more_recent, :order => "created_at DESC" def self.display_filter(user, profile) @@ -608,7 +606,7 @@ class Article < ActiveRecord::Base end - def more_views_label + def more_popular_label amount = self.hits { 0 => _('no views'), diff --git a/app/models/organization.rb b/app/models/organization.rb index 2282071..b00f295 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -1,6 +1,11 @@ # Represents any organization of the system class Organization < Profile + SEARCH_FILTERS += %w[ + more_popular + more_active + ] + settings_items :closed, :type => :boolean, :default => false def closed? closed diff --git a/app/models/person.rb b/app/models/person.rb index e0808c7..aad72bc 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,6 +1,11 @@ # A person is the profile of an user holding all relationships with the rest of the system class Person < Profile + SEARCH_FILTERS += %w[ + more_popular + more_active + ] + def self.type_name _('Person') end diff --git a/app/models/product.rb b/app/models/product.rb index 2fc76f9..343e2c1 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -5,6 +5,10 @@ class Product < ActiveRecord::Base :description => 1, } + SEARCH_FILTERS = %w[ + more_recent + ] + belongs_to :enterprise has_one :region, :through => :enterprise validates_presence_of :enterprise diff --git a/app/models/profile.rb b/app/models/profile.rb index 5fc1a3e..278fd37 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -9,6 +9,10 @@ class Profile < ActiveRecord::Base :nickname => 2, } + SEARCH_FILTERS = %w[ + more_recent + ] + module Roles def self.admin(env_id) find_role('admin', env_id) diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index d8e7845..8eb90a7 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1480,8 +1480,8 @@ class ArticleTest < ActiveSupport::TestCase assert_respond_to Article, :more_comments end - should 'respond to more views' do - assert_respond_to Article, :more_views + should 'respond to more popular' do + assert_respond_to Article, :more_popular end should "return the more recent label" do @@ -1510,19 +1510,19 @@ class ArticleTest < ActiveSupport::TestCase should "return no views if profile has 0 views" do a = Article.new assert_equal 0, a.hits - assert_equal "no views", a.more_views_label + assert_equal "no views", a.more_popular_label end should "return 1 view on label if the content has 1 view" do a = Article.new(:hits => 1) assert_equal 1, a.hits - assert_equal "one view", a.more_views_label + assert_equal "one view", a.more_popular_label end should "return number of views on label if the content has more than one view" do a = Article.new(:hits => 4) assert_equal 4, a.hits - assert_equal "4 views", a.more_views_label + assert_equal "4 views", a.more_popular_label end should 'return only text articles' do -- libgit2 0.21.2