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.

acts_as_customizable.rb 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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. module Redmine
  19. module Acts
  20. module Customizable
  21. def self.included(base)
  22. base.extend ClassMethods
  23. end
  24. module ClassMethods
  25. def acts_as_customizable(options = {})
  26. return if self.included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
  27. cattr_accessor :customizable_options
  28. self.customizable_options = options
  29. has_many :custom_values, lambda {includes(:custom_field)},
  30. :as => :customized,
  31. :inverse_of => :customized,
  32. :dependent => :delete_all,
  33. :validate => false
  34. send :include, Redmine::Acts::Customizable::InstanceMethods
  35. validate :validate_custom_field_values
  36. after_save :save_custom_field_values
  37. end
  38. end
  39. module InstanceMethods
  40. def self.included(base)
  41. base.extend ClassMethods
  42. end
  43. def available_custom_fields
  44. CustomField.where("type = '#{self.class.name}CustomField'").sorted.to_a
  45. end
  46. # Sets the values of the object's custom fields
  47. # values is an array like [{'id' => 1, 'value' => 'foo'}, {'id' => 2, 'value' => 'bar'}]
  48. def custom_fields=(values)
  49. values_to_hash = values.inject({}) do |hash, v|
  50. v = v.stringify_keys
  51. if v['id'] && v.has_key?('value')
  52. hash[v['id']] = v['value']
  53. end
  54. hash
  55. end
  56. self.custom_field_values = values_to_hash
  57. end
  58. # Sets the values of the object's custom fields
  59. # values is a hash like {'1' => 'foo', 2 => 'bar'}
  60. def custom_field_values=(values)
  61. values = values.stringify_keys
  62. custom_field_values.each do |custom_field_value|
  63. key = custom_field_value.custom_field_id.to_s
  64. if values.has_key?(key)
  65. custom_field_value.value = values[key]
  66. end
  67. end
  68. @custom_field_values_changed = true
  69. end
  70. def custom_field_values
  71. @custom_field_values ||= available_custom_fields.collect do |field|
  72. x = CustomFieldValue.new
  73. x.custom_field = field
  74. x.customized = self
  75. if field.multiple?
  76. values = custom_values.select { |v| v.custom_field == field }
  77. if values.empty?
  78. values << custom_values.build(:customized => self, :custom_field => field)
  79. end
  80. x.instance_variable_set("@value", values.map(&:value))
  81. else
  82. cv = custom_values.detect { |v| v.custom_field == field }
  83. cv ||= custom_values.build(:customized => self, :custom_field => field)
  84. x.instance_variable_set("@value", cv.value)
  85. end
  86. x.value_was = x.value.dup if x.value
  87. x
  88. end
  89. end
  90. def visible_custom_field_values
  91. custom_field_values.select(&:visible?)
  92. end
  93. def custom_field_values_changed?
  94. @custom_field_values_changed == true
  95. end
  96. # Should the default custom field value be set for the given custom_value?
  97. # By default, default custom field value is set for new objects only
  98. def set_custom_field_default?(custom_value)
  99. new_record?
  100. end
  101. def custom_value_for(c)
  102. field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
  103. custom_values.detect {|v| v.custom_field_id == field_id }
  104. end
  105. def custom_field_value(c)
  106. field_id = (c.is_a?(CustomField) ? c.id : c.to_i)
  107. custom_field_values.detect {|v| v.custom_field_id == field_id }.try(:value)
  108. end
  109. def validate_custom_field_values
  110. if new_record? || custom_field_values_changed?
  111. custom_field_values.each(&:validate_value)
  112. end
  113. end
  114. def save_custom_field_values
  115. target_custom_values = []
  116. custom_field_values.each do |custom_field_value|
  117. if custom_field_value.value.is_a?(Array)
  118. custom_field_value.value.each do |v|
  119. target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field && cv.value == v}
  120. target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field, :value => v)
  121. target_custom_values << target
  122. end
  123. else
  124. target = custom_values.detect {|cv| cv.custom_field == custom_field_value.custom_field}
  125. target ||= custom_values.build(:customized => self, :custom_field => custom_field_value.custom_field)
  126. target.value = custom_field_value.value
  127. target_custom_values << target
  128. end
  129. end
  130. self.custom_values = target_custom_values
  131. custom_values.each(&:save)
  132. touch if !saved_changes? && custom_values.any?(&:saved_changes?)
  133. @custom_field_values_changed = false
  134. true
  135. end
  136. def reassign_custom_field_values
  137. if @custom_field_values
  138. values = @custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
  139. @custom_field_values = nil
  140. self.custom_field_values = values
  141. end
  142. end
  143. def reset_custom_values!
  144. @custom_field_values = nil
  145. @custom_field_values_changed = true
  146. end
  147. def reload(*args)
  148. @custom_field_values = nil
  149. @custom_field_values_changed = false
  150. super
  151. end
  152. module ClassMethods
  153. end
  154. end
  155. end
  156. end
  157. end