From 7707457145442d6177ce57c956dbe09af65df1b4 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 12 Sep 2009 08:36:46 +0000 Subject: User groups branch merged. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2869 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/member.rb | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'app/models/member.rb') 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 -- cgit v1.2.3