File: app/models/principal.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: Principal#18
inherits from
  Base ( ActiveRecord )
has properties
method: name / 1 #69
method: <=> / 1 #73
method: set_default_empty_values #87

Class Hierarchy

Object ( Builtin-Module )
Base ( ActiveRecord )
  Principal    #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 Principal < ActiveRecord::Base
  19    set_table_name "#{table_name_prefix}users#{table_name_suffix}"
  20 
  21    has_many :members, :foreign_key => 'user_id', :dependent => :destroy
  22    has_many :memberships, :class_name => 'Member', :foreign_key => 'user_id', :include => [ :project, :roles ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :order => "#{Project.table_name}.name"
  23    has_many :projects, :through => :memberships
  24    has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
  25 
  26    # Groups and active users
  27    named_scope :active, :conditions => "#{Principal.table_name}.status = 1"
  28 
  29    named_scope :like, lambda {|q|
  30      if q.blank?
  31        {}
  32      else
  33        q = q.to_s.downcase
  34        pattern = "%#{q}%"
  35        sql = "LOWER(login) LIKE :p OR LOWER(firstname) LIKE :p OR LOWER(lastname) LIKE :p OR LOWER(mail) LIKE :p"
  36        params = {:p => pattern}
  37        if q =~ /^(.+)\s+(.+)$/
  38          a, b = "#{$1}%", "#{$2}%"
  39          sql << " OR (LOWER(firstname) LIKE :a AND LOWER(lastname) LIKE :b) OR (LOWER(firstname) LIKE :b AND LOWER(lastname) LIKE :a)"
  40          params.merge!(:a => a, :b => b)
  41        end
  42        {:conditions => [sql, params]}
  43      end
  44    }
  45 
  46    # Principals that are members of a collection of projects
  47    named_scope :member_of, lambda {|projects|
  48      projects = [projects] unless projects.is_a?(Array)
  49      if projects.empty?
  50        {:conditions => "1=0"}
  51      else
  52        ids = projects.map(&:id)
  53        {:conditions => ["#{Principal.table_name}.status = 1 AND #{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]}
  54      end
  55    }
  56    # Principals that are not members of projects
  57    named_scope :not_member_of, lambda {|projects|
  58      projects = [projects] unless projects.is_a?(Array)
  59      if projects.empty?
  60        {:conditions => "1=0"}
  61      else
  62        ids = projects.map(&:id)
  63        {:conditions => ["#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids]}
  64      end
  65    }
  66 
  67    before_create :set_default_empty_values
  68 
  69    def name(formatter = nil)
  70      to_s
  71    end
  72 
  73    def <=>(principal)
  74      if principal.nil?
  75        -1
  76      elsif self.class.name == principal.class.name
  77        self.to_s.downcase <=> principal.to_s.downcase
  78      else
  79        # groups after users
  80        principal.class.name <=> self.class.name
  81      end
  82    end
  83 
  84    protected
  85 
  86    # Make sure we don't try to insert NULL values (see #4632)
  87    def set_default_empty_values
  88      self.login ||= ''
  89      self.hashed_password ||= ''
  90      self.firstname ||= ''
  91      self.lastname ||= ''
  92      self.mail ||= ''
  93      true
  94    end
  95  end