annotate .svn/pristine/c9/c9279655fb4fdfcc8e980a83f20d7c2161d58ca6.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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