assets.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class OnlineNotification
  3. module Assets
  4. extend ActiveSupport::Concern
  5. =begin
  6. get all assets / related models for this online notification item
  7. online_notification = OnlineNotification.find(123)
  8. result = online_notification.assets(assets_if_exists)
  9. returns
  10. result = {
  11. :OnlineNotification => {
  12. 123 => online_notification_model_123,
  13. 1234 => online_notification_model_1234,
  14. }
  15. }
  16. =end
  17. def assets(data)
  18. app_model = self.class.to_app_model
  19. if !data[ app_model ]
  20. data[ app_model ] = {}
  21. end
  22. return data if data[ app_model ][ id ]
  23. local_attributes = attributes_with_association_ids
  24. local_attributes['object'] = ObjectLookup.by_id(local_attributes['object_lookup_id'])
  25. local_attributes['type'] = TypeLookup.by_id(local_attributes['type_lookup_id'])
  26. # set temp. current attributes to assets pool to prevent
  27. # loops, will be updated with lookup attributes later
  28. data[ app_model ][ id ] = local_attributes
  29. ApplicationModel.assets_of_object_list([local_attributes], data)
  30. return data if !self['created_by_id'] && !self['updated_by_id']
  31. app_model_user = User.to_app_model
  32. %w[created_by_id updated_by_id].each do |local_user_id|
  33. next if !self[ local_user_id ]
  34. next if data[ app_model_user ] && data[ app_model_user ][ self[ local_user_id ] ]
  35. user = User.lookup(id: self[ local_user_id ])
  36. next if !user
  37. data = user.assets(data)
  38. end
  39. data
  40. end
  41. end
  42. end