Mercurial > hg > soundsoftware-site
comparison .svn/pristine/c9/c9279655fb4fdfcc8e980a83f20d7c2161d58ca6.svn-base @ 1517:dffacf8a6908 redmine-2.5
Update to Redmine SVN revision 13367 on 2.5-stable branch
author | Chris Cannam |
---|---|
date | Tue, 09 Sep 2014 09:29:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1516:b450a9d58aed | 1517:dffacf8a6908 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 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 destroy | |
77 if member_roles.reload.present? | |
78 # destroying the last role will destroy another instance | |
79 # of the same Member record, #super would then trigger callbacks twice | |
80 member_roles.destroy_all | |
81 @destroyed = true | |
82 freeze | |
83 else | |
84 super | |
85 end | |
86 end | |
87 | |
88 def include?(user) | |
89 if principal.is_a?(Group) | |
90 !user.nil? && user.groups.include?(principal) | |
91 else | |
92 self.user == user | |
93 end | |
94 end | |
95 | |
96 def set_issue_category_nil | |
97 if user | |
98 # remove category based auto assignments for this member | |
99 IssueCategory.where(["project_id = ? AND assigned_to_id = ?", project.id, user.id]). | |
100 update_all("assigned_to_id = NULL") | |
101 end | |
102 end | |
103 | |
104 # Find or initilize a Member with an id, attributes, and for a Principal | |
105 def self.edit_membership(id, new_attributes, principal=nil) | |
106 @membership = id.present? ? Member.find(id) : Member.new(:principal => principal) | |
107 @membership.attributes = new_attributes | |
108 @membership | |
109 end | |
110 | |
111 # Finds or initilizes a Member for the given project and principal | |
112 def self.find_or_new(project, principal) | |
113 project_id = project.is_a?(Project) ? project.id : project | |
114 principal_id = principal.is_a?(Principal) ? principal.id : principal | |
115 | |
116 member = Member.find_by_project_id_and_user_id(project_id, principal_id) | |
117 member ||= Member.new(:project_id => project_id, :user_id => principal_id) | |
118 member | |
119 end | |
120 | |
121 protected | |
122 | |
123 def validate_role | |
124 errors.add_on_empty :role if member_roles.empty? && roles.empty? | |
125 end | |
126 end |