diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-09-12 08:36:46 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-09-12 08:36:46 +0000 |
commit | 7707457145442d6177ce57c956dbe09af65df1b4 (patch) | |
tree | 2278f27bf20e08bb2f92e42ab728b4427cce6f07 /app/models/member.rb | |
parent | 847c7367b429e8df0e0fa1dbf3e415e37dd82bf1 (diff) | |
download | redmine-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.rb')
-rw-r--r-- | app/models/member.rb | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/app/models/member.rb b/app/models/member.rb index 2dc91cba7..6fffb2161 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -17,40 +17,50 @@ class Member < ActiveRecord::Base belongs_to :user - has_many :member_roles, :dependent => :delete_all + belongs_to :principal, :foreign_key => 'user_id' + has_many :member_roles, :dependent => :destroy has_many :roles, :through => :member_roles belongs_to :project - validates_presence_of :user, :project + validates_presence_of :principal, :project validates_uniqueness_of :user_id, :scope => :project_id def name self.user.name end - # Sets user by login - def user_login=(login) - login = login.to_s - unless login.blank? - if (u = User.find_by_login(login)) - self.user = u - end - end + alias :base_role_ids= :role_ids= + def role_ids=(arg) + ids = (arg || []).collect(&:to_i) - [0] + # Keep inherited roles + ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id) + + new_role_ids = ids - role_ids + # Add new roles + new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) } + # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy) + member_roles.select {|mr| !ids.include?(mr.role_id)}.each(&:destroy) end def <=>(member) a, b = roles.sort.first, member.roles.sort.first - a == b ? (user <=> member.user) : (a <=> b) + a == b ? (principal <=> member.principal) : (a <=> b) + end + + def deletable? + member_roles.detect {|mr| mr.inherited_from}.nil? end def before_destroy - # remove category based auto assignments for this member - IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id] + if user + # remove category based auto assignments for this member + IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id] + end end protected def validate - errors.add_to_base "Role can't be blank" if roles.empty? + errors.add_to_base "Role can't be blank" if member_roles.empty? && roles.empty? end end |