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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class Enumeration < ApplicationRecord
  19. include Redmine::SubclassFactory
  20. default_scope lambda {order(:position)}
  21. belongs_to :project
  22. acts_as_positioned :scope => [:project_id, :parent_id]
  23. acts_as_customizable
  24. acts_as_tree
  25. before_destroy :check_integrity
  26. before_save :check_default
  27. after_save :update_children_name
  28. validates_presence_of :name
  29. validates_uniqueness_of :name, :scope => [:type, :project_id], :case_sensitive => true
  30. validates_length_of :name, :maximum => 30
  31. scope :shared, lambda {where(:project_id => nil)}
  32. scope :sorted, lambda {order(:position)}
  33. scope :active, lambda {where(:active => true)}
  34. scope :system, lambda {where(:project_id => nil)}
  35. scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
  36. def self.default
  37. # Creates a fake default scope so Enumeration.default will check
  38. # it's type. STI subclasses will automatically add their own
  39. # types to the finder.
  40. if self.descends_from_active_record?
  41. where(:is_default => true, :type => 'Enumeration').first
  42. else
  43. # STI classes are
  44. where(:is_default => true).first
  45. end
  46. end
  47. # Overloaded on concrete classes
  48. def option_name
  49. nil
  50. end
  51. def check_default
  52. if is_default? && is_default_changed?
  53. Enumeration.where({:type => type}).update_all({:is_default => false})
  54. end
  55. end
  56. # Overloaded on concrete classes
  57. def objects_count
  58. 0
  59. end
  60. def in_use?
  61. self.objects_count != 0
  62. end
  63. # Is this enumeration overriding a system level enumeration?
  64. def is_override?
  65. !self.parent.nil?
  66. end
  67. alias :destroy_without_reassign :destroy
  68. # Destroy the enumeration
  69. # If a enumeration is specified, objects are reassigned
  70. def destroy(reassign_to = nil)
  71. if reassign_to && reassign_to.is_a?(Enumeration)
  72. self.transfer_relations(reassign_to)
  73. end
  74. destroy_without_reassign
  75. end
  76. def <=>(enumeration)
  77. return nil unless enumeration.is_a?(Enumeration)
  78. position <=> enumeration.position
  79. end
  80. def to_s; name end
  81. # Returns the Subclasses of Enumeration. Each Subclass needs to be
  82. # required in development mode.
  83. #
  84. # Note: subclasses is protected in ActiveRecord
  85. def self.get_subclasses
  86. subclasses
  87. end
  88. # Does the +new+ Hash override the previous Enumeration?
  89. def self.overriding_change?(new, previous)
  90. if (same_active_state?(new['active'], previous.active)) &&
  91. same_custom_values?(new, previous)
  92. return false
  93. else
  94. return true
  95. end
  96. end
  97. # Does the +new+ Hash have the same custom values as the previous Enumeration?
  98. def self.same_custom_values?(new, previous)
  99. previous.custom_field_values.each do |custom_value|
  100. if custom_value.to_s != new["custom_field_values"][custom_value.custom_field_id.to_s].to_s
  101. return false
  102. end
  103. end
  104. return true
  105. end
  106. # Are the new and previous fields equal?
  107. def self.same_active_state?(new, previous)
  108. new = (new == "1" ? true : false)
  109. return new == previous
  110. end
  111. private
  112. def check_integrity
  113. raise "Cannot delete enumeration" if self.in_use?
  114. end
  115. def update_children_name
  116. if saved_change_to_name? && self.parent_id.nil?
  117. self.class.where(name: self.name_before_last_save, parent_id: self.id).update_all(name: self.name_in_database)
  118. end
  119. end
  120. # Overrides Redmine::Acts::Positioned#set_default_position so that enumeration overrides
  121. # get the same position as the overridden enumeration
  122. def set_default_position
  123. if position.nil? && parent
  124. self.position = parent.position
  125. end
  126. super
  127. end
  128. # Overrides Redmine::Acts::Positioned#update_position so that overrides get the same
  129. # position as the overridden enumeration
  130. def update_position
  131. super
  132. if saved_change_to_position? && self.parent_id.nil?
  133. self.class.where.not(:parent_id => nil).update_all(
  134. "position = coalesce((
  135. select position
  136. from (select id, position from enumerations) as parent
  137. where parent_id = parent.id), 1)"
  138. )
  139. end
  140. end
  141. # Overrides Redmine::Acts::Positioned#remove_position so that enumeration overrides
  142. # get the same position as the overridden enumeration
  143. def remove_position
  144. if parent_id.blank?
  145. super
  146. end
  147. end
  148. end