annotate app/models/principal.rb @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 433d4f72a19b
children 622f24f53b42
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class Principal < ActiveRecord::Base
Chris@1115 19 self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
Chris@0 20
Chris@0 21 has_many :members, :foreign_key => 'user_id', :dependent => :destroy
Chris@1115 22 has_many :memberships, :class_name => 'Member', :foreign_key => 'user_id', :include => [ :project, :roles ], :conditions => "#{Project.table_name}.status<>#{Project::STATUS_ARCHIVED}", :order => "#{Project.table_name}.name"
Chris@0 23 has_many :projects, :through => :memberships
Chris@909 24 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
Chris@0 25
Chris@0 26 # Groups and active users
Chris@1115 27 scope :active, :conditions => "#{Principal.table_name}.status = 1"
Chris@909 28
Chris@1115 29 scope :like, lambda {|q|
Chris@1115 30 q = q.to_s
Chris@1115 31 if q.blank?
Chris@1115 32 where({})
Chris@1115 33 else
Chris@1115 34 pattern = "%#{q}%"
Chris@1115 35 sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ")
Chris@1115 36 params = {:p => pattern}
Chris@1115 37 if q =~ /^(.+)\s+(.+)$/
Chris@1115 38 a, b = "#{$1}%", "#{$2}%"
Chris@1115 39 sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))"
Chris@1115 40 sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))"
Chris@1115 41 params.merge!(:a => a, :b => b)
Chris@1115 42 end
Chris@1115 43 where(sql, params)
Chris@1115 44 end
Chris@1115 45 }
Chris@1115 46
Chris@1115 47 # Principals that are members of a collection of projects
Chris@1115 48 scope :member_of, lambda {|projects|
Chris@1115 49 projects = [projects] unless projects.is_a?(Array)
Chris@1115 50 if projects.empty?
Chris@1115 51 where("1=0")
Chris@1115 52 else
Chris@1115 53 ids = projects.map(&:id)
Chris@1115 54 where("#{Principal.table_name}.status = 1 AND #{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
Chris@1115 55 end
Chris@0 56 }
Chris@929 57 # Principals that are not members of projects
Chris@1115 58 scope :not_member_of, lambda {|projects|
Chris@929 59 projects = [projects] unless projects.is_a?(Array)
Chris@929 60 if projects.empty?
Chris@1115 61 where("1=0")
Chris@929 62 else
Chris@929 63 ids = projects.map(&:id)
Chris@1115 64 where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
Chris@929 65 end
Chris@929 66 }
Chris@909 67
Chris@0 68 before_create :set_default_empty_values
chris@22 69
chris@22 70 def name(formatter = nil)
chris@22 71 to_s
chris@22 72 end
chris@22 73
Chris@0 74 def <=>(principal)
Chris@929 75 if principal.nil?
Chris@929 76 -1
Chris@929 77 elsif self.class.name == principal.class.name
Chris@0 78 self.to_s.downcase <=> principal.to_s.downcase
Chris@0 79 else
Chris@0 80 # groups after users
Chris@0 81 principal.class.name <=> self.class.name
Chris@0 82 end
Chris@0 83 end
Chris@909 84
Chris@0 85 protected
Chris@909 86
Chris@0 87 # Make sure we don't try to insert NULL values (see #4632)
Chris@0 88 def set_default_empty_values
Chris@0 89 self.login ||= ''
Chris@0 90 self.hashed_password ||= ''
Chris@0 91 self.firstname ||= ''
Chris@0 92 self.lastname ||= ''
Chris@0 93 self.mail ||= ''
Chris@0 94 true
Chris@0 95 end
Chris@0 96 end