annotate .svn/pristine/dd/ddbb3ce710b464f41a52d6045553bac554f74a5e.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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