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