Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: class Principal < ActiveRecord::Base Chris@909: set_table_name "#{table_name_prefix}users#{table_name_suffix}" Chris@909: Chris@909: has_many :members, :foreign_key => 'user_id', :dependent => :destroy Chris@909: 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@909: has_many :projects, :through => :memberships Chris@909: has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify Chris@909: Chris@909: # Groups and active users Chris@909: named_scope :active, :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status = 1)" Chris@909: Chris@909: named_scope :like, lambda {|q| Chris@909: s = "%#{q.to_s.strip.downcase}%" Chris@909: {:conditions => ["LOWER(login) LIKE :s OR LOWER(firstname) LIKE :s OR LOWER(lastname) LIKE :s OR LOWER(mail) LIKE :s", {:s => s}], Chris@909: :order => 'type, login, lastname, firstname, mail' Chris@909: } Chris@909: } Chris@909: Chris@909: before_create :set_default_empty_values Chris@909: Chris@909: def name(formatter = nil) Chris@909: to_s Chris@909: end Chris@909: Chris@909: def <=>(principal) Chris@909: if self.class.name == principal.class.name Chris@909: self.to_s.downcase <=> principal.to_s.downcase Chris@909: else Chris@909: # groups after users Chris@909: principal.class.name <=> self.class.name Chris@909: end Chris@909: end Chris@909: Chris@909: protected Chris@909: Chris@909: # Make sure we don't try to insert NULL values (see #4632) Chris@909: def set_default_empty_values Chris@909: self.login ||= '' Chris@909: self.hashed_password ||= '' Chris@909: self.firstname ||= '' Chris@909: self.lastname ||= '' Chris@909: self.mail ||= '' Chris@909: true Chris@909: end Chris@909: end