history.rb 7.9 KB

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