summaryrefslogtreecommitdiffstats
path: root/app/models/member_role.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2009-09-12 08:36:46 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2009-09-12 08:36:46 +0000
commit7707457145442d6177ce57c956dbe09af65df1b4 (patch)
tree2278f27bf20e08bb2f92e42ab728b4427cce6f07 /app/models/member_role.rb
parent847c7367b429e8df0e0fa1dbf3e415e37dd82bf1 (diff)
downloadredmine-7707457145442d6177ce57c956dbe09af65df1b4.tar.gz
redmine-7707457145442d6177ce57c956dbe09af65df1b4.zip
User groups branch merged.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2869 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/member_role.rb')
-rw-r--r--app/models/member_role.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/app/models/member_role.rb b/app/models/member_role.rb
index 46777cd1e..5a31c17c5 100644
--- a/app/models/member_role.rb
+++ b/app/models/member_role.rb
@@ -19,9 +19,36 @@ class MemberRole < ActiveRecord::Base
belongs_to :member
belongs_to :role
+ after_destroy :remove_member_if_empty
+
+ after_create :add_role_to_group_users
+ after_destroy :remove_role_from_group_users
+
validates_presence_of :role
def validate
errors.add :role_id, :invalid if role && !role.member?
end
+
+ private
+
+ def remove_member_if_empty
+ if member.roles.empty?
+ member.destroy
+ end
+ end
+
+ def add_role_to_group_users
+ if member.principal.is_a?(Group)
+ member.principal.users.each do |user|
+ user_member = Member.find_by_project_id_and_user_id(member.project_id, user.id) || Member.new(:project_id => member.project_id, :user_id => user.id)
+ user_member.member_roles << MemberRole.new(:role => role, :inherited_from => id)
+ user_member.save!
+ end
+ end
+ end
+
+ def remove_role_from_group_users
+ MemberRole.find(:all, :conditions => { :inherited_from => id }).each(&:destroy)
+ end
end