Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Enumeration < ActiveRecord::Base Chris@0: default_scope :order => "#{Enumeration.table_name}.position ASC" Chris@909: Chris@0: belongs_to :project Chris@909: Chris@0: acts_as_list :scope => 'type = \'#{type}\'' Chris@0: acts_as_customizable Chris@0: acts_as_tree :order => 'position ASC' Chris@0: Chris@0: before_destroy :check_integrity Chris@909: before_save :check_default Chris@909: Chris@0: validates_presence_of :name Chris@0: validates_uniqueness_of :name, :scope => [:type, :project_id] Chris@0: validates_length_of :name, :maximum => 30 Chris@0: Chris@0: named_scope :shared, :conditions => { :project_id => nil } Chris@0: named_scope :active, :conditions => { :active => true } Chris@507: named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}} Chris@0: Chris@0: def self.default Chris@0: # Creates a fake default scope so Enumeration.default will check Chris@0: # it's type. STI subclasses will automatically add their own Chris@0: # types to the finder. Chris@0: if self.descends_from_active_record? Chris@0: find(:first, :conditions => { :is_default => true, :type => 'Enumeration' }) Chris@0: else Chris@0: # STI classes are Chris@0: find(:first, :conditions => { :is_default => true }) Chris@0: end Chris@0: end Chris@909: Chris@0: # Overloaded on concrete classes Chris@0: def option_name Chris@0: nil Chris@0: end Chris@0: Chris@909: def check_default Chris@0: if is_default? && is_default_changed? Chris@0: Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type}) Chris@0: end Chris@0: end Chris@909: Chris@0: # Overloaded on concrete classes Chris@0: def objects_count Chris@0: 0 Chris@0: end Chris@909: Chris@0: def in_use? Chris@0: self.objects_count != 0 Chris@0: end Chris@0: Chris@0: # Is this enumeration overiding a system level enumeration? Chris@0: def is_override? Chris@0: !self.parent.nil? Chris@0: end Chris@909: Chris@0: alias :destroy_without_reassign :destroy Chris@909: Chris@0: # Destroy the enumeration Chris@0: # If a enumeration is specified, objects are reassigned Chris@0: def destroy(reassign_to = nil) Chris@0: if reassign_to && reassign_to.is_a?(Enumeration) Chris@0: self.transfer_relations(reassign_to) Chris@0: end Chris@0: destroy_without_reassign Chris@0: end Chris@909: Chris@0: def <=>(enumeration) Chris@0: position <=> enumeration.position Chris@0: end Chris@909: Chris@0: def to_s; name end Chris@0: Chris@0: # Returns the Subclasses of Enumeration. Each Subclass needs to be Chris@0: # required in development mode. Chris@0: # Chris@0: # Note: subclasses is protected in ActiveRecord Chris@0: def self.get_subclasses Chris@0: @@subclasses[Enumeration] Chris@0: end Chris@0: Chris@0: # Does the +new+ Hash override the previous Enumeration? Chris@0: def self.overridding_change?(new, previous) Chris@0: if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous) Chris@0: return false Chris@0: else Chris@0: return true Chris@0: end Chris@0: end Chris@0: Chris@0: # Does the +new+ Hash have the same custom values as the previous Enumeration? Chris@0: def self.same_custom_values?(new, previous) Chris@0: previous.custom_field_values.each do |custom_value| Chris@0: if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s] Chris@0: return false Chris@0: end Chris@0: end Chris@0: Chris@0: return true Chris@0: end Chris@909: Chris@0: # Are the new and previous fields equal? Chris@0: def self.same_active_state?(new, previous) Chris@0: new = (new == "1" ? true : false) Chris@0: return new == previous Chris@0: end Chris@909: Chris@0: private Chris@0: def check_integrity Chris@0: raise "Can't delete enumeration" if self.in_use? Chris@0: end Chris@0: Chris@0: end Chris@0: Chris@0: # Force load the subclasses in development mode Chris@0: require_dependency 'time_entry_activity' Chris@0: require_dependency 'document_category' Chris@0: require_dependency 'issue_priority'