history.rb 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://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. # the noop is needed since Layout/EmptyLines detects
  10. # the block commend below wrongly as the measurement of
  11. # the wanted indentation of the rubocop re-enabling above
  12. def noop; end
  13. =begin
  14. add a new history entry for an object
  15. History.add(
  16. history_type: 'updated',
  17. history_object: 'Ticket',
  18. history_attribute: 'state',
  19. o_id: ticket.id,
  20. id_to: 3,
  21. id_from: 2,
  22. value_from: 'open',
  23. value_to: 'pending reminder',
  24. created_by_id: 1,
  25. created_at: '2013-06-04 10:00:00',
  26. updated_at: '2013-06-04 10:00:00'
  27. )
  28. =end
  29. def self.add(data)
  30. # return if we run import mode
  31. return if Setting.get('import_mode') && !data[:id]
  32. # lookups
  33. if data[:history_type].present?
  34. history_type = type_lookup(data[:history_type])
  35. end
  36. if data[:history_object].present?
  37. history_object = object_lookup(data[:history_object])
  38. end
  39. related_history_object_id = nil
  40. if data[:related_history_object].present?
  41. related_history_object = object_lookup(data[:related_history_object])
  42. related_history_object_id = related_history_object.id
  43. end
  44. history_attribute_id = nil
  45. if data[:history_attribute].present?
  46. history_attribute = attribute_lookup(data[:history_attribute])
  47. history_attribute_id = history_attribute.id
  48. end
  49. # create history
  50. record = {
  51. id: data[:id],
  52. o_id: data[:o_id],
  53. history_type_id: history_type.id,
  54. history_object_id: history_object.id,
  55. history_attribute_id: history_attribute_id,
  56. related_history_object_id: related_history_object_id,
  57. related_o_id: data[:related_o_id],
  58. value_from: data[:value_from],
  59. value_to: data[:value_to],
  60. id_from: data[:id_from],
  61. id_to: data[:id_to],
  62. created_at: data[:created_at],
  63. created_by_id: data[:created_by_id]
  64. }
  65. history_record = nil
  66. if data[:id]
  67. history_record = History.find_by(id: data[:id])
  68. end
  69. if history_record
  70. history_record.update!(record)
  71. else
  72. record_new = History.create!(record)
  73. if record[:id]
  74. record_new.id = record[:id]
  75. end
  76. record_new.save!
  77. end
  78. end
  79. =begin
  80. remove whole history entries of an object
  81. History.remove('Ticket', 123)
  82. =end
  83. def self.remove(requested_object, requested_object_id)
  84. history_object = History::Object.find_by(name: requested_object)
  85. return if !history_object
  86. History.where(
  87. history_object_id: history_object.id,
  88. o_id: requested_object_id,
  89. ).destroy_all
  90. end
  91. =begin
  92. return all history entries of an object
  93. history_list = History.list('Ticket', 123)
  94. returns
  95. history_list = [
  96. { ... },
  97. { ... },
  98. { ... },
  99. { ... },
  100. ]
  101. return all history entries of an object and it's related history objects
  102. history_list = History.list('Ticket', 123, true)
  103. returns
  104. history_list = [
  105. { ... },
  106. { ... },
  107. { ... },
  108. { ... },
  109. ]
  110. return all history entries of an object and it's assets
  111. history = History.list('Ticket', 123, nil, true)
  112. returns
  113. history = {
  114. list: list,
  115. assets: assets,
  116. }
  117. =end
  118. def self.list(requested_object, requested_object_id, related_history_object = nil, assets = nil)
  119. histories = History.where(
  120. history_object_id: object_lookup(requested_object).id,
  121. o_id: requested_object_id
  122. )
  123. if related_history_object.present?
  124. histories = histories.or(
  125. History.where(
  126. history_object_id: object_lookup(related_history_object).id,
  127. related_o_id: requested_object_id
  128. )
  129. )
  130. end
  131. histories = histories.order(:created_at)
  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