assets.rb 1.9 KB

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