annotate .svn/pristine/7e/7ed0d1fdf9d439d825a4be4f6bf2f7c9112ea3a3.svn-base @ 1296:038ba2d95de8 redmine-2.2

Fix redmine-2.2 branch update (add missing svn files)
author Chris Cannam
date Fri, 14 Jun 2013 09:05:06 +0100
parents
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 class Member < ActiveRecord::Base
Chris@1296 19 belongs_to :user
Chris@1296 20 belongs_to :principal, :foreign_key => 'user_id'
Chris@1296 21 has_many :member_roles, :dependent => :destroy
Chris@1296 22 has_many :roles, :through => :member_roles
Chris@1296 23 belongs_to :project
Chris@1296 24
Chris@1296 25 validates_presence_of :principal, :project
Chris@1296 26 validates_uniqueness_of :user_id, :scope => :project_id
Chris@1296 27 validate :validate_role
Chris@1296 28
Chris@1296 29 before_destroy :set_issue_category_nil
Chris@1296 30 after_destroy :unwatch_from_permission_change
Chris@1296 31
Chris@1296 32 def role
Chris@1296 33 end
Chris@1296 34
Chris@1296 35 def role=
Chris@1296 36 end
Chris@1296 37
Chris@1296 38 def name
Chris@1296 39 self.user.name
Chris@1296 40 end
Chris@1296 41
Chris@1296 42 alias :base_role_ids= :role_ids=
Chris@1296 43 def role_ids=(arg)
Chris@1296 44 ids = (arg || []).collect(&:to_i) - [0]
Chris@1296 45 # Keep inherited roles
Chris@1296 46 ids += member_roles.select {|mr| !mr.inherited_from.nil?}.collect(&:role_id)
Chris@1296 47
Chris@1296 48 new_role_ids = ids - role_ids
Chris@1296 49 # Add new roles
Chris@1296 50 new_role_ids.each {|id| member_roles << MemberRole.new(:role_id => id) }
Chris@1296 51 # Remove roles (Rails' #role_ids= will not trigger MemberRole#on_destroy)
Chris@1296 52 member_roles_to_destroy = member_roles.select {|mr| !ids.include?(mr.role_id)}
Chris@1296 53 if member_roles_to_destroy.any?
Chris@1296 54 member_roles_to_destroy.each(&:destroy)
Chris@1296 55 unwatch_from_permission_change
Chris@1296 56 end
Chris@1296 57 end
Chris@1296 58
Chris@1296 59 def <=>(member)
Chris@1296 60 a, b = roles.sort.first, member.roles.sort.first
Chris@1296 61 if a == b
Chris@1296 62 if principal
Chris@1296 63 principal <=> member.principal
Chris@1296 64 else
Chris@1296 65 1
Chris@1296 66 end
Chris@1296 67 elsif a
Chris@1296 68 a <=> b
Chris@1296 69 else
Chris@1296 70 1
Chris@1296 71 end
Chris@1296 72 end
Chris@1296 73
Chris@1296 74 def deletable?
Chris@1296 75 member_roles.detect {|mr| mr.inherited_from}.nil?
Chris@1296 76 end
Chris@1296 77
Chris@1296 78 def include?(user)
Chris@1296 79 if principal.is_a?(Group)
Chris@1296 80 !user.nil? && user.groups.include?(principal)
Chris@1296 81 else
Chris@1296 82 self.user == user
Chris@1296 83 end
Chris@1296 84 end
Chris@1296 85
Chris@1296 86 def set_issue_category_nil
Chris@1296 87 if user
Chris@1296 88 # remove category based auto assignments for this member
Chris@1296 89 IssueCategory.update_all "assigned_to_id = NULL", ["project_id = ? AND assigned_to_id = ?", project.id, user.id]
Chris@1296 90 end
Chris@1296 91 end
Chris@1296 92
Chris@1296 93 # Find or initilize a Member with an id, attributes, and for a Principal
Chris@1296 94 def self.edit_membership(id, new_attributes, principal=nil)
Chris@1296 95 @membership = id.present? ? Member.find(id) : Member.new(:principal => principal)
Chris@1296 96 @membership.attributes = new_attributes
Chris@1296 97 @membership
Chris@1296 98 end
Chris@1296 99
Chris@1296 100 protected
Chris@1296 101
Chris@1296 102 def validate_role
Chris@1296 103 errors.add_on_empty :role if member_roles.empty? && roles.empty?
Chris@1296 104 end
Chris@1296 105
Chris@1296 106 private
Chris@1296 107
Chris@1296 108 # Unwatch things that the user is no longer allowed to view inside project
Chris@1296 109 def unwatch_from_permission_change
Chris@1296 110 if user
Chris@1296 111 Watcher.prune(:user => user, :project => project)
Chris@1296 112 end
Chris@1296 113 end
Chris@1296 114 end