annotate .svn/pristine/47/47ed0077c51b203c1b8446fdf799171f3a5e25c0.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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