Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: class Group < Principal Chris@1295: include Redmine::SafeAttributes Chris@1295: Chris@1295: has_and_belongs_to_many :users, :after_add => :user_added, Chris@1295: :after_remove => :user_removed Chris@1295: Chris@1295: acts_as_customizable Chris@1295: Chris@1295: validates_presence_of :lastname Chris@1295: validates_uniqueness_of :lastname, :case_sensitive => false Chris@1295: validates_length_of :lastname, :maximum => 255 Chris@1295: Chris@1295: before_destroy :remove_references_before_destroy Chris@1295: Chris@1295: scope :sorted, lambda { order("#{table_name}.lastname ASC") } Chris@1295: scope :named, lambda {|arg| where("LOWER(#{table_name}.lastname) = LOWER(?)", arg.to_s.strip)} Chris@1295: Chris@1295: safe_attributes 'name', Chris@1295: 'user_ids', Chris@1295: 'custom_field_values', Chris@1295: 'custom_fields', Chris@1295: :if => lambda {|group, user| user.admin?} Chris@1295: Chris@1295: def to_s Chris@1295: lastname.to_s Chris@1295: end Chris@1295: Chris@1295: def name Chris@1295: lastname Chris@1295: end Chris@1295: Chris@1295: def name=(arg) Chris@1295: self.lastname = arg Chris@1295: end Chris@1295: Chris@1295: def user_added(user) Chris@1295: members.each do |member| Chris@1295: next if member.project.nil? Chris@1295: 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) Chris@1295: member.member_roles.each do |member_role| Chris@1295: user_member.member_roles << MemberRole.new(:role => member_role.role, :inherited_from => member_role.id) Chris@1295: end Chris@1295: user_member.save! Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def user_removed(user) Chris@1295: members.each do |member| Chris@1295: MemberRole. Chris@1295: includes(:member). Chris@1295: where("#{Member.table_name}.user_id = ? AND #{MemberRole.table_name}.inherited_from IN (?)", user.id, member.member_role_ids). Chris@1295: all. Chris@1295: each(&:destroy) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def self.human_attribute_name(attribute_key_name, *args) Chris@1295: attr_name = attribute_key_name.to_s Chris@1295: if attr_name == 'lastname' Chris@1295: attr_name = "name" Chris@1295: end Chris@1295: super(attr_name, *args) Chris@1295: end Chris@1295: Chris@1295: private Chris@1295: Chris@1295: # Removes references that are not handled by associations Chris@1295: def remove_references_before_destroy Chris@1295: return if self.id.nil? Chris@1295: Chris@1295: Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] Chris@1295: end Chris@1295: end