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