annotate .svn/pristine/04/04dfe98ce0a611eee30923688999ac146c40ca35.svn-base @ 1384:b51b5ae3734c luisf

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