stores_taskbar_state.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module FormUpdater::Concerns::StoresTaskbarState
  3. extend ActiveSupport::Concern
  4. class_methods do
  5. def store_state_collect_group_key(group_key)
  6. @store_state_collect_group_key ||= group_key
  7. end
  8. def store_state_group_keys(group_keys)
  9. @store_state_group_keys ||= group_keys
  10. end
  11. end
  12. def resolve
  13. resolved_result = super
  14. # Store handling needs to be done after all the other processing is over (so the result is present).
  15. if current_taskbar.present? && should_store?
  16. store_taskbar_state
  17. end
  18. resolved_result
  19. end
  20. private
  21. def store_taskbar_state
  22. store_state_collect_group_key = self.class.instance_variable_get(:@store_state_collect_group_key)
  23. store_state_group_keys = self.class.instance_variable_get(:@store_state_group_keys)
  24. store_value = FormUpdater::StoreValue.new(store_state_group_keys)
  25. state = {
  26. form_id: meta[:form_id],
  27. }
  28. if store_state_collect_group_key.present?
  29. state[store_state_collect_group_key] = {}
  30. end
  31. prepared_data.each_pair do |field, value|
  32. next if !should_store_field?(field, value, store_state_group_keys)
  33. field_state = store_value.perform(field:, value:)
  34. if store_state_collect_group_key.present? && (store_state_group_keys.blank? || store_state_group_keys.exclude?(field))
  35. state[store_state_collect_group_key] = state[store_state_collect_group_key].merge field_state
  36. else
  37. state = state.merge field_state
  38. end
  39. end
  40. after_store_taskbar_preperation(state) if self.class.method_defined?(:after_store_taskbar_preperation)
  41. current_taskbar.update!(state:)
  42. end
  43. def prepared_data
  44. # Iterate through the result hash and merge values.
  45. result.each do |key, value|
  46. # Only process fields that have a 'value' key.
  47. next if !value.key?(:value)
  48. data[key] = value[:value]
  49. end
  50. data
  51. end
  52. def current_taskbar
  53. id = meta.dig(:additional_data, 'taskbarId')
  54. return if id.blank?
  55. Gql::ZammadSchema.authorized_object_from_id(id, type: Taskbar, user: context[:current_user])
  56. end
  57. def should_store_field?(field, value, store_state_group_keys)
  58. # When no object already exists, we can ignore the check, then we save all values from the form.
  59. return true if object.blank?
  60. # State groups are always stored and the sub fields are checked separately.
  61. return true if store_state_group_keys&.include?(field)
  62. # Return always true, when field does not exists on object, because we need always to store the value.
  63. return true if !object.respond_to?(field)
  64. object[field] != value
  65. end
  66. def should_store?
  67. meta.dig(:additional_data, 'applyTaskbarState') != true && !meta[:initial]
  68. end
  69. end