applies_taskbar_state.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module FormUpdater::Concerns::AppliesTaskbarState
  3. extend ActiveSupport::Concern
  4. SKIP_FIELDS = %w[attachments].freeze
  5. class_methods do
  6. def apply_state_group_keys(group_keys)
  7. @apply_state_group_keys ||= group_keys
  8. end
  9. end
  10. def resolve
  11. if current_taskbar.present? && should_apply?
  12. apply_taskbar_state
  13. end
  14. super
  15. end
  16. private
  17. def apply_taskbar_state
  18. apply_value = FormUpdater::ApplyValue.new(context:, data:, result:)
  19. apply_state_group_keys = self.class.instance_variable_get(:@apply_state_group_keys)
  20. current_taskbar.state.each_pair do |field, value|
  21. next if SKIP_FIELDS.include?(field)
  22. if apply_state_group_keys.present? && apply_state_group_keys.include?(field) && value.is_a?(Hash)
  23. value.each_pair do |sub_field, sub_value|
  24. next if SKIP_FIELDS.include?(sub_field)
  25. apply_value.perform(field: sub_field, config: { 'value' => sub_value }, include_blank: true, parent_field: field)
  26. end
  27. else
  28. apply_value.perform(field: field, config: { 'value' => value }, include_blank: true)
  29. end
  30. end
  31. end
  32. def current_taskbar
  33. id = meta.dig(:additional_data, 'taskbarId')
  34. Gql::ZammadSchema.authorized_object_from_id(id, type: Taskbar, user: context[:current_user]) if id.present?
  35. end
  36. def should_apply?
  37. meta.dig(:additional_data, 'applyTaskbarState') == true || meta[:initial]
  38. end
  39. end