annotate .svn/pristine/80/80976803aa4ae03da071ef1a25ad25828bf8c96b.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 dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 class Principal < ActiveRecord::Base
Chris@1517 19 self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
Chris@1517 20
Chris@1517 21 # Account statuses
Chris@1517 22 STATUS_ANONYMOUS = 0
Chris@1517 23 STATUS_ACTIVE = 1
Chris@1517 24 STATUS_REGISTERED = 2
Chris@1517 25 STATUS_LOCKED = 3
Chris@1517 26
Chris@1517 27 has_many :members, :foreign_key => 'user_id', :dependent => :destroy
Chris@1517 28 has_many :memberships, :class_name => 'Member',
Chris@1517 29 :foreign_key => 'user_id',
Chris@1517 30 :include => [:project, :roles],
Chris@1517 31 :conditions => "#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}",
Chris@1517 32 :order => "#{Project.table_name}.name"
Chris@1517 33 has_many :projects, :through => :memberships
Chris@1517 34 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
Chris@1517 35
Chris@1517 36 # Groups and active users
Chris@1517 37 scope :active, lambda { where(:status => STATUS_ACTIVE) }
Chris@1517 38
Chris@1517 39 scope :like, lambda {|q|
Chris@1517 40 q = q.to_s
Chris@1517 41 if q.blank?
Chris@1517 42 where({})
Chris@1517 43 else
Chris@1517 44 pattern = "%#{q}%"
Chris@1517 45 sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ")
Chris@1517 46 params = {:p => pattern}
Chris@1517 47 if q =~ /^(.+)\s+(.+)$/
Chris@1517 48 a, b = "#{$1}%", "#{$2}%"
Chris@1517 49 sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))"
Chris@1517 50 sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))"
Chris@1517 51 params.merge!(:a => a, :b => b)
Chris@1517 52 end
Chris@1517 53 where(sql, params)
Chris@1517 54 end
Chris@1517 55 }
Chris@1517 56
Chris@1517 57 # Principals that are members of a collection of projects
Chris@1517 58 scope :member_of, lambda {|projects|
Chris@1517 59 projects = [projects] unless projects.is_a?(Array)
Chris@1517 60 if projects.empty?
Chris@1517 61 where("1=0")
Chris@1517 62 else
Chris@1517 63 ids = projects.map(&:id)
Chris@1517 64 active.uniq.joins(:members).where("#{Member.table_name}.project_id IN (?)", ids)
Chris@1517 65 end
Chris@1517 66 }
Chris@1517 67 # Principals that are not members of projects
Chris@1517 68 scope :not_member_of, lambda {|projects|
Chris@1517 69 projects = [projects] unless projects.is_a?(Array)
Chris@1517 70 if projects.empty?
Chris@1517 71 where("1=0")
Chris@1517 72 else
Chris@1517 73 ids = projects.map(&:id)
Chris@1517 74 where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
Chris@1517 75 end
Chris@1517 76 }
Chris@1517 77 scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
Chris@1517 78
Chris@1517 79 before_create :set_default_empty_values
Chris@1517 80
Chris@1517 81 def name(formatter = nil)
Chris@1517 82 to_s
Chris@1517 83 end
Chris@1517 84
Chris@1517 85 def <=>(principal)
Chris@1517 86 if principal.nil?
Chris@1517 87 -1
Chris@1517 88 elsif self.class.name == principal.class.name
Chris@1517 89 self.to_s.downcase <=> principal.to_s.downcase
Chris@1517 90 else
Chris@1517 91 # groups after users
Chris@1517 92 principal.class.name <=> self.class.name
Chris@1517 93 end
Chris@1517 94 end
Chris@1517 95
Chris@1517 96 # Returns an array of fields names than can be used to make an order statement for principals.
Chris@1517 97 # Users are sorted before Groups.
Chris@1517 98 # Examples:
Chris@1517 99 def self.fields_for_order_statement(table=nil)
Chris@1517 100 table ||= table_name
Chris@1517 101 columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
Chris@1517 102 columns.uniq.map {|field| "#{table}.#{field}"}
Chris@1517 103 end
Chris@1517 104
Chris@1517 105 protected
Chris@1517 106
Chris@1517 107 # Make sure we don't try to insert NULL values (see #4632)
Chris@1517 108 def set_default_empty_values
Chris@1517 109 self.login ||= ''
Chris@1517 110 self.hashed_password ||= ''
Chris@1517 111 self.firstname ||= ''
Chris@1517 112 self.lastname ||= ''
Chris@1517 113 self.mail ||= ''
Chris@1517 114 true
Chris@1517 115 end
Chris@1517 116 end