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