annotate .svn/pristine/3f/3fecfd23ccdcab419a5392dfa395ed433f4bc031.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module Acts
Chris@1295 20 module Searchable
Chris@1295 21 def self.included(base)
Chris@1295 22 base.extend ClassMethods
Chris@1295 23 end
Chris@1295 24
Chris@1295 25 module ClassMethods
Chris@1295 26 # Options:
Chris@1295 27 # * :columns - a column or an array of columns to search
Chris@1295 28 # * :project_key - project foreign key (default to project_id)
Chris@1295 29 # * :date_column - name of the datetime column (default to created_on)
Chris@1295 30 # * :sort_order - name of the column used to sort results (default to :date_column or created_on)
Chris@1295 31 # * :permission - permission required to search the model (default to :view_"objects")
Chris@1295 32 def acts_as_searchable(options = {})
Chris@1295 33 return if self.included_modules.include?(Redmine::Acts::Searchable::InstanceMethods)
Chris@1295 34
Chris@1295 35 cattr_accessor :searchable_options
Chris@1295 36 self.searchable_options = options
Chris@1295 37
Chris@1295 38 if searchable_options[:columns].nil?
Chris@1295 39 raise 'No searchable column defined.'
Chris@1295 40 elsif !searchable_options[:columns].is_a?(Array)
Chris@1295 41 searchable_options[:columns] = [] << searchable_options[:columns]
Chris@1295 42 end
Chris@1295 43
Chris@1295 44 searchable_options[:project_key] ||= "#{table_name}.project_id"
Chris@1295 45 searchable_options[:date_column] ||= "#{table_name}.created_on"
Chris@1295 46 searchable_options[:order_column] ||= searchable_options[:date_column]
Chris@1295 47
Chris@1295 48 # Should we search custom fields on this model ?
Chris@1295 49 searchable_options[:search_custom_fields] = !reflect_on_association(:custom_values).nil?
Chris@1295 50
Chris@1295 51 send :include, Redmine::Acts::Searchable::InstanceMethods
Chris@1295 52 end
Chris@1295 53 end
Chris@1295 54
Chris@1295 55 module InstanceMethods
Chris@1295 56 def self.included(base)
Chris@1295 57 base.extend ClassMethods
Chris@1295 58 end
Chris@1295 59
Chris@1295 60 module ClassMethods
Chris@1295 61 # Searches the model for the given tokens
Chris@1295 62 # projects argument can be either nil (will search all projects), a project or an array of projects
Chris@1295 63 # Returns the results and the results count
Chris@1295 64 def search(tokens, projects=nil, options={})
Chris@1295 65 if projects.is_a?(Array) && projects.empty?
Chris@1295 66 # no results
Chris@1295 67 return [[], 0]
Chris@1295 68 end
Chris@1295 69
Chris@1295 70 # TODO: make user an argument
Chris@1295 71 user = User.current
Chris@1295 72 tokens = [] << tokens unless tokens.is_a?(Array)
Chris@1295 73 projects = [] << projects unless projects.nil? || projects.is_a?(Array)
Chris@1295 74
Chris@1295 75 find_options = {:include => searchable_options[:include]}
Chris@1295 76 find_options[:order] = "#{searchable_options[:order_column]} " + (options[:before] ? 'DESC' : 'ASC')
Chris@1295 77
Chris@1295 78 limit_options = {}
Chris@1295 79 limit_options[:limit] = options[:limit] if options[:limit]
Chris@1295 80 if options[:offset]
Chris@1295 81 limit_options[:conditions] = "(#{searchable_options[:date_column]} " + (options[:before] ? '<' : '>') + "'#{connection.quoted_date(options[:offset])}')"
Chris@1295 82 end
Chris@1295 83
Chris@1295 84 columns = searchable_options[:columns]
Chris@1295 85 columns = columns[0..0] if options[:titles_only]
Chris@1295 86
Chris@1295 87 token_clauses = columns.collect {|column| "(LOWER(#{column}) LIKE ?)"}
Chris@1295 88
Chris@1295 89 if !options[:titles_only] && searchable_options[:search_custom_fields]
Chris@1295 90 searchable_custom_field_ids = CustomField.find(:all,
Chris@1295 91 :select => 'id',
Chris@1295 92 :conditions => { :type => "#{self.name}CustomField",
Chris@1295 93 :searchable => true }).collect(&:id)
Chris@1295 94 if searchable_custom_field_ids.any?
Chris@1295 95 custom_field_sql = "#{table_name}.id IN (SELECT customized_id FROM #{CustomValue.table_name}" +
Chris@1295 96 " WHERE customized_type='#{self.name}' AND customized_id=#{table_name}.id AND LOWER(value) LIKE ?" +
Chris@1295 97 " AND #{CustomValue.table_name}.custom_field_id IN (#{searchable_custom_field_ids.join(',')}))"
Chris@1295 98 token_clauses << custom_field_sql
Chris@1295 99 end
Chris@1295 100 end
Chris@1295 101
Chris@1295 102 sql = (['(' + token_clauses.join(' OR ') + ')'] * tokens.size).join(options[:all_words] ? ' AND ' : ' OR ')
Chris@1295 103
Chris@1295 104 find_options[:conditions] = [sql, * (tokens.collect {|w| "%#{w.downcase}%"} * token_clauses.size).sort]
Chris@1295 105
Chris@1295 106 scope = self
Chris@1295 107 project_conditions = []
Chris@1295 108 if searchable_options.has_key?(:permission)
Chris@1295 109 project_conditions << Project.allowed_to_condition(user, searchable_options[:permission] || :view_project)
Chris@1295 110 elsif respond_to?(:visible)
Chris@1295 111 scope = scope.visible(user)
Chris@1295 112 else
Chris@1295 113 ActiveSupport::Deprecation.warn "acts_as_searchable with implicit :permission option is deprecated. Add a visible scope to the #{self.name} model or use explicit :permission option."
Chris@1295 114 project_conditions << Project.allowed_to_condition(user, "view_#{self.name.underscore.pluralize}".to_sym)
Chris@1295 115 end
Chris@1295 116 # TODO: use visible scope options instead
Chris@1295 117 project_conditions << "#{searchable_options[:project_key]} IN (#{projects.collect(&:id).join(',')})" unless projects.nil?
Chris@1295 118 project_conditions = project_conditions.empty? ? nil : project_conditions.join(' AND ')
Chris@1295 119
Chris@1295 120 results = []
Chris@1295 121 results_count = 0
Chris@1295 122
Chris@1295 123 scope = scope.scoped({:conditions => project_conditions}).scoped(find_options)
Chris@1295 124 results_count = scope.count(:all)
Chris@1295 125 results = scope.find(:all, limit_options)
Chris@1295 126
Chris@1295 127 [results, results_count]
Chris@1295 128 end
Chris@1295 129 end
Chris@1295 130 end
Chris@1295 131 end
Chris@1295 132 end
Chris@1295 133 end