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