comparison .svn/pristine/a6/a6c2d0d0b8c42346610cefc80f58028a31c51655.svn-base @ 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
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
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 MemberRole < ActiveRecord::Base
19 belongs_to :member
20 belongs_to :role
21
22 after_destroy :remove_member_if_empty
23
24 after_create :add_role_to_group_users, :add_role_to_subprojects
25 after_destroy :remove_inherited_roles
26
27 validates_presence_of :role
28 validate :validate_role_member
29
30 def validate_role_member
31 errors.add :role_id, :invalid if role && !role.member?
32 end
33
34 def inherited?
35 !inherited_from.nil?
36 end
37
38 private
39
40 def remove_member_if_empty
41 if member.roles.empty?
42 member.destroy
43 end
44 end
45
46 def add_role_to_group_users
47 if member.principal.is_a?(Group) && !inherited?
48 member.principal.users.each do |user|
49 user_member = Member.find_or_new(member.project_id, user.id)
50 user_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
51 user_member.save!
52 end
53 end
54 end
55
56 def add_role_to_subprojects
57 member.project.children.each do |subproject|
58 if subproject.inherit_members?
59 child_member = Member.find_or_new(subproject.id, member.user_id)
60 child_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
61 child_member.save!
62 end
63 end
64 end
65
66 def remove_inherited_roles
67 MemberRole.where(:inherited_from => id).all.group_by(&:member).each do |member, member_roles|
68 member_roles.each(&:destroy)
69 end
70 end
71 end