backend.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class CoreWorkflow::Result::Backend
  3. def initialize(result_object:, field:, perform_config:, skip_rerun: false, skip_mark_restricted: false)
  4. @result_object = result_object
  5. @field = field
  6. @perform_config = perform_config
  7. @skip_rerun = skip_rerun
  8. @skip_mark_restricted = skip_mark_restricted
  9. end
  10. def field
  11. @field.sub(%r{.*\.}, '')
  12. end
  13. def multiple?
  14. @result_object.payload['params'][field].is_a?(Array) || @result_object.attributes.object_elements_hash.dig(field, :multiple)
  15. end
  16. def set_rerun
  17. return if @skip_rerun
  18. @result_object.rerun = true
  19. end
  20. def mark_restricted
  21. return if @skip_mark_restricted
  22. @result_object.restricted_fields[field] = true
  23. end
  24. def result(backend, field, value = nil)
  25. @result_object.run_backend_value(backend, field, value)
  26. end
  27. def saved_value
  28. # make sure we have a saved object
  29. return [] if @result_object.attributes.saved_only.blank?
  30. # we only want to have the saved value in the restrictions
  31. # if no changes happend to the form. If the users does changes
  32. # to the form then also the saved value should get removed
  33. return [] if @result_object.attributes.selected.changed?
  34. # attribute can be blank e.g. in custom development
  35. # or if attribute is only available in the frontend but not
  36. # in the backend
  37. return [] if attribute.blank?
  38. Array(@result_object.attributes.saved_attribute_value(attribute)).map(&:to_s)
  39. end
  40. def attribute
  41. @attribute ||= @result_object.attributes.object_elements_hash[field]
  42. end
  43. end