enumeration.rb
132 lines
| 4.1 KiB
| text/x-ruby
|
RubyLexer
|
r330 | # redMine - project management software | ||
# Copyright (C) 2006 Jean-Philippe Lang | ||||
# | ||||
# 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. | ||||
# | ||||
# This program is distributed in the hope that it will be useful, | ||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
class Enumeration < ActiveRecord::Base | ||||
|
r2677 | acts_as_list :scope => 'type = \'#{type}\'' | ||
|
r2831 | acts_as_customizable | ||
|
r789 | |||
|
r330 | before_destroy :check_integrity | ||
|
r2677 | validates_presence_of :name | ||
validates_uniqueness_of :name, :scope => [:type] | ||||
|
r590 | validates_length_of :name, :maximum => 30 | ||
|
r789 | |||
|
r2677 | # Backwards compatiblity named_scopes. | ||
# Can be removed post-0.9 | ||||
named_scope :priorities, :conditions => { :type => "IssuePriority" }, :order => 'position' do | ||||
ActiveSupport::Deprecation.warn("Enumeration#priorities is deprecated, use the IssuePriority class. (#{Redmine::Info.issue(3007)})") | ||||
def default | ||||
find(:first, :conditions => { :is_default => true }) | ||||
end | ||||
end | ||||
named_scope :document_categories, :conditions => { :type => "DocumentCategory" }, :order => 'position' do | ||||
ActiveSupport::Deprecation.warn("Enumeration#document_categories is deprecated, use the DocumentCategories class. (#{Redmine::Info.issue(3007)})") | ||||
def default | ||||
find(:first, :conditions => { :is_default => true }) | ||||
end | ||||
end | ||||
named_scope :activities, :conditions => { :type => "TimeEntryActivity" }, :order => 'position' do | ||||
ActiveSupport::Deprecation.warn("Enumeration#activities is deprecated, use the TimeEntryActivity class. (#{Redmine::Info.issue(3007)})") | ||||
def default | ||||
find(:first, :conditions => { :is_default => true }) | ||||
|
r2411 | end | ||
|
r789 | end | ||
|
r792 | |||
|
r2677 | named_scope :values, lambda {|type| { :conditions => { :type => type }, :order => 'position' } } do | ||
|
r2411 | def default | ||
find(:first, :conditions => { :is_default => true }) | ||||
end | ||||
|
r792 | end | ||
|
r789 | |||
|
r2677 | named_scope :all, :order => 'position' | ||
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 | ||||
# Overloaded on concrete classes | ||||
|
r330 | def option_name | ||
|
r2677 | nil | ||
end | ||||
# Backwards compatiblity. Can be removed post-0.9 | ||||
def opt | ||||
ActiveSupport::Deprecation.warn("Enumeration#opt is deprecated, use the STI classes now. (#{Redmine::Info.issue(3007)})") | ||||
return OptName | ||||
|
r330 | end | ||
|
r792 | |||
def before_save | ||||
|
r2121 | if is_default? && is_default_changed? | ||
|
r2677 | Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type}) | ||
|
r2121 | end | ||
|
r792 | end | ||
|
r789 | |||
|
r2677 | # Overloaded on concrete classes | ||
|
r1544 | def objects_count | ||
|
r2677 | 0 | ||
|
r1544 | end | ||
def in_use? | ||||
self.objects_count != 0 | ||||
end | ||||
alias :destroy_without_reassign :destroy | ||||
# Destroy the enumeration | ||||
# If a enumeration is specified, objects are reassigned | ||||
def destroy(reassign_to = nil) | ||||
if reassign_to && reassign_to.is_a?(Enumeration) | ||||
|
r2677 | self.transfer_relations(reassign_to) | ||
|
r1544 | end | ||
destroy_without_reassign | ||||
end | ||||
|
r866 | def <=>(enumeration) | ||
position <=> enumeration.position | ||||
end | ||||
|
r771 | def to_s; name end | ||
|
r2677 | |||
# 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 | ||||
|
r771 | |||
|
r330 | private | ||
def check_integrity | ||||
|
r1544 | raise "Can't delete enumeration" if self.in_use? | ||
|
r2 | end | ||
|
r2677 | |||
|
r2 | end | ||
|
r2677 | |||
# Force load the subclasses in development mode | ||||
require_dependency 'time_entry_activity' | ||||
require_dependency 'document_category' | ||||
require_dependency 'issue_priority' | ||||