history.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class History < ApplicationModel
  3. include History::Assets
  4. self.table_name = 'histories'
  5. # rubocop:disable Rails/InverseOf
  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. # rubocop:enable Rails/InverseOf
  10. # the noop is needed since Layout/EmptyLines detects
  11. # the block commend below wrongly as the measurement of
  12. # the wanted indentation of the rubocop re-enabling above
  13. def noop; end
  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!(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')
  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. History::Type.lookup(id: id)
  180. end
  181. def self.type_lookup(name)
  182. # lookup
  183. history_type = History::Type.lookup(name: name)
  184. if history_type
  185. return history_type
  186. end
  187. # create
  188. History::Type.create(
  189. name: name
  190. )
  191. end
  192. def self.object_lookup_id(id)
  193. History::Object.lookup(id: id)
  194. end
  195. def self.object_lookup(name)
  196. # lookup
  197. history_object = History::Object.lookup(name: name)
  198. if history_object
  199. return history_object
  200. end
  201. # create
  202. History::Object.create(
  203. name: name
  204. )
  205. end
  206. def self.attribute_lookup_id(id)
  207. History::Attribute.lookup(id: id)
  208. end
  209. def self.attribute_lookup(name)
  210. # lookup
  211. history_attribute = History::Attribute.lookup(name: name)
  212. if history_attribute
  213. return history_attribute
  214. end
  215. # create
  216. History::Attribute.create(
  217. name: name
  218. )
  219. end
  220. class Object < ApplicationModel
  221. end
  222. class Type < ApplicationModel
  223. end
  224. class Attribute < ApplicationModel
  225. end
  226. end