has_activity_stream_log.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module HasActivityStreamLog
  3. extend ActiveSupport::Concern
  4. included do
  5. after_create :activity_stream_create
  6. after_update :activity_stream_update
  7. before_destroy :activity_stream_destroy
  8. end
  9. =begin
  10. log object create activity stream, if configured - will be executed automatically
  11. model = Model.find(123)
  12. model.activity_stream_create
  13. =end
  14. def activity_stream_create
  15. activity_stream_log('create', self['created_by_id'])
  16. true
  17. end
  18. =begin
  19. log object update activity stream, if configured - will be executed automatically
  20. model = Model.find(123)
  21. model.activity_stream_update
  22. =end
  23. def activity_stream_update
  24. return true if !saved_changes?
  25. ignored_attributes = self.class.instance_variable_get(:@activity_stream_attributes_ignored) || []
  26. ignored_attributes += %i[created_at updated_at created_by_id updated_by_id]
  27. log = false
  28. saved_changes.each_key do |key|
  29. next if ignored_attributes.include?(key.to_sym)
  30. log = true
  31. end
  32. return true if !log
  33. activity_stream_log('update', self['updated_by_id'])
  34. true
  35. end
  36. =begin
  37. delete object activity stream, will be executed automatically
  38. model = Model.find(123)
  39. model.activity_stream_destroy
  40. =end
  41. def activity_stream_destroy
  42. ActivityStream.remove(self.class.to_s, id)
  43. true
  44. end
  45. # methods defined here are going to extend the class, not the instance of it
  46. class_methods do
  47. =begin
  48. serve method to ignore model attributes in activity stream and/or limit activity stream permission
  49. class Model < ApplicationModel
  50. include HasActivityStreamLog
  51. activity_stream_permission 'admin.user'
  52. activity_stream_attributes_ignored :create_article_type_id, :preferences
  53. end
  54. =end
  55. def activity_stream_attributes_ignored(*attributes)
  56. @activity_stream_attributes_ignored = attributes
  57. end
  58. def activity_stream_permission(permission)
  59. @activity_stream_permission = permission
  60. end
  61. end
  62. end