can_assets.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
  50. next
  51. end
  52. reflection = attribute[1].sub(/_id$/, '')
  53. #reflection = reflection.to_sym
  54. next if !models[attribute_class]
  55. next if !models[attribute_class][:reflections]
  56. next if !models[attribute_class][:reflections][reflection]
  57. next if !models[attribute_class][:reflections][reflection].klass
  58. attribute_ref_class = models[attribute_class][:reflections][reflection].klass
  59. if content['value'].instance_of?(Array)
  60. content['value'].each do |item_id|
  61. next if item_id.blank?
  62. attribute_object = attribute_ref_class.lookup(id: item_id)
  63. next if !attribute_object
  64. assets = attribute_object.assets(assets)
  65. end
  66. elsif content['value'].present?
  67. attribute_object = attribute_ref_class.find_by(id: content['value'])
  68. if attribute_object
  69. assets = attribute_object.assets(assets)
  70. end
  71. end
  72. end
  73. assets
  74. end
  75. # methods defined here are going to extend the class, not the instance of it
  76. class_methods do
  77. =begin
  78. return object and assets
  79. data = Model.full(123)
  80. data = {
  81. id: 123,
  82. assets: assets,
  83. }
  84. =end
  85. def full(id)
  86. object = find(id)
  87. assets = object.assets({})
  88. {
  89. id: id,
  90. assets: assets,
  91. }
  92. end
  93. =begin
  94. get assets of object list
  95. list = [
  96. {
  97. object => 'Ticket',
  98. o_id => 1,
  99. },
  100. {
  101. object => 'User',
  102. o_id => 121,
  103. },
  104. ]
  105. assets = Model.assets_of_object_list(list, assets)
  106. =end
  107. def assets_of_object_list(list, assets = {})
  108. list.each do |item|
  109. require item['object'].to_filename
  110. record = Kernel.const_get(item['object']).find(item['o_id'])
  111. assets = record.assets(assets)
  112. if item['created_by_id'].present?
  113. user = User.find(item['created_by_id'])
  114. assets = user.assets(assets)
  115. end
  116. if item['updated_by_id'].present?
  117. user = User.find(item['updated_by_id'])
  118. assets = user.assets(assets)
  119. end
  120. end
  121. assets
  122. end
  123. end
  124. end