history.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class History < ApplicationModel
  3. include CanBeImported
  4. include History::Assets
  5. self.table_name = 'histories'
  6. belongs_to :history_type, class_name: 'History::Type', optional: true
  7. belongs_to :history_object, class_name: 'History::Object', optional: true
  8. belongs_to :history_attribute, class_name: 'History::Attribute', optional: true
  9. =begin
  10. add a new history entry for an object
  11. History.add(
  12. history_type: 'updated',
  13. history_object: 'Ticket',
  14. history_attribute: 'state',
  15. o_id: ticket.id,
  16. id_to: 3,
  17. id_from: 2,
  18. value_from: 'open',
  19. value_to: 'pending reminder',
  20. created_by_id: 1,
  21. created_at: '2013-06-04 10:00:00',
  22. updated_at: '2013-06-04 10:00:00'
  23. )
  24. =end
  25. def self.add(data)
  26. # return if we run import mode
  27. return if Setting.get('import_mode') && !data[:id]
  28. # lookups
  29. if data[:history_type].present?
  30. history_type = type_lookup(data[:history_type])
  31. end
  32. if data[:history_object].present?
  33. history_object = object_lookup(data[:history_object])
  34. end
  35. related_history_object_id = nil
  36. if data[:related_history_object].present?
  37. related_history_object = object_lookup(data[:related_history_object])
  38. related_history_object_id = related_history_object.id
  39. end
  40. history_attribute_id = nil
  41. if data[:history_attribute].present?
  42. history_attribute = attribute_lookup(data[:history_attribute])
  43. history_attribute_id = history_attribute.id
  44. end
  45. # create history
  46. record = {
  47. id: data[:id],
  48. o_id: data[:o_id],
  49. history_type_id: history_type.id,
  50. history_object_id: history_object.id,
  51. history_attribute_id: history_attribute_id,
  52. related_history_object_id: related_history_object_id,
  53. related_o_id: data[:related_o_id],
  54. value_from: data[:value_from],
  55. value_to: data[:value_to],
  56. id_from: data[:id_from],
  57. id_to: data[:id_to],
  58. created_at: data[:created_at],
  59. created_by_id: data[:created_by_id]
  60. }
  61. history_record = nil
  62. if data[:id]
  63. history_record = History.find_by(id: data[:id])
  64. end
  65. if history_record
  66. history_record.update!(record)
  67. else
  68. record_new = History.create!(record)
  69. if record[:id]
  70. record_new.id = record[:id]
  71. end
  72. record_new.save!
  73. end
  74. end
  75. =begin
  76. remove whole history entries of an object
  77. History.remove('Ticket', 123)
  78. =end
  79. def self.remove(requested_object, requested_object_id)
  80. history_object = History::Object.find_by(name: requested_object)
  81. return if !history_object
  82. History.where(
  83. history_object_id: history_object.id,
  84. o_id: requested_object_id,
  85. ).destroy_all
  86. end
  87. =begin
  88. return all history entries of an object
  89. history_list = History.list('Ticket', 123)
  90. returns
  91. history_list = [
  92. { ... },
  93. { ... },
  94. { ... },
  95. { ... },
  96. ]
  97. return all history entries of an object and it's related history objects
  98. history_list = History.list('Ticket', 123, 'Ticket::Article')
  99. returns
  100. history_list = [
  101. { ... },
  102. { ... },
  103. { ... },
  104. { ... },
  105. ]
  106. return all history entries of an object and it's assets
  107. history = History.list('Ticket', 123, nil, ['Ticket::Article'])
  108. returns
  109. history = {
  110. list: list,
  111. assets: assets,
  112. }
  113. =end
  114. def self.list(requested_object, requested_object_id, related_history_object = [], assets = nil)
  115. histories = History.where(
  116. history_object_id: object_lookup(requested_object).id,
  117. o_id: requested_object_id
  118. )
  119. if related_history_object.present?
  120. object_ids = []
  121. Array(related_history_object).each do |object|
  122. object_ids << object_lookup(object).id
  123. end
  124. histories = histories.or(
  125. History.where(
  126. history_object_id: object_ids,
  127. related_o_id: requested_object_id
  128. )
  129. )
  130. end
  131. histories = histories.order(:created_at, :id)
  132. list = histories.map(&:attributes).each do |data|
  133. data['object'] = History::Object.lookup(id: data.delete('history_object_id'))&.name
  134. data['type'] = History::Type.lookup(id: data.delete('history_type_id'))&.name
  135. if data['history_attribute_id']
  136. data['attribute'] = History::Attribute.lookup(id: data.delete('history_attribute_id'))&.name
  137. end
  138. if data['related_history_object_id']
  139. data['related_object'] = History::Object.lookup(id: data.delete('related_history_object_id'))&.name
  140. end
  141. data.delete('updated_at')
  142. data.delete('related_o_id') if data['related_o_id'].nil?
  143. if data['id_to'].nil? && data['id_from'].nil?
  144. data.delete('id_from')
  145. data.delete('id_to')
  146. end
  147. if data['value_to'].nil? && data['value_from'].nil?
  148. data.delete('value_from')
  149. data.delete('value_to')
  150. end
  151. end
  152. return list if !assets
  153. {
  154. list: list,
  155. assets: histories.reduce({}) { |memo, obj| obj.assets(memo) }
  156. }
  157. end
  158. def self.type_lookup(name)
  159. # lookup
  160. history_type = History::Type.lookup(name: name)
  161. return history_type if history_type
  162. # create
  163. History::Type.create!(name: name)
  164. end
  165. def self.object_lookup(name)
  166. # lookup
  167. history_object = History::Object.lookup(name: name)
  168. return history_object if history_object
  169. # create
  170. History::Object.create!(name: name)
  171. end
  172. def self.attribute_lookup(name)
  173. # lookup
  174. history_attribute = History::Attribute.lookup(name: name)
  175. return history_attribute if history_attribute
  176. # create
  177. History::Attribute.create!(name: name)
  178. end
  179. end