obsolete.ChilliProject/app/models/enumeration.rb

132 lines
3.4 KiB
Ruby
Raw Normal View History

2011-10-29 16:19:11 +04:00
#-- encoding: UTF-8
#-- copyright
# ChiliProject is a project management system.
2011-05-30 22:52:25 +04:00
#
# Copyright (C) 2010-2011 the ChiliProject Team
2011-05-30 22:52:25 +04:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
2011-05-30 22:52:25 +04:00
#
# See doc/COPYRIGHT.rdoc for more details.
#++
class Enumeration < ActiveRecord::Base
default_scope :order => "#{Enumeration.table_name}.position ASC"
2011-05-30 22:52:25 +04:00
belongs_to :project
2011-05-30 22:52:25 +04:00
acts_as_list :scope => 'type = \'#{type}\''
acts_as_customizable
acts_as_tree :order => 'position ASC'
before_destroy :check_integrity
2011-05-30 22:52:25 +04:00
validates_presence_of :name
validates_uniqueness_of :name, :scope => [:type, :project_id]
validates_length_of :name, :maximum => 30
named_scope :shared, :conditions => { :project_id => nil }
named_scope :active, :conditions => { :active => true }
def self.default
# Creates a fake default scope so Enumeration.default will check
# it's type. STI subclasses will automatically add their own
# types to the finder.
if self.descends_from_active_record?
find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
else
# STI classes are
find(:first, :conditions => { :is_default => true })
end
end
2011-05-30 22:52:25 +04:00
# Overloaded on concrete classes
def option_name
nil
end
def before_save
if is_default? && is_default_changed?
Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
end
end
2011-05-30 22:52:25 +04:00
# Overloaded on concrete classes
def objects_count
0
end
2011-05-30 22:52:25 +04:00
def in_use?
self.objects_count != 0
end
# Is this enumeration overiding a system level enumeration?
def is_override?
!self.parent.nil?
end
2011-05-30 22:52:25 +04:00
alias :destroy_without_reassign :destroy
2011-05-30 22:52:25 +04:00
# Destroy the enumeration
# If a enumeration is specified, objects are reassigned
def destroy(reassign_to = nil)
if reassign_to && reassign_to.is_a?(Enumeration)
self.transfer_relations(reassign_to)
end
destroy_without_reassign
end
2011-05-30 22:52:25 +04:00
def <=>(enumeration)
position <=> enumeration.position
end
2011-05-30 22:52:25 +04:00
def to_s; name end
# Returns the Subclasses of Enumeration. Each Subclass needs to be
# required in development mode.
#
# Note: subclasses is protected in ActiveRecord
def self.get_subclasses
@@subclasses[Enumeration]
end
# Does the +new+ Hash override the previous Enumeration?
def self.overridding_change?(new, previous)
if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
return false
else
return true
end
end
# Does the +new+ Hash have the same custom values as the previous Enumeration?
def self.same_custom_values?(new, previous)
previous.custom_field_values.each do |custom_value|
if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
return false
end
end
return true
end
2011-05-30 22:52:25 +04:00
# Are the new and previous fields equal?
def self.same_active_state?(new, previous)
new = (new == "1" ? true : false)
return new == previous
end
2011-05-30 22:52:25 +04:00
private
def check_integrity
raise "Can't delete enumeration" if self.in_use?
end
end
# Force load the subclasses in development mode
require_dependency 'time_entry_activity'
require_dependency 'document_category'
require_dependency 'issue_priority'