triggers_subscriptions.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Link::TriggersSubscriptions
  3. extend ActiveSupport::Concern
  4. included do
  5. after_commit :trigger_subscriptions, on: %i[create update destroy]
  6. end
  7. private
  8. def trigger_subscriptions
  9. # Captain, oh my captain! I hate to do this, but we need to do it.
  10. list = [
  11. [ link_object_source_id, link_object_source_value ],
  12. [ link_object_target_id, link_object_target_value ]
  13. ]
  14. list.each do |link_object_id, link_object_value|
  15. object_class = Link::Object.find(link_object_id).name.constantize
  16. object = begin
  17. object_class.find(link_object_value)
  18. rescue ActiveRecord::RecordNotFound
  19. # No need to inform a non-existing object about link changes
  20. next
  21. end
  22. target_type = Link::Object.find(link_object_target_id).name
  23. Gql::Subscriptions::LinkUpdates
  24. .trigger(
  25. nil,
  26. arguments: {
  27. object_id: object.to_global_id.to_s,
  28. target_type: target_type
  29. }
  30. )
  31. end
  32. end
  33. end