history.rb 6.5 KB

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