annotate .svn/pristine/ca/ca2690f201462335a7ca37a385b267b1298dce63.svn-base @ 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
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 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 => "#{Enumeration.table_name}.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, 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@1295 42 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
Chris@1295 43
Chris@1295 44 def self.default
Chris@1295 45 # Creates a fake default scope so Enumeration.default will check
Chris@1295 46 # it's type. STI subclasses will automatically add their own
Chris@1295 47 # types to the finder.
Chris@1295 48 if self.descends_from_active_record?
Chris@1295 49 where(:is_default => true, :type => 'Enumeration').first
Chris@1295 50 else
Chris@1295 51 # STI classes are
Chris@1295 52 where(:is_default => true).first
Chris@1295 53 end
Chris@1295 54 end
Chris@1295 55
Chris@1295 56 # Overloaded on concrete classes
Chris@1295 57 def option_name
Chris@1295 58 nil
Chris@1295 59 end
Chris@1295 60
Chris@1295 61 def check_default
Chris@1295 62 if is_default? && is_default_changed?
Chris@1295 63 Enumeration.update_all({:is_default => false}, {:type => type})
Chris@1295 64 end
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 # Overloaded on concrete classes
Chris@1295 68 def objects_count
Chris@1295 69 0
Chris@1295 70 end
Chris@1295 71
Chris@1295 72 def in_use?
Chris@1295 73 self.objects_count != 0
Chris@1295 74 end
Chris@1295 75
Chris@1295 76 # Is this enumeration overiding a system level enumeration?
Chris@1295 77 def is_override?
Chris@1295 78 !self.parent.nil?
Chris@1295 79 end
Chris@1295 80
Chris@1295 81 alias :destroy_without_reassign :destroy
Chris@1295 82
Chris@1295 83 # Destroy the enumeration
Chris@1295 84 # If a enumeration is specified, objects are reassigned
Chris@1295 85 def destroy(reassign_to = nil)
Chris@1295 86 if reassign_to && reassign_to.is_a?(Enumeration)
Chris@1295 87 self.transfer_relations(reassign_to)
Chris@1295 88 end
Chris@1295 89 destroy_without_reassign
Chris@1295 90 end
Chris@1295 91
Chris@1295 92 def <=>(enumeration)
Chris@1295 93 position <=> enumeration.position
Chris@1295 94 end
Chris@1295 95
Chris@1295 96 def to_s; name end
Chris@1295 97
Chris@1295 98 # Returns the Subclasses of Enumeration. Each Subclass needs to be
Chris@1295 99 # required in development mode.
Chris@1295 100 #
Chris@1295 101 # Note: subclasses is protected in ActiveRecord
Chris@1295 102 def self.get_subclasses
Chris@1295 103 subclasses
Chris@1295 104 end
Chris@1295 105
Chris@1295 106 # Does the +new+ Hash override the previous Enumeration?
Chris@1295 107 def self.overridding_change?(new, previous)
Chris@1295 108 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
Chris@1295 109 return false
Chris@1295 110 else
Chris@1295 111 return true
Chris@1295 112 end
Chris@1295 113 end
Chris@1295 114
Chris@1295 115 # Does the +new+ Hash have the same custom values as the previous Enumeration?
Chris@1295 116 def self.same_custom_values?(new, previous)
Chris@1295 117 previous.custom_field_values.each do |custom_value|
Chris@1295 118 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
Chris@1295 119 return false
Chris@1295 120 end
Chris@1295 121 end
Chris@1295 122
Chris@1295 123 return true
Chris@1295 124 end
Chris@1295 125
Chris@1295 126 # Are the new and previous fields equal?
Chris@1295 127 def self.same_active_state?(new, previous)
Chris@1295 128 new = (new == "1" ? true : false)
Chris@1295 129 return new == previous
Chris@1295 130 end
Chris@1295 131
Chris@1295 132 private
Chris@1295 133 def check_integrity
Chris@1295 134 raise "Can't delete enumeration" if self.in_use?
Chris@1295 135 end
Chris@1295 136
Chris@1295 137 end
Chris@1295 138
Chris@1295 139 # Force load the subclasses in development mode
Chris@1295 140 require_dependency 'time_entry_activity'
Chris@1295 141 require_dependency 'document_category'
Chris@1295 142 require_dependency 'issue_priority'