history.rb 5.9 KB

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