annotate .svn/pristine/13/13a70fe72c228213d71cc1e6f52274fd3abf3062.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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