annotate app/models/principal.rb @ 929:5f33065ddc4b redmine-1.3

Update to Redmine SVN rev 9414 on 1.3-stable branch
author Chris Cannam
date Wed, 27 Jun 2012 14:54:18 +0100
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@0 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 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@0 19 set_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@0 22 has_many :memberships, :class_name => 'Member', :foreign_key => 'user_id', :include => [ :project, :roles ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :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@0 27 named_scope :active, :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status = 1)"
Chris@909 28
Chris@909 29 named_scope :like, lambda {|q|
Chris@0 30 s = "%#{q.to_s.strip.downcase}%"
Chris@0 31 {:conditions => ["LOWER(login) LIKE :s OR LOWER(firstname) LIKE :s OR LOWER(lastname) LIKE :s OR LOWER(mail) LIKE :s", {:s => s}],
Chris@0 32 :order => 'type, login, lastname, firstname, mail'
Chris@0 33 }
Chris@0 34 }
Chris@929 35 # Principals that are not members of projects
Chris@929 36 named_scope :not_member_of, lambda {|projects|
Chris@929 37 projects = [projects] unless projects.is_a?(Array)
Chris@929 38 if projects.empty?
Chris@929 39 {:conditions => "1=0"}
Chris@929 40 else
Chris@929 41 ids = projects.map(&:id)
Chris@929 42 {:conditions => ["#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]}
Chris@929 43 end
Chris@929 44 }
Chris@909 45
Chris@0 46 before_create :set_default_empty_values
chris@22 47
chris@22 48 def name(formatter = nil)
chris@22 49 to_s
chris@22 50 end
chris@22 51
Chris@0 52 def <=>(principal)
Chris@929 53 if principal.nil?
Chris@929 54 -1
Chris@929 55 elsif self.class.name == principal.class.name
Chris@0 56 self.to_s.downcase <=> principal.to_s.downcase
Chris@0 57 else
Chris@0 58 # groups after users
Chris@0 59 principal.class.name <=> self.class.name
Chris@0 60 end
Chris@0 61 end
Chris@909 62
Chris@0 63 protected
Chris@909 64
Chris@0 65 # Make sure we don't try to insert NULL values (see #4632)
Chris@0 66 def set_default_empty_values
Chris@0 67 self.login ||= ''
Chris@0 68 self.hashed_password ||= ''
Chris@0 69 self.firstname ||= ''
Chris@0 70 self.lastname ||= ''
Chris@0 71 self.mail ||= ''
Chris@0 72 true
Chris@0 73 end
Chris@0 74 end