diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-07-20 17:26:07 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2008-07-20 17:26:07 +0000 |
commit | be2b8a62f4d0d8bf413a5583fe644bd41a8ebf04 (patch) | |
tree | 95fe7f273935739e1ffa82be16cee93927a4c054 /vendor/plugins | |
parent | 83baccb71ac0e609ce72ebf322f73884548a9ba5 (diff) | |
download | redmine-be2b8a62f4d0d8bf413a5583fe644bd41a8ebf04.tar.gz redmine-be2b8a62f4d0d8bf413a5583fe644bd41a8ebf04.zip |
Search engine: display total results count (#906) and count by result type.
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1681 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'vendor/plugins')
-rw-r--r-- | vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb b/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb index 2c773d70a..fec933352 100644 --- a/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb +++ b/vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb @@ -23,6 +23,12 @@ module Redmine end module ClassMethods + # Options: + # * :columns - a column or an array of columns to search + # * :project_key - project foreign key (default to project_id) + # * :date_column - name of the datetime column (default to created_on) + # * :sort_order - name of the column used to sort results (default to :date_column or created_on) + # * :permission - permission required to search the model (default to :view_"objects") def acts_as_searchable(options = {}) return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods) @@ -49,6 +55,8 @@ module Redmine raise 'No date column defined defined.' end + searchable_options[:order_column] ||= searchable_options[:date_column] + # Permission needed to search this model searchable_options[:permission] = "view_#{self.name.underscore.pluralize}".to_sym unless searchable_options.has_key?(:permission) @@ -65,15 +73,22 @@ module Redmine end module ClassMethods - # Search the model for the given tokens + # Searches the model for the given tokens # projects argument can be either nil (will search all projects), a project or an array of projects + # Returns the results and the results count def search(tokens, projects=nil, options={}) tokens = [] << tokens unless tokens.is_a?(Array) projects = [] << projects unless projects.nil? || projects.is_a?(Array) find_options = {:include => searchable_options[:include]} - find_options[:limit] = options[:limit] if options[:limit] - find_options[:order] = "#{searchable_options[:date_column]} " + (options[:before] ? 'DESC' : 'ASC') + find_options[:order] = "#{searchable_options[:order_column]} " + (options[:before] ? 'DESC' : 'ASC') + + limit_options = {} + limit_options[:limit] = options[:limit] if options[:limit] + if options[:offset] + limit_options[:conditions] = "(#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')" + end + columns = searchable_options[:columns] columns.slice!(1..-1) if options[:titles_only] @@ -94,9 +109,6 @@ module Redmine sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ') - if options[:offset] - sql = "(#{sql}) AND (#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')" - end find_options[:conditions] = [sql, * (tokens * token_clauses.size).sort] project_conditions = [] @@ -104,16 +116,16 @@ module Redmine Project.allowed_to_condition(User.current, searchable_options[:permission])) project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil? - results = with_scope(:find => {:conditions => project_conditions.join(' AND ')}) do - find(:all, find_options) - end - if searchable_options[:with] && !options[:titles_only] - searchable_options[:with].each do |model, assoc| - results += model.to_s.camelcase.constantize.search(tokens, projects, options).collect {|r| r.send assoc} + results = [] + results_count = 0 + + with_scope(:find => {:conditions => project_conditions.join(' AND ')}) do + with_scope(:find => find_options) do + results_count = count(:all) + results = find(:all, limit_options) end - results.uniq! end - results + [results, results_count] end end end |