Mercurial > hg > soundsoftware-site
comparison .svn/pristine/22/2231a90d74735e9a796f1508f0e2f1bfd1774f24.svn-base @ 1295:622f24f53b42 redmine-2.3
Update to Redmine SVN revision 11972 on 2.3-stable branch
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:02:21 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1294:3e4c3460b6ca | 1295:622f24f53b42 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2013 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program; if not, write to the Free Software | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 class Member < ActiveRecord::Base | |
19 belongs_to :user | |
20 belongs_to :principal, :foreign_key => 'user_id' | |
21 has_many :member_roles, :dependent => :destroy | |
22 has_many :roles, :through => :member_roles | |
23 belongs_to :project | |
24 | |
25 validates_presence_of :principal, :project | |
26 validates_uniqueness_of :user_id, :scope => :project_id | |
27 validate :validate_role | |
28 | |
29 before_destroy :set_issue_category_nil | |
30 | |
31 def role | |
32 end | |
33 | |
34 def role= | |
35 end | |
36 | |
37 def name | |
38 self.user.name | |
39 end | |
40 | |
41 alias :base_role_ids= :role_ids= | |
42 def role_ids=(arg) | |
43 ids = (arg || []).collect(&:to_i) - [0] | |
44 # Keep inherited roles | |
45 ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id) | |
46 | |
47 new_role_ids = ids - role_ids | |
48 # Add new roles | |
49 new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) } | |
50 # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy) | |
51 member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)} | |
52 if member_roles_to_destroy.any? | |
53 member_roles_to_destroy.each(&:destroy) | |
54 end | |
55 end | |
56 | |
57 def <=>(member) | |
58 a, b = roles.sort.first, member.roles.sort.first | |
59 if a == b | |
60 if principal | |
61 principal <=> member.principal | |
62 else | |
63 1 | |
64 end | |
65 elsif a | |
66 a <=> b | |
67 else | |
68 1 | |
69 end | |
70 end | |
71 | |
72 def deletable? | |
73 member_roles.detect {|mr| mr.inherited_from}.nil? | |
74 end | |
75 | |
76 def include?(user) | |
77 if principal.is_a?(Group) | |
78 !user.nil? && user.groups.include?(principal) | |
79 else | |
80 self.user == user | |
81 end | |
82 end | |
83 | |
84 def set_issue_category_nil | |
85 if user | |
86 # remove category based auto assignments for this member | |
87 IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id] | |
88 end | |
89 end | |
90 | |
91 # Find or initilize a Member with an id, attributes, and for a Principal | |
92 def self.edit_membership(id, new_attributes, principal=nil) | |
93 @membership = id.present? ? Member.find(id) : Member.new(:principal => principal) | |
94 @membership.attributes = new_attributes | |
95 @membership | |
96 end | |
97 | |
98 # Finds or initilizes a Member for the given project and principal | |
99 def self.find_or_new(project, principal) | |
100 project_id = project.is_a?(Project) ? project.id : project | |
101 principal_id = principal.is_a?(Principal) ? principal.id : principal | |
102 | |
103 member = Member.find_by_project_id_and_user_id(project_id, principal_id) | |
104 member ||= Member.new(:project_id => project_id, :user_id => principal_id) | |
105 member | |
106 end | |
107 | |
108 protected | |
109 | |
110 def validate_role | |
111 errors.add_on_empty :role if member_roles.empty? && roles.empty? | |
112 end | |
113 end |