Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

enumeration.rb 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 < ActiveRecord::Base
  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. position <=> enumeration.position
  78. end
  79. def to_s; name end
  80. # Returns the Subclasses of Enumeration. Each Subclass needs to be
  81. # required in development mode.
  82. #
  83. # Note: subclasses is protected in ActiveRecord
  84. def self.get_subclasses
  85. subclasses
  86. end
  87. # Does the +new+ Hash override the previous Enumeration?
  88. def self.overriding_change?(new, previous)
  89. if (same_active_state?(new['active'], previous.active)) &&
  90. same_custom_values?(new, previous)
  91. return false
  92. else
  93. return true
  94. end
  95. end
  96. # Does the +new+ Hash have the same custom values as the previous Enumeration?
  97. def self.same_custom_values?(new, previous)
  98. previous.custom_field_values.each do |custom_value|
  99. if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
  100. return false
  101. end
  102. end
  103. return true
  104. end
  105. # Are the new and previous fields equal?
  106. def self.same_active_state?(new, previous)
  107. new = (new == "1" ? true : false)
  108. return new == previous
  109. end
  110. private
  111. def check_integrity
  112. raise "Cannot delete enumeration" if self.in_use?
  113. end
  114. def update_children_name
  115. if saved_change_to_name? && self.parent_id.nil?
  116. self.class.where(name: self.name_before_last_save, parent_id: self.id).update_all(name: self.name_in_database)
  117. end
  118. end
  119. # Overrides Redmine::Acts::Positioned#set_default_position so that enumeration overrides
  120. # get the same position as the overridden enumeration
  121. def set_default_position
  122. if position.nil? && parent
  123. self.position = parent.position
  124. end
  125. super
  126. end
  127. # Overrides Redmine::Acts::Positioned#update_position so that overrides get the same
  128. # position as the overridden enumeration
  129. def update_position
  130. super
  131. if saved_change_to_position? && self.parent_id.nil?
  132. self.class.where.not(:parent_id => nil).update_all(
  133. "position = coalesce((
  134. select position
  135. from (select id, position from enumerations) as parent
  136. where parent_id = parent.id), 1)"
  137. )
  138. end
  139. end
  140. # Overrides Redmine::Acts::Positioned#remove_position so that enumeration overrides
  141. # get the same position as the overridden enumeration
  142. def remove_position
  143. if parent_id.blank?
  144. super
  145. end
  146. end
  147. end