history.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. return all history entries of an object and it's assets and extended with an condition (e. g. to only retrive new history entries)
  118. history = History.list('Ticket', 123, nil, true, ['created_at > ?', [Time.zone.now - 2.days]])
  119. returns
  120. history = {
  121. list: list,
  122. assets: assets,
  123. }
  124. =end
  125. def self.list(requested_object, requested_object_id, related_history_object = nil, assets = nil, condition = nil)
  126. histories = History.where(
  127. history_object_id: object_lookup(requested_object).id,
  128. o_id: requested_object_id
  129. )
  130. if related_history_object.present?
  131. histories = histories.or(
  132. History.where(
  133. history_object_id: object_lookup(related_history_object).id,
  134. related_o_id: requested_object_id
  135. )
  136. )
  137. end
  138. if condition.present?
  139. histories = histories.where(condition)
  140. end
  141. histories = histories.order(:created_at)
  142. list = histories.map(&:attributes).each do |data|
  143. data['object'] = History::Object.lookup(id: data.delete('history_object_id'))&.name
  144. data['type'] = History::Type.lookup(id: data.delete('history_type_id'))&.name
  145. if data['history_attribute_id']
  146. data['attribute'] = History::Attribute.lookup(id: data.delete('history_attribute_id'))&.name
  147. end
  148. if data['related_history_object_id']
  149. data['related_object'] = History::Object.lookup(id: data.delete('related_history_object_id'))&.name
  150. end
  151. data.delete('updated_at')
  152. data.delete('related_o_id') if data['related_o_id'].nil?
  153. if data['id_to'].nil? && data['id_from'].nil?
  154. data.delete('id_from')
  155. data.delete('id_to')
  156. end
  157. if data['value_to'].nil? && data['value_from'].nil?
  158. data.delete('value_from')
  159. data.delete('value_to')
  160. end
  161. end
  162. return list if !assets
  163. {
  164. list: list,
  165. assets: histories.reduce({}) { |memo, obj| obj.assets(memo) }
  166. }
  167. end
  168. def self.type_lookup(name)
  169. # lookup
  170. history_type = History::Type.lookup(name: name)
  171. return history_type if history_type
  172. # create
  173. History::Type.create!(name: name)
  174. end
  175. def self.object_lookup(name)
  176. # lookup
  177. history_object = History::Object.lookup(name: name)
  178. return history_object if history_object
  179. # create
  180. History::Object.create!(name: name)
  181. end
  182. def self.attribute_lookup(name)
  183. # lookup
  184. history_attribute = History::Attribute.lookup(name: name)
  185. return history_attribute if history_attribute
  186. # create
  187. History::Attribute.create!(name: name)
  188. end
  189. end