assets.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Organization
  3. module Assets
  4. extend ActiveSupport::Concern
  5. =begin
  6. get all assets / related models for this organization
  7. organization = Organization.find(123)
  8. result = organization.assets(assets_if_exists)
  9. returns
  10. result = {
  11. :organizations => {
  12. 123 => organization_model_123,
  13. 1234 => organization_model_1234,
  14. }
  15. }
  16. =end
  17. def assets(data)
  18. app_model_organization = Organization.to_app_model
  19. app_model_user = User.to_app_model
  20. if !data[ app_model_organization ]
  21. data[ app_model_organization ] = {}
  22. end
  23. if !data[ app_model_user ]
  24. data[ app_model_user ] = {}
  25. end
  26. if !data[ app_model_organization ][ id ]
  27. local_attributes = attributes_with_association_ids
  28. # set temp. current attributes to assets pool to prevent
  29. # loops, will be updated with lookup attributes later
  30. data[ app_model_organization ][ id ] = local_attributes
  31. if local_attributes['member_ids'].present?
  32. # feature used for different purpose; do limit references
  33. if local_attributes['member_ids'].count > 100
  34. local_attributes['member_ids'] = local_attributes['member_ids'].sort[0, 100]
  35. end
  36. local_attributes['member_ids'].each do |local_user_id|
  37. next if data[ app_model_user ] && data[ app_model_user ][ local_user_id ]
  38. user = User.lookup(id: local_user_id)
  39. next if !user
  40. data = user.assets(data)
  41. end
  42. end
  43. data[ app_model_organization ][ id ] = local_attributes
  44. end
  45. %w[created_by_id updated_by_id].each do |local_user_id|
  46. next if !self[ local_user_id ]
  47. next if data[ app_model_user ][ self[ local_user_id ] ]
  48. user = User.lookup(id: self[ local_user_id ])
  49. next if !user
  50. data = user.assets(data)
  51. end
  52. data
  53. end
  54. end
  55. end