summaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2014-10-23 21:46:40 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2014-10-23 21:46:40 +0000
commitc2e73160daa7782d7a91f2b6a974a936c6f084da (patch)
tree62e174df69aba43cb5a84f2ddd278dc4562963f0 /app/models
parentca5946d82ebb96464a3d283be657bc24ce0c47f1 (diff)
downloadredmine-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')
-rw-r--r--app/models/member.rb24
-rw-r--r--app/models/principal.rb5
-rw-r--r--app/models/user.rb5
3 files changed, 23 insertions, 11 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
diff --git a/app/models/principal.rb b/app/models/principal.rb
index d10241b3f..e6e6ea78e 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -84,6 +84,11 @@ class Principal < ActiveRecord::Base
to_s
end
+ # Return true if the principal is a member of project
+ def member_of?(project)
+ projects.to_a.include?(project)
+ end
+
def <=>(principal)
if principal.nil?
-1
diff --git a/app/models/user.rb b/app/models/user.rb
index d86627e85..3ac98620b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -498,11 +498,6 @@ class User < Principal
end
end
- # Return true if the user is a member of project
- def member_of?(project)
- projects.to_a.include?(project)
- end
-
# Returns a hash of user's projects grouped by roles
def projects_by_role
return @projects_by_role if @projects_by_role