File: app/models/enumeration.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  class: Enumeration#18
includes
  SubclassFactory ( Redmine )
inherits from
  Base ( ActiveRecord )
has properties
class method: default #42
method: option_name #55
method: check_default #59
method: objects_count #66
method: in_use? #70
method: is_override? #75
alias: destroy_without_reassign destroy #79
method: destroy / 1 #83
method: <=> / 1 #90
method: to_s #94
class method: get_subclasses #100
class method: overridding_change? / 2 #105
class method: same_custom_values? / 2 #114
class method: same_active_state? / 2 #125
method: check_integrity #131

Class Hierarchy

Object ( Builtin-Module )
Base ( ActiveRecord )
  Enumeration    #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 Enumeration < ActiveRecord::Base
  19    include Redmine::SubclassFactory
  20 
  21    default_scope :order => "#{Enumeration.table_name}.position ASC"
  22 
  23    belongs_to :project
  24 
  25    acts_as_list :scope => 'type = \'#{type}\''
  26    acts_as_customizable
  27    acts_as_tree :order => 'position ASC'
  28 
  29    before_destroy :check_integrity
  30    before_save    :check_default
  31 
  32    attr_protected :type
  33 
  34    validates_presence_of :name
  35    validates_uniqueness_of :name, :scope => [:type, :project_id]
  36    validates_length_of :name, :maximum => 30
  37 
  38    named_scope :shared, :conditions => { :project_id => nil }
  39    named_scope :active, :conditions => { :active => true }
  40    named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
  41 
  42    def self.default
  43      # Creates a fake default scope so Enumeration.default will check
  44      # it's type.  STI subclasses will automatically add their own
  45      # types to the finder.
  46      if self.descends_from_active_record?
  47        find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
  48      else
  49        # STI classes are
  50        find(:first, :conditions => { :is_default => true })
  51      end
  52    end
  53 
  54    # Overloaded on concrete classes
  55    def option_name
  56      nil
  57    end
  58 
  59    def check_default
  60      if is_default? && is_default_changed?
  61        Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
  62      end
  63    end
  64 
  65    # Overloaded on concrete classes
  66    def objects_count
  67      0
  68    end
  69 
  70    def in_use?
  71      self.objects_count != 0
  72    end
  73 
  74    # Is this enumeration overiding a system level enumeration?
  75    def is_override?
  76      !self.parent.nil?
  77    end
  78 
  79    alias :destroy_without_reassign :destroy
  80 
  81    # Destroy the enumeration
  82    # If a enumeration is specified, objects are reassigned
  83    def destroy(reassign_to = nil)
  84      if reassign_to && reassign_to.is_a?(Enumeration)
  85        self.transfer_relations(reassign_to)
  86      end
  87      destroy_without_reassign
  88    end
  89 
  90    def <=>(enumeration)
  91      position <=> enumeration.position
  92    end
  93 
  94    def to_s; name end
  95 
  96    # Returns the Subclasses of Enumeration.  Each Subclass needs to be
  97    # required in development mode.
  98    #
  99    # Note: subclasses is protected in ActiveRecord
 100    def self.get_subclasses
 101      subclasses
 102    end
 103 
 104    # Does the +new+ Hash override the previous Enumeration?
 105    def self.overridding_change?(new, previous)
 106      if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
 107        return false
 108      else
 109        return true
 110      end
 111    end
 112 
 113    # Does the +new+ Hash have the same custom values as the previous Enumeration?
 114    def self.same_custom_values?(new, previous)
 115      previous.custom_field_values.each do |custom_value|
 116        if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
 117          return false
 118        end
 119      end
 120 
 121      return true
 122    end
 123 
 124    # Are the new and previous fields equal?
 125    def self.same_active_state?(new, previous)
 126      new = (new == "1" ? true : false)
 127      return new == previous
 128    end
 129 
 130  private
 131    def check_integrity
 132      raise "Can't delete enumeration" if self.in_use?
 133    end
 134 
 135  end
 136 
 137  # Force load the subclasses in development mode
 138  require_dependency 'time_entry_activity'
 139  require_dependency 'document_category'
 140  require_dependency 'issue_priority'