annotate .svn/pristine/d6/d6f5abe81850d0a2184399707a466120a7c0f2ab.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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