models.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Models
  3. include ApplicationLib
  4. =begin
  5. get list of models
  6. result = Models.all
  7. returns
  8. {
  9. Some::Classname1 => {
  10. attributes: ['id', 'name', '...'],
  11. reflections: ...model.reflections...,
  12. table: 'some_classname1s',
  13. },
  14. Some::Classname2 => {
  15. attributes: ['id', 'name', '...']
  16. reflections: ...model.reflections...
  17. table: 'some_classname2s',
  18. },
  19. }
  20. =end
  21. def self.all
  22. @all ||= begin
  23. all = {}
  24. dir = Rails.root.join('app/models').to_s
  25. tables = ActiveRecord::Base.connection.tables
  26. Dir.glob("#{dir}/**/*.rb") do |entry|
  27. next if entry.match?(%r{application_model}i)
  28. next if entry.match?(%r{channel/}i)
  29. next if entry.match?(%r{observer/}i)
  30. next if entry.match?(%r{store/provider/}i)
  31. next if entry.match?(%r{models/concerns/}i)
  32. next if entry.match?(%r{models/object_manager/attribute/validation/}i)
  33. entry.gsub!(dir, '')
  34. entry = entry.to_classname
  35. model_class = entry.constantize
  36. next if !model_class.respond_to? :new
  37. next if !model_class.respond_to? :table_name
  38. table_name = model_class.table_name # handle models where not table exists, pending migrations
  39. next if tables.exclude?(table_name)
  40. model_object = model_class.new
  41. next if !model_object.respond_to? :attributes
  42. all[model_class] = {}
  43. all[model_class][:attributes] = model_class.attribute_names
  44. all[model_class][:reflections] = model_class.reflections
  45. all[model_class][:table] = model_class.table_name
  46. # puts model_class
  47. # puts "rrrr #{all[model_class][:attributes]}"
  48. # puts " #{model_class.attribute_names.inspect}"
  49. end
  50. all
  51. end
  52. end
  53. =begin
  54. get list of searchable models for UI
  55. result = Models.searchable
  56. returns
  57. [Model1, Model2, Model3]
  58. =end
  59. def self.searchable
  60. @searchable ||= Models.all.keys.select { |model| model.respond_to?(:search_preferences) }
  61. end
  62. =begin
  63. get list of indexable models
  64. result = Models.indexable
  65. returns
  66. [Model1, Model2, Model3]
  67. =end
  68. def self.indexable
  69. @indexable ||= Models.all.keys.select { |model| model.method_defined?(:search_index_update_backend) }
  70. end
  71. =begin
  72. get reference list of a models
  73. result = Models.references('User', 2)
  74. returns
  75. {
  76. 'Some::Classname1' => {
  77. attribute1: 12,
  78. attribute2: 6,
  79. },
  80. 'Some::Classname2' => {
  81. updated_by_id: 12,
  82. created_by_id: 6,
  83. },
  84. }
  85. =end
  86. def self.references(object_name, object_id, include_zero = false)
  87. object_name = object_name.to_s
  88. # check if model exists
  89. object_name.constantize.find(object_id)
  90. list = all
  91. references = {}
  92. # find relations via attributes
  93. ref_attributes = ["#{object_name.downcase}_id"]
  94. # for users we do not define relations for created_by_id &
  95. # updated_by_id - add it here directly
  96. if object_name == 'User'
  97. ref_attributes.push 'created_by_id'
  98. ref_attributes.push 'updated_by_id'
  99. ref_attributes.push 'out_of_office_replacement_id'
  100. end
  101. list.each do |model_class, model_attributes|
  102. if !references[model_class.to_s]
  103. references[model_class.to_s] = {}
  104. end
  105. next if !model_attributes[:attributes]
  106. ref_attributes.each do |item|
  107. next if model_attributes[:attributes].exclude?(item)
  108. count = model_class.where(item => object_id).count
  109. next if count.zero? && !include_zero
  110. if !references[model_class.to_s][item]
  111. references[model_class.to_s][item] = 0
  112. end
  113. Rails.logger.debug { "FOUND (by id) #{model_class}->#{item} #{count}!" }
  114. references[model_class.to_s][item] += count
  115. end
  116. end
  117. # find relations via reflections
  118. list.each do |model_class, model_attributes| # rubocop:disable Style/CombinableLoops
  119. next if !model_attributes[:reflections]
  120. model_attributes[:reflections].each_value do |reflection_value|
  121. next if reflection_value.macro != :belongs_to
  122. col_name = "#{reflection_value.name}_id"
  123. next if ref_attributes.include?(col_name)
  124. if reflection_value.options[:class_name] == object_name
  125. count = model_class.where(col_name => object_id).count
  126. next if count.zero? && !include_zero
  127. if !references[model_class.to_s][col_name]
  128. references[model_class.to_s][col_name] = 0
  129. end
  130. Rails.logger.debug { "FOUND (by ref without class) #{model_class}->#{col_name} #{count}!" }
  131. references[model_class.to_s][col_name] += count
  132. end
  133. next if reflection_value.options[:class_name]
  134. next if reflection_value.name != object_name.downcase.to_sym
  135. count = model_class.where(col_name => object_id).count
  136. next if count.zero? && !include_zero
  137. if !references[model_class.to_s][col_name]
  138. references[model_class.to_s][col_name] = 0
  139. end
  140. Rails.logger.debug { "FOUND (by ref with class) #{model_class}->#{col_name} #{count}!" }
  141. references[model_class.to_s][col_name] += count
  142. end
  143. end
  144. # cleanup, remove models with empty references
  145. references.each do |k, v|
  146. next if v.present?
  147. references.delete(k)
  148. end
  149. references
  150. end
  151. =begin
  152. get reference total of a models
  153. count = Models.references_total('User', 2)
  154. returns
  155. count # 1234
  156. =end
  157. def self.references_total(object_name, object_id)
  158. references = references(object_name, object_id)
  159. total = 0
  160. references.each_value do |model_references|
  161. model_references.each_value do |count|
  162. total += count
  163. end
  164. end
  165. total
  166. end
  167. =begin
  168. merge model references to other model
  169. result = Models.merge('User', 2, 4711) # Object, object_id_of_primary, object_id_which_should_be_merged
  170. returns
  171. true # false
  172. =end
  173. def self.merge(object_name, object_id_primary, object_id_to_merge, force = false)
  174. # if lower x references to update, do it right now
  175. if force
  176. total = references_total(object_name, object_id_to_merge)
  177. if total > 1000
  178. raise "Can't merge object because object has more then 1000 (#{total}) references, please contact your system administrator."
  179. end
  180. end
  181. # update references
  182. references = references(object_name, object_id_to_merge)
  183. references.each do |model, attributes|
  184. model_object = model.constantize
  185. # collect items and attributes to update
  186. items_to_update = {}
  187. attributes.each_key do |attribute|
  188. Rails.logger.debug { "#{object_name}: #{model}.#{attribute}->#{object_id_to_merge}->#{object_id_primary}" }
  189. model_object.where(attribute => object_id_to_merge).each do |item|
  190. if !items_to_update[item.id]
  191. items_to_update[item.id] = item
  192. end
  193. items_to_update[item.id][attribute.to_sym] = object_id_primary
  194. end
  195. end
  196. # update items
  197. ActiveRecord::Base.transaction do
  198. items_to_update.each_value(&:save!)
  199. end
  200. end
  201. ExternalSync.migrate(object_name, object_id_primary, object_id_to_merge)
  202. true
  203. end
  204. end