diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2014-10-23 21:46:40 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2014-10-23 21:46:40 +0000 |
commit | c2e73160daa7782d7a91f2b6a974a936c6f084da (patch) | |
tree | 62e174df69aba43cb5a84f2ddd278dc4562963f0 /app/models/member.rb | |
parent | ca5946d82ebb96464a3d283be657bc24ce0c47f1 (diff) | |
download | redmine-c2e73160daa7782d7a91f2b6a974a936c6f084da.tar.gz redmine-c2e73160daa7782d7a91f2b6a974a936c6f084da.zip |
Adds a single controller for users and groups memberships and support for adding multiple projects at once (#11702).
git-svn-id: http://svn.redmine.org/redmine/trunk@13498 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/member.rb')
-rw-r--r-- | app/models/member.rb | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/app/models/member.rb b/app/models/member.rb index 8256d2e68..257178866 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -47,7 +47,7 @@ class Member < ActiveRecord::Base new_role_ids = ids - role_ids # Add new roles - new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) } + new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id, :member => self) } # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy) member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)} if member_roles_to_destroy.any? @@ -102,11 +102,23 @@ class Member < ActiveRecord::Base end end - # Find or initialize a Member with an id, attributes, and for a Principal - def self.edit_membership(id, new_attributes, principal=nil) - @membership = id.present? ? Member.find(id) : Member.new(:principal => principal) - @membership.attributes = new_attributes - @membership + # Creates memberships for principal with the attributes + # * project_ids : one or more project ids + # * role_ids : ids of the roles to give to each membership + # + # Example: + # Member.create_principal_memberships(user, :project_ids => [2, 5], :role_ids => [1, 3] + def self.create_principal_memberships(principal, attributes) + members = [] + if attributes + project_ids = Array.wrap(attributes[:project_ids] || attributes[:project_id]) + role_ids = attributes[:role_ids] + project_ids.each do |project_id| + members << Member.new(:principal => principal, :role_ids => role_ids, :project_id => project_id) + end + principal.members << members + end + members end # Finds or initilizes a Member for the given project and principal |