annotate .svn/pristine/13/13a70fe72c228213d71cc1e6f52274fd3abf3062.svn-base @ 1295:622f24f53b42 redmine-2.3

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