annotate app/models/enumeration.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children e248c7af89ec
rev   line source
Chris@909 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class Enumeration < ActiveRecord::Base
Chris@1115 19 include Redmine::SubclassFactory
Chris@1115 20
Chris@0 21 default_scope :order => "#{Enumeration.table_name}.position ASC"
Chris@909 22
Chris@0 23 belongs_to :project
Chris@909 24
Chris@0 25 acts_as_list :scope => 'type = \'#{type}\''
Chris@0 26 acts_as_customizable
Chris@1295 27 acts_as_tree :order => "#{Enumeration.table_name}.position ASC"
Chris@0 28
Chris@0 29 before_destroy :check_integrity
Chris@909 30 before_save :check_default
Chris@909 31
Chris@1115 32 attr_protected :type
Chris@1115 33
Chris@0 34 validates_presence_of :name
Chris@0 35 validates_uniqueness_of :name, :scope => [:type, :project_id]
Chris@0 36 validates_length_of :name, :maximum => 30
Chris@0 37
Chris@1295 38 scope :shared, lambda { where(:project_id => nil) }
Chris@1295 39 scope :sorted, lambda { order("#{table_name}.position ASC") }
Chris@1295 40 scope :active, lambda { where(:active => true) }
Chris@1295 41 scope :system, lambda { where(:project_id => nil) }
Chris@1115 42 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
Chris@0 43
Chris@0 44 def self.default
Chris@0 45 # Creates a fake default scope so Enumeration.default will check
Chris@0 46 # it's type. STI subclasses will automatically add their own
Chris@0 47 # types to the finder.
Chris@0 48 if self.descends_from_active_record?
Chris@1115 49 where(:is_default => true, :type => 'Enumeration').first
Chris@0 50 else
Chris@0 51 # STI classes are
Chris@1115 52 where(:is_default => true).first
Chris@0 53 end
Chris@0 54 end
Chris@909 55
Chris@0 56 # Overloaded on concrete classes
Chris@0 57 def option_name
Chris@0 58 nil
Chris@0 59 end
Chris@0 60
Chris@909 61 def check_default
Chris@0 62 if is_default? && is_default_changed?
Chris@1115 63 Enumeration.update_all({:is_default => false}, {:type => type})
Chris@0 64 end
Chris@0 65 end
Chris@909 66
Chris@0 67 # Overloaded on concrete classes
Chris@0 68 def objects_count
Chris@0 69 0
Chris@0 70 end
Chris@909 71
Chris@0 72 def in_use?
Chris@0 73 self.objects_count != 0
Chris@0 74 end
Chris@0 75
Chris@0 76 # Is this enumeration overiding a system level enumeration?
Chris@0 77 def is_override?
Chris@0 78 !self.parent.nil?
Chris@0 79 end
Chris@909 80
Chris@0 81 alias :destroy_without_reassign :destroy
Chris@909 82
Chris@0 83 # Destroy the enumeration
Chris@0 84 # If a enumeration is specified, objects are reassigned
Chris@0 85 def destroy(reassign_to = nil)
Chris@0 86 if reassign_to && reassign_to.is_a?(Enumeration)
Chris@0 87 self.transfer_relations(reassign_to)
Chris@0 88 end
Chris@0 89 destroy_without_reassign
Chris@0 90 end
Chris@909 91
Chris@0 92 def <=>(enumeration)
Chris@0 93 position <=> enumeration.position
Chris@0 94 end
Chris@909 95
Chris@0 96 def to_s; name end
Chris@0 97
Chris@0 98 # Returns the Subclasses of Enumeration. Each Subclass needs to be
Chris@0 99 # required in development mode.
Chris@0 100 #
Chris@0 101 # Note: subclasses is protected in ActiveRecord
Chris@0 102 def self.get_subclasses
Chris@1115 103 subclasses
Chris@0 104 end
Chris@0 105
Chris@0 106 # Does the +new+ Hash override the previous Enumeration?
Chris@0 107 def self.overridding_change?(new, previous)
Chris@0 108 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
Chris@0 109 return false
Chris@0 110 else
Chris@0 111 return true
Chris@0 112 end
Chris@0 113 end
Chris@0 114
Chris@0 115 # Does the +new+ Hash have the same custom values as the previous Enumeration?
Chris@0 116 def self.same_custom_values?(new, previous)
Chris@0 117 previous.custom_field_values.each do |custom_value|
Chris@0 118 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
Chris@0 119 return false
Chris@0 120 end
Chris@0 121 end
Chris@0 122
Chris@0 123 return true
Chris@0 124 end
Chris@909 125
Chris@0 126 # Are the new and previous fields equal?
Chris@0 127 def self.same_active_state?(new, previous)
Chris@0 128 new = (new == "1" ? true : false)
Chris@0 129 return new == previous
Chris@0 130 end
Chris@909 131
Chris@0 132 private
Chris@0 133 def check_integrity
Chris@0 134 raise "Can't delete enumeration" if self.in_use?
Chris@0 135 end
Chris@0 136
Chris@0 137 end
Chris@0 138
Chris@0 139 # Force load the subclasses in development mode
Chris@0 140 require_dependency 'time_entry_activity'
Chris@0 141 require_dependency 'document_category'
Chris@0 142 require_dependency 'issue_priority'