You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

enumeration.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class Enumeration < ActiveRecord::Base
  18. include Redmine::SubclassFactory
  19. default_scope :order => "#{Enumeration.table_name}.position ASC"
  20. belongs_to :project
  21. acts_as_list :scope => 'type = \'#{type}\''
  22. acts_as_customizable
  23. acts_as_tree :order => 'position ASC'
  24. before_destroy :check_integrity
  25. before_save :check_default
  26. attr_protected :type
  27. validates_presence_of :name
  28. validates_uniqueness_of :name, :scope => [:type, :project_id]
  29. validates_length_of :name, :maximum => 30
  30. scope :shared, :conditions => { :project_id => nil }
  31. scope :active, :conditions => { :active => true }
  32. scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
  33. def self.default
  34. # Creates a fake default scope so Enumeration.default will check
  35. # it's type. STI subclasses will automatically add their own
  36. # types to the finder.
  37. if self.descends_from_active_record?
  38. find(:first, :conditions => { :is_default => true, :type => 'Enumeration' })
  39. else
  40. # STI classes are
  41. find(:first, :conditions => { :is_default => true })
  42. end
  43. end
  44. # Overloaded on concrete classes
  45. def option_name
  46. nil
  47. end
  48. def check_default
  49. if is_default? && is_default_changed?
  50. Enumeration.update_all("is_default = #{connection.quoted_false}", {:type => type})
  51. end
  52. end
  53. # Overloaded on concrete classes
  54. def objects_count
  55. 0
  56. end
  57. def in_use?
  58. self.objects_count != 0
  59. end
  60. # Is this enumeration overiding a system level enumeration?
  61. def is_override?
  62. !self.parent.nil?
  63. end
  64. alias :destroy_without_reassign :destroy
  65. # Destroy the enumeration
  66. # If a enumeration is specified, objects are reassigned
  67. def destroy(reassign_to = nil)
  68. if reassign_to && reassign_to.is_a?(Enumeration)
  69. self.transfer_relations(reassign_to)
  70. end
  71. destroy_without_reassign
  72. end
  73. def <=>(enumeration)
  74. position <=> enumeration.position
  75. end
  76. def to_s; name end
  77. # Returns the Subclasses of Enumeration. Each Subclass needs to be
  78. # required in development mode.
  79. #
  80. # Note: subclasses is protected in ActiveRecord
  81. def self.get_subclasses
  82. subclasses
  83. end
  84. # Does the +new+ Hash override the previous Enumeration?
  85. def self.overridding_change?(new, previous)
  86. if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
  87. return false
  88. else
  89. return true
  90. end
  91. end
  92. # Does the +new+ Hash have the same custom values as the previous Enumeration?
  93. def self.same_custom_values?(new, previous)
  94. previous.custom_field_values.each do |custom_value|
  95. if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
  96. return false
  97. end
  98. end
  99. return true
  100. end
  101. # Are the new and previous fields equal?
  102. def self.same_active_state?(new, previous)
  103. new = (new == "1" ? true : false)
  104. return new == previous
  105. end
  106. private
  107. def check_integrity
  108. raise "Can't delete enumeration" if self.in_use?
  109. end
  110. end
  111. # Force load the subclasses in development mode
  112. require_dependency 'time_entry_activity'
  113. require_dependency 'document_category'
  114. require_dependency 'issue_priority'