annotate .svn/pristine/54/5447dc3d932c927abf0b10719fdc9715866eb175.svn-base @ 1621:3a510bf6a9bc

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