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