models.rb 6.4 KB

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