apply_value.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FormUpdater::ApplyValue
  3. include Mixin::RequiredSubPaths
  4. attr_reader :context, :data, :dirty_fields, :result
  5. def initialize(context:, data:, result:, dirty_fields: nil)
  6. @context = context
  7. @data = data
  8. @dirty_fields = dirty_fields
  9. @result = result
  10. end
  11. FIELD_RENAMING_MAP = {
  12. 'formSenderType' => 'articleSenderType',
  13. 'article.type' => 'articleType',
  14. }.freeze
  15. def perform(field:, config:, include_blank: false, parent_field: nil)
  16. # Skip fields without a configured value
  17. return if config['value'].blank? && !include_blank
  18. full_field_path = parent_field ? "#{parent_field}.#{field}" : field
  19. field = FIELD_RENAMING_MAP[full_field_path] || field
  20. result[field] ||= {}
  21. # Cache the field attribute
  22. field_attribute = ObjectManager::Attribute.get(object: 'Ticket', name: field)
  23. # Complex fields
  24. if (handler = find_handler(field:, field_attribute:))
  25. return handler.apply_value(field:, config:)
  26. end
  27. # Simple fields
  28. return if dirty_fields&.include?(field) && data[field].present?
  29. result[field][:value] = config['value']
  30. end
  31. private
  32. def find_handler(field:, field_attribute:)
  33. FormUpdater::ApplyValue::Base
  34. .descendants
  35. .lazy
  36. .map { |handler_class| handler_class.new(context:, data:, dirty_fields:, result:) }
  37. .find { |elem| elem.can_handle_field?(field:, field_attribute:) }
  38. end
  39. end