history.rb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class History < ApplicationModel
  3. load 'history/assets.rb'
  4. include History::Assets
  5. self.table_name = 'histories'
  6. belongs_to :history_type, class_name: 'History::Type'
  7. belongs_to :history_object, class_name: 'History::Object'
  8. belongs_to :history_attribute, class_name: 'History::Attribute'
  9. # before_validation :check_type, :check_object
  10. # attr_writer :history_type, :history_object
  11. # rubocop:disable Style/ClassVars
  12. @@cache_type = {}
  13. @@cache_object = {}
  14. @@cache_attribute = {}
  15. # rubocop:enable Style/ClassVars
  16. =begin
  17. add a new history entry for an object
  18. History.add(
  19. history_type: 'updated',
  20. history_object: 'Ticket',
  21. history_attribute: 'state',
  22. o_id: ticket.id,
  23. id_to: 3,
  24. id_from: 2,
  25. value_from: 'open',
  26. value_to: 'pending reminder',
  27. created_by_id: 1,
  28. created_at: '2013-06-04 10:00:00',
  29. updated_at: '2013-06-04 10:00:00'
  30. )
  31. =end
  32. def self.add(data)
  33. # return if we run import mode
  34. return if Setting.get('import_mode') && !data[:id]
  35. # lookups
  36. if data[:history_type]
  37. history_type = type_lookup(data[:history_type])
  38. end
  39. if data[:history_object]
  40. history_object = object_lookup(data[:history_object])
  41. end
  42. related_history_object_id = nil
  43. if data[:related_history_object]
  44. related_history_object = object_lookup(data[:related_history_object])
  45. related_history_object_id = related_history_object.id
  46. end
  47. history_attribute_id = nil
  48. if data[:history_attribute]
  49. history_attribute = attribute_lookup(data[:history_attribute])
  50. history_attribute_id = history_attribute.id
  51. end
  52. # create history
  53. record = {
  54. id: data[:id],
  55. o_id: data[:o_id],
  56. history_type_id: history_type.id,
  57. history_object_id: history_object.id,
  58. history_attribute_id: history_attribute_id,
  59. related_history_object_id: related_history_object_id,
  60. related_o_id: data[:related_o_id],
  61. value_from: data[:value_from],
  62. value_to: data[:value_to],
  63. id_from: data[:id_from],
  64. id_to: data[:id_to],
  65. created_at: data[:created_at],
  66. created_by_id: data[:created_by_id]
  67. }
  68. history_record = nil
  69. if data[:id]
  70. history_record = History.find_by(id: data[:id])
  71. end
  72. if history_record
  73. history_record.update_attributes(record)
  74. else
  75. record_new = History.create(record)
  76. if record[:id]
  77. record_new.id = record[:id]
  78. end
  79. record_new.save
  80. end
  81. end
  82. =begin
  83. remove whole history entries of an object
  84. History.remove('Ticket', 123)
  85. =end
  86. def self.remove(requested_object, requested_object_id)
  87. history_object = History::Object.find_by(name: requested_object)
  88. return if !history_object
  89. History.where(
  90. history_object_id: history_object.id,
  91. o_id: requested_object_id,
  92. ).destroy_all
  93. end
  94. =begin
  95. return all history entries of an object
  96. history_list = History.list('Ticket', 123)
  97. returns
  98. history_list = [
  99. { ... },
  100. { ... },
  101. { ... },
  102. { ... },
  103. ]
  104. return all history entries of an object and it's related history objects
  105. history_list = History.list('Ticket', 123, true)
  106. returns
  107. history_list = [
  108. { ... },
  109. { ... },
  110. { ... },
  111. { ... },
  112. ]
  113. return all history entries of an object and it's assets
  114. history = History.list('Ticket', 123, nil, true)
  115. returns
  116. history = {
  117. list: list,
  118. assets: assets,
  119. }
  120. =end
  121. def self.list(requested_object, requested_object_id, related_history_object = nil, assets = nil)
  122. if !related_history_object
  123. history_object = object_lookup(requested_object)
  124. history = History.where(history_object_id: history_object.id)
  125. .where(o_id: requested_object_id)
  126. .order('created_at ASC, id ASC')
  127. else
  128. history_object_requested = object_lookup(requested_object)
  129. history_object_related = object_lookup(related_history_object)
  130. history = History.where(
  131. '((history_object_id = ? AND o_id = ?) OR (history_object_id = ? AND related_o_id = ? ))',
  132. history_object_requested.id,
  133. requested_object_id,
  134. history_object_related.id,
  135. requested_object_id,
  136. )
  137. .order('created_at ASC, id ASC')
  138. end
  139. asset_list = {}
  140. list = []
  141. history.each do |item|
  142. if assets
  143. asset_list = item.assets(asset_list)
  144. end
  145. data = item.attributes
  146. data['object'] = object_lookup_id(data['history_object_id']).name
  147. data['type'] = type_lookup_id(data['history_type_id']).name
  148. data.delete('history_object_id')
  149. data.delete('history_type_id')
  150. if data['history_attribute_id']
  151. data['attribute'] = attribute_lookup_id(data['history_attribute_id']).name
  152. end
  153. data.delete('history_attribute_id')
  154. data.delete('updated_at')
  155. if data['id_to'].nil? && data['id_from'].nil?
  156. data.delete('id_to')
  157. data.delete('id_from')
  158. end
  159. if data['value_to'].nil? && data['value_from'].nil?
  160. data.delete('value_to')
  161. data.delete('value_from')
  162. end
  163. if !data['related_history_object_id'].nil?
  164. data['related_object'] = object_lookup_id(data['related_history_object_id']).name
  165. end
  166. data.delete('related_history_object_id')
  167. if data['related_o_id'].nil?
  168. data.delete('related_o_id')
  169. end
  170. list.push data
  171. end
  172. if assets
  173. return {
  174. list: list,
  175. assets: asset_list,
  176. }
  177. end
  178. list
  179. end
  180. def self.type_lookup_id(id)
  181. # use cache
  182. return @@cache_type[ id ] if @@cache_type[ id ]
  183. # lookup
  184. history_type = History::Type.lookup(id: id)
  185. @@cache_type[ id ] = history_type
  186. history_type
  187. end
  188. def self.type_lookup(name)
  189. # use cache
  190. return @@cache_type[ name ] if @@cache_type[ name ]
  191. # lookup
  192. history_type = History::Type.lookup(name: name)
  193. if history_type
  194. @@cache_type[ name ] = history_type
  195. return history_type
  196. end
  197. # create
  198. history_type = History::Type.create(
  199. name: name
  200. )
  201. @@cache_type[ name ] = history_type
  202. history_type
  203. end
  204. def self.object_lookup_id(id)
  205. # use cache
  206. return @@cache_object[ id ] if @@cache_object[ id ]
  207. # lookup
  208. history_object = History::Object.lookup(id: id)
  209. @@cache_object[ id ] = history_object
  210. history_object
  211. end
  212. def self.object_lookup(name)
  213. # use cache
  214. return @@cache_object[ name ] if @@cache_object[ name ]
  215. # lookup
  216. history_object = History::Object.lookup(name: name)
  217. if history_object
  218. @@cache_object[ name ] = history_object
  219. return history_object
  220. end
  221. # create
  222. history_object = History::Object.create(
  223. name: name
  224. )
  225. @@cache_object[ name ] = history_object
  226. history_object
  227. end
  228. def self.attribute_lookup_id(id)
  229. # use cache
  230. return @@cache_attribute[ id ] if @@cache_attribute[ id ]
  231. # lookup
  232. history_attribute = History::Attribute.lookup(id: id)
  233. @@cache_attribute[ id ] = history_attribute
  234. history_attribute
  235. end
  236. def self.attribute_lookup(name)
  237. # use cache
  238. return @@cache_attribute[ name ] if @@cache_attribute[ name ]
  239. # lookup
  240. history_attribute = History::Attribute.lookup(name: name)
  241. if history_attribute
  242. @@cache_attribute[ name ] = history_attribute
  243. return history_attribute
  244. end
  245. # create
  246. history_attribute = History::Attribute.create(
  247. name: name
  248. )
  249. @@cache_attribute[ name ] = history_attribute
  250. history_attribute
  251. end
  252. class Object < ApplicationModel
  253. end
  254. class Type < ApplicationModel
  255. end
  256. class Attribute < ApplicationModel
  257. end
  258. end