comparison .svn/pristine/20/20aee30a3886ed67fbcf2637dc7c9a63361c83fd.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class Group < Principal
19 include Redmine::SafeAttributes
20
21 has_and_belongs_to_many :users, :after_add => :user_added,
22 :after_remove => :user_removed
23
24 acts_as_customizable
25
26 validates_presence_of :lastname
27 validates_uniqueness_of :lastname, :case_sensitive => false
28 validates_length_of :lastname, :maximum => 255
29
30 before_destroy :remove_references_before_destroy
31
32 scope :sorted, lambda { order("#{table_name}.lastname ASC") }
33 scope :named, lambda {|arg| where("LOWER(#{table_name}.lastname) = LOWER(?)", arg.to_s.strip)}
34
35 safe_attributes 'name',
36 'user_ids',
37 'custom_field_values',
38 'custom_fields',
39 :if => lambda {|group, user| user.admin?}
40
41 def to_s
42 lastname.to_s
43 end
44
45 def name
46 lastname
47 end
48
49 def name=(arg)
50 self.lastname = arg
51 end
52
53 def user_added(user)
54 members.each do |member|
55 next if member.project.nil?
56 user_member = Member.find_by_project_id_and_user_id(member.project_id, user.id) || Member.new(:project_id => member.project_id, :user_id => user.id)
57 member.member_roles.each do |member_role|
58 user_member.member_roles << MemberRole.new(:role => member_role.role, :inherited_from => member_role.id)
59 end
60 user_member.save!
61 end
62 end
63
64 def user_removed(user)
65 members.each do |member|
66 MemberRole.
67 includes(:member).
68 where("#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, member.member_role_ids).
69 all.
70 each(&:destroy)
71 end
72 end
73
74 def self.human_attribute_name(attribute_key_name, *args)
75 attr_name = attribute_key_name.to_s
76 if attr_name == 'lastname'
77 attr_name = "name"
78 end
79 super(attr_name, *args)
80 end
81
82 private
83
84 # Removes references that are not handled by associations
85 def remove_references_before_destroy
86 return if self.id.nil?
87
88 Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id]
89 end
90 end