can_assets.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright (C) 2012-2021 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. next if attribute[0] == 'customer'
  51. next if attribute[0] == 'execution_time'
  52. logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
  53. next
  54. end
  55. if attribute_class == ::Notification
  56. next if content['recipient'].blank?
  57. attribute_ref_class = ::User
  58. item_ids = []
  59. Array(content['recipient']).each do |identifier|
  60. next if identifier !~ %r{\Auserid_(\d+)\z}
  61. item_ids.push($1)
  62. end
  63. else
  64. reflection = attribute[1].sub(%r{_id$}, '')
  65. next if !models[attribute_class]
  66. next if !models[attribute_class][:reflections]
  67. next if !models[attribute_class][:reflections][reflection]
  68. next if !models[attribute_class][:reflections][reflection].klass
  69. attribute_ref_class = models[attribute_class][:reflections][reflection].klass
  70. item_ids = Array(content['value'])
  71. end
  72. item_ids.each do |item_id|
  73. next if item_id.blank?
  74. attribute_object = attribute_ref_class.lookup(id: item_id)
  75. next if !attribute_object
  76. assets = attribute_object.assets(assets)
  77. end
  78. end
  79. assets
  80. end
  81. def assets_added_to?(data)
  82. data.dig(self.class.to_app_model, id).present?
  83. end
  84. # methods defined here are going to extend the class, not the instance of it
  85. class_methods do
  86. =begin
  87. return object and assets
  88. data = Model.full(123)
  89. data = {
  90. id: 123,
  91. assets: assets,
  92. }
  93. =end
  94. def full(id)
  95. object = find(id)
  96. assets = object.assets({})
  97. {
  98. id: object.id,
  99. assets: assets,
  100. }
  101. end
  102. =begin
  103. get assets of object list
  104. list = [
  105. {
  106. object => 'Ticket',
  107. o_id => 1,
  108. },
  109. {
  110. object => 'User',
  111. o_id => 121,
  112. },
  113. ]
  114. assets = Model.assets_of_object_list(list, assets)
  115. =end
  116. def assets_of_object_list(list, assets = {})
  117. list.each do |item|
  118. record = item['object'].constantize.lookup(id: item['o_id'])
  119. next if record.blank?
  120. assets = record.assets(assets)
  121. if item['created_by_id'].present?
  122. user = User.find(item['created_by_id'])
  123. assets = user.assets(assets)
  124. end
  125. if item['updated_by_id'].present?
  126. user = User.find(item['updated_by_id'])
  127. assets = user.assets(assets)
  128. end
  129. end
  130. assets
  131. end
  132. end
  133. =begin
  134. Compiles an assets hash for given items
  135. @param items [Array<CanAssets>] list of items responding to @see #assets
  136. @param data [Hash] given collection. Empty {} or assets collection in progress
  137. @param suffix [String] try to use non-default assets method
  138. @return [Hash] collection including assets of items
  139. @example
  140. list = Ticket.all
  141. ApplicationModel::CanAssets.reduce(list, {})
  142. =end
  143. def self.reduce(items, data = {}, suffix = nil)
  144. items.reduce(data) do |memo, elem|
  145. method_name = if suffix.present? && elem.respond_to?("assets_#{suffix}")
  146. "assets_#{suffix}"
  147. else
  148. :assets
  149. end
  150. elem.send method_name, memo
  151. end
  152. end
  153. end