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