comparison app/models/principal.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children e248c7af89ec
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 class Principal < ActiveRecord::Base 18 class Principal < ActiveRecord::Base
19 self.table_name = "#{table_name_prefix}users#{table_name_suffix}" 19 self.table_name = "#{table_name_prefix}users#{table_name_suffix}"
20 20
21 # Account statuses
22 STATUS_ANONYMOUS = 0
23 STATUS_ACTIVE = 1
24 STATUS_REGISTERED = 2
25 STATUS_LOCKED = 3
26
21 has_many :members, :foreign_key => 'user_id', :dependent => :destroy 27 has_many :members, :foreign_key => 'user_id', :dependent => :destroy
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" 28 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"
23 has_many :projects, :through => :memberships 29 has_many :projects, :through => :memberships
24 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify 30 has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
25 31
26 # Groups and active users 32 # Groups and active users
27 scope :active, :conditions => "#{Principal.table_name}.status = 1" 33 scope :active, lambda { where(:status => STATUS_ACTIVE) }
28 34
29 scope :like, lambda {|q| 35 scope :like, lambda {|q|
30 q = q.to_s 36 q = q.to_s
31 if q.blank? 37 if q.blank?
32 where({}) 38 where({})
49 projects = [projects] unless projects.is_a?(Array) 55 projects = [projects] unless projects.is_a?(Array)
50 if projects.empty? 56 if projects.empty?
51 where("1=0") 57 where("1=0")
52 else 58 else
53 ids = projects.map(&:id) 59 ids = projects.map(&:id)
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) 60 active.uniq.joins(:members).where("#{Member.table_name}.project_id IN (?)", ids)
55 end 61 end
56 } 62 }
57 # Principals that are not members of projects 63 # Principals that are not members of projects
58 scope :not_member_of, lambda {|projects| 64 scope :not_member_of, lambda {|projects|
59 projects = [projects] unless projects.is_a?(Array) 65 projects = [projects] unless projects.is_a?(Array)
62 else 68 else
63 ids = projects.map(&:id) 69 ids = projects.map(&:id)
64 where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids) 70 where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
65 end 71 end
66 } 72 }
73 scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
67 74
68 before_create :set_default_empty_values 75 before_create :set_default_empty_values
69 76
70 def name(formatter = nil) 77 def name(formatter = nil)
71 to_s 78 to_s
80 # groups after users 87 # groups after users
81 principal.class.name <=> self.class.name 88 principal.class.name <=> self.class.name
82 end 89 end
83 end 90 end
84 91
92 # Returns an array of fields names than can be used to make an order statement for principals.
93 # Users are sorted before Groups.
94 # Examples:
95 def self.fields_for_order_statement(table=nil)
96 table ||= table_name
97 columns = ['type DESC'] + (User.name_formatter[:order] - ['id']) + ['lastname', 'id']
98 columns.uniq.map {|field| "#{table}.#{field}"}
99 end
100
85 protected 101 protected
86 102
87 # Make sure we don't try to insert NULL values (see #4632) 103 # Make sure we don't try to insert NULL values (see #4632)
88 def set_default_empty_values 104 def set_default_empty_values
89 self.login ||= '' 105 self.login ||= ''