File: app/models/group.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: Group#18
inherits from
  Principal   
has properties
method: to_s #30
alias: name to_s #34
method: user_added / 1 #36
method: user_removed / 1 #47
class method: human_attribute_name / 2 #54
method: remove_references_before_destroy #65

Class Hierarchy

Object ( Builtin-Module )
Base ( ActiveRecord )
Principal
  Group    #18

Code

   1  # Redmine - project management software
   2  # Copyright (C) 2006-2011  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    before_destroy :remove_references_before_destroy
  29 
  30    def to_s
  31      lastname.to_s
  32    end
  33 
  34    alias :name :to_s
  35 
  36    def user_added(user)
  37      members.each do |member|
  38        next if member.project.nil?
  39        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)
  40        member.member_roles.each do |member_role|
  41          user_member.member_roles << MemberRole.new(:role => member_role.role, :inherited_from => member_role.id)
  42        end
  43        user_member.save!
  44      end
  45    end
  46 
  47    def user_removed(user)
  48      members.each do |member|
  49        MemberRole.find(:all, :include => :member,
  50                              :conditions => ["#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, member.member_role_ids]).each(&:destroy)
  51      end
  52    end
  53 
  54    def self.human_attribute_name(attribute_key_name, *args)
  55      attr_name = attribute_key_name.to_s
  56      if attr_name == 'lastname'
  57        attr_name = "name"
  58      end
  59      super(attr_name, *args)
  60    end
  61 
  62    private
  63 
  64    # Removes references that are not handled by associations
  65    def remove_references_before_destroy
  66      return if self.id.nil?
  67 
  68      Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id]
  69    end
  70  end