checks_kb_client_notification.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. module ChecksKbClientNotification
  2. extend ActiveSupport::Concern
  3. included do
  4. after_create :notify_kb_clients_after_create
  5. after_update :notify_kb_clients_after_update
  6. after_touch :notify_kb_clients_after_touch
  7. after_destroy :notify_kb_clients_after_destroy
  8. class_attribute :notify_kb_clients_suspend, default: false
  9. end
  10. def self.disable_in_all_classes!
  11. all_classes.each { |klass| klass.notify_kb_clients_suspend = true }
  12. end
  13. def self.enable_in_all_classes!
  14. all_classes.each { |klass| klass.notify_kb_clients_suspend = false }
  15. end
  16. def self.all_classes
  17. ActiveRecord::Base
  18. .descendants
  19. .select { |c| c.included_modules.include?(ChecksKbClientNotification) }
  20. end
  21. private
  22. # generic call
  23. def notify_kb_clients(event)
  24. return if self.class.notify_kb_clients_suspend?
  25. ChecksKbClientNotificationJob.notify_later(self, event)
  26. end
  27. def notify_kb_clients_after_create
  28. notify_kb_clients(:create)
  29. end
  30. def notify_kb_clients_after_update
  31. notify_kb_clients(:update)
  32. end
  33. def notify_kb_clients_after_touch
  34. notify_kb_clients(:touch)
  35. end
  36. def notify_kb_clients_after_destroy
  37. notify_kb_clients(:destroy)
  38. end
  39. end