can_assets.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::CanAssets
  3. extend ActiveSupport::Concern
  4. =begin
  5. get all assets / related models for this user
  6. user = User.find(123)
  7. result = user.assets(assets_if_exists)
  8. returns
  9. result = {
  10. :User => {
  11. 123 => user_model_123,
  12. 1234 => user_model_1234,
  13. }
  14. }
  15. =end
  16. def assets(data = {})
  17. app_model = self.class.to_app_model
  18. if !data[ app_model ]
  19. data[ app_model ] = {}
  20. end
  21. if !data[ app_model ][ id ]
  22. data[ app_model ][ id ] = attributes_with_association_ids
  23. end
  24. return data if !self['created_by_id'] && !self['updated_by_id']
  25. app_model_user = User.to_app_model
  26. %w[created_by_id updated_by_id].each do |local_user_id|
  27. next if !self[ local_user_id ]
  28. next if data[ app_model_user ] && data[ app_model_user ][ self[ local_user_id ] ]
  29. user = User.lookup(id: self[ local_user_id ])
  30. next if !user
  31. data = user.assets(data)
  32. end
  33. data
  34. end
  35. =begin
  36. get assets and record_ids of selector
  37. model = Model.find(123)
  38. assets = model.assets_of_selector('attribute_name_of_selector', assets)
  39. =end
  40. def assets_of_selector(selector, assets = {})
  41. # get assets of condition
  42. models = Models.all
  43. send(selector).each do |item, content|
  44. attribute = item.split(/\./)
  45. next if !attribute[1]
  46. begin
  47. attribute_class = attribute[0].to_classname.constantize
  48. rescue => e
  49. next if attribute[0] == 'article'
  50. logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
  51. next
  52. end
  53. reflection = attribute[1].sub(/_id$/, '')
  54. #reflection = reflection.to_sym
  55. next if !models[attribute_class]
  56. next if !models[attribute_class][:reflections]
  57. next if !models[attribute_class][:reflections][reflection]
  58. next if !models[attribute_class][:reflections][reflection].klass
  59. attribute_ref_class = models[attribute_class][:reflections][reflection].klass
  60. if content['value'].instance_of?(Array)
  61. content['value'].each do |item_id|
  62. next if item_id.blank?
  63. attribute_object = attribute_ref_class.lookup(id: item_id)
  64. next if !attribute_object
  65. assets = attribute_object.assets(assets)
  66. end
  67. elsif content['value'].present?
  68. attribute_object = attribute_ref_class.find_by(id: content['value'])
  69. if attribute_object
  70. assets = attribute_object.assets(assets)
  71. end
  72. end
  73. end
  74. assets
  75. end
  76. # methods defined here are going to extend the class, not the instance of it
  77. class_methods do
  78. =begin
  79. return object and assets
  80. data = Model.full(123)
  81. data = {
  82. id: 123,
  83. assets: assets,
  84. }
  85. =end
  86. def full(id)
  87. object = find(id)
  88. assets = object.assets({})
  89. {
  90. id: object.id,
  91. assets: assets,
  92. }
  93. end
  94. =begin
  95. get assets of object list
  96. list = [
  97. {
  98. object => 'Ticket',
  99. o_id => 1,
  100. },
  101. {
  102. object => 'User',
  103. o_id => 121,
  104. },
  105. ]
  106. assets = Model.assets_of_object_list(list, assets)
  107. =end
  108. def assets_of_object_list(list, assets = {})
  109. list.each do |item|
  110. require item['object'].to_filename
  111. record = Kernel.const_get(item['object']).find(item['o_id'])
  112. assets = record.assets(assets)
  113. if item['created_by_id'].present?
  114. user = User.find(item['created_by_id'])
  115. assets = user.assets(assets)
  116. end
  117. if item['updated_by_id'].present?
  118. user = User.find(item['updated_by_id'])
  119. assets = user.assets(assets)
  120. end
  121. end
  122. assets
  123. end
  124. end
  125. end