To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / group.rb @ 442:753f1380d6bc

History | View | Annotate | Download (1.87 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2009  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
  has_and_belongs_to_many :users, :after_add => :user_added,
20
                                  :after_remove => :user_removed
21
  
22
  acts_as_customizable
23
  
24
  validates_presence_of :lastname
25
  validates_uniqueness_of :lastname, :case_sensitive => false
26
  validates_length_of :lastname, :maximum => 30
27
    
28
  def to_s
29
    lastname.to_s
30
  end
31
  
32
  def user_added(user)
33
    members.each do |member|
34
      next if member.project.nil?
35
      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)
36
      member.member_roles.each do |member_role|
37
        user_member.member_roles << MemberRole.new(:role => member_role.role, :inherited_from => member_role.id)
38
      end
39
      user_member.save!
40
    end
41
  end
42
  
43
  def user_removed(user)
44
    members.each do |member|
45
      MemberRole.find(:all, :include => :member,
46
                            :conditions => ["#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, member.member_role_ids]).each(&:destroy)
47
    end
48
  end
49
end