store_value.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FormUpdater::StoreValue
  3. include Mixin::RequiredSubPaths
  4. class OmitFieldError < StandardError; end
  5. FIELD_RENAMING_MAP = {
  6. 'articleSenderType' => 'formSenderType',
  7. }.freeze
  8. attr_reader :store_state_group_keys
  9. def initialize(store_state_group_keys)
  10. @store_state_group_keys = store_state_group_keys || []
  11. end
  12. def perform(field:, value:)
  13. field = FIELD_RENAMING_MAP[field] || field
  14. result = {}
  15. # If the field is in the skip keys and the value is a hash, handle it recursively
  16. if store_state_group_keys.include?(field) && value.is_a?(Hash)
  17. sub_result = {}
  18. value.each do |sub_field, sub_value|
  19. # Perform processing for each sub-field and merge the results
  20. sub_result.merge!(perform(field: sub_field, value: sub_value))
  21. end
  22. # Save the processed sub-fields inside the main field key
  23. result[field] = sub_result
  24. else
  25. begin
  26. # Handle complex fields via their handler.
  27. result[field] = if (handler = find_handler(field:, value:))
  28. handler.store_value(field:, value:)
  29. # Return the passed value for simple fields.
  30. else
  31. value
  32. end
  33. rescue FormUpdater::StoreValue::Base::OmitFieldError
  34. # Skip omitted fields.
  35. end
  36. end
  37. result
  38. end
  39. private
  40. def find_handler(field:, value:)
  41. FormUpdater::StoreValue::Base
  42. .descendants
  43. .lazy
  44. .map(&:new)
  45. .find { |elem| elem.can_handle_field?(field:, value:) }
  46. end
  47. end