2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
class History < ApplicationModel
|
2019-01-28 06:04:05 +00:00
|
|
|
include CanBeImported
|
2013-08-19 06:29:49 +00:00
|
|
|
include History::Assets
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
self.table_name = 'histories'
|
2018-05-05 17:03:14 +00:00
|
|
|
|
2019-07-04 11:16:55 +00:00
|
|
|
belongs_to :history_type, class_name: 'History::Type', optional: true
|
|
|
|
belongs_to :history_object, class_name: 'History::Object', optional: true
|
|
|
|
belongs_to :history_attribute, class_name: 'History::Attribute', optional: true
|
2018-04-12 14:57:37 +00:00
|
|
|
|
|
|
|
# the noop is needed since Layout/EmptyLines detects
|
|
|
|
# the block commend below wrongly as the measurement of
|
|
|
|
# the wanted indentation of the rubocop re-enabling above
|
|
|
|
def noop; end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
add a new history entry for an object
|
|
|
|
|
|
|
|
History.add(
|
2016-01-26 14:25:32 +00:00
|
|
|
history_type: 'updated',
|
|
|
|
history_object: 'Ticket',
|
|
|
|
history_attribute: 'state',
|
|
|
|
o_id: ticket.id,
|
|
|
|
id_to: 3,
|
|
|
|
id_from: 2,
|
|
|
|
value_from: 'open',
|
|
|
|
value_to: 'pending reminder',
|
|
|
|
created_by_id: 1,
|
|
|
|
created_at: '2013-06-04 10:00:00',
|
|
|
|
updated_at: '2013-06-04 10:00:00'
|
2013-06-14 08:05:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.add(data)
|
2012-07-10 08:09:58 +00:00
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
# return if we run import mode
|
2014-11-24 23:31:16 +00:00
|
|
|
return if Setting.get('import_mode') && !data[:id]
|
2013-09-29 16:40:42 +00:00
|
|
|
|
2012-07-10 08:09:58 +00:00
|
|
|
# lookups
|
2018-12-31 19:15:17 +00:00
|
|
|
if data[:history_type].present?
|
2016-01-26 14:25:32 +00:00
|
|
|
history_type = type_lookup(data[:history_type])
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2018-12-31 19:15:17 +00:00
|
|
|
if data[:history_object].present?
|
2016-01-26 14:25:32 +00:00
|
|
|
history_object = object_lookup(data[:history_object])
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
related_history_object_id = nil
|
2018-12-31 19:15:17 +00:00
|
|
|
if data[:related_history_object].present?
|
2016-01-26 14:25:32 +00:00
|
|
|
related_history_object = object_lookup(data[:related_history_object])
|
2012-07-10 08:09:58 +00:00
|
|
|
related_history_object_id = related_history_object.id
|
|
|
|
end
|
|
|
|
history_attribute_id = nil
|
2018-12-31 19:15:17 +00:00
|
|
|
if data[:history_attribute].present?
|
2016-01-26 14:25:32 +00:00
|
|
|
history_attribute = attribute_lookup(data[:history_attribute])
|
2012-07-10 08:09:58 +00:00
|
|
|
history_attribute_id = history_attribute.id
|
|
|
|
end
|
|
|
|
|
|
|
|
# create history
|
2012-12-24 13:55:43 +00:00
|
|
|
record = {
|
2018-12-19 17:31:51 +00:00
|
|
|
id: data[:id],
|
|
|
|
o_id: data[:o_id],
|
|
|
|
history_type_id: history_type.id,
|
|
|
|
history_object_id: history_object.id,
|
|
|
|
history_attribute_id: history_attribute_id,
|
2015-04-27 13:42:53 +00:00
|
|
|
related_history_object_id: related_history_object_id,
|
2018-12-19 17:31:51 +00:00
|
|
|
related_o_id: data[:related_o_id],
|
|
|
|
value_from: data[:value_from],
|
|
|
|
value_to: data[:value_to],
|
|
|
|
id_from: data[:id_from],
|
|
|
|
id_to: data[:id_to],
|
|
|
|
created_at: data[:created_at],
|
|
|
|
created_by_id: data[:created_by_id]
|
2012-12-24 13:55:43 +00:00
|
|
|
}
|
|
|
|
history_record = nil
|
|
|
|
if data[:id]
|
2016-01-26 14:25:32 +00:00
|
|
|
history_record = History.find_by(id: data[:id])
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
|
|
|
if history_record
|
2017-09-11 11:16:08 +00:00
|
|
|
history_record.update!(record)
|
2012-12-24 13:55:43 +00:00
|
|
|
else
|
2018-12-31 19:15:17 +00:00
|
|
|
record_new = History.create!(record)
|
2013-05-27 07:25:34 +00:00
|
|
|
if record[:id]
|
|
|
|
record_new.id = record[:id]
|
|
|
|
end
|
2018-12-31 19:15:17 +00:00
|
|
|
record_new.save!
|
2012-12-24 13:55:43 +00:00
|
|
|
end
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
remove whole history entries of an object
|
|
|
|
|
2016-01-26 14:25:32 +00:00
|
|
|
History.remove('Ticket', 123)
|
2013-06-14 08:05:42 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-26 14:25:32 +00:00
|
|
|
def self.remove(requested_object, requested_object_id)
|
|
|
|
history_object = History::Object.find_by(name: requested_object)
|
2013-09-28 00:07:11 +00:00
|
|
|
return if !history_object
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2013-06-04 12:52:56 +00:00
|
|
|
History.where(
|
2015-04-27 13:42:53 +00:00
|
|
|
history_object_id: history_object.id,
|
2018-12-19 17:31:51 +00:00
|
|
|
o_id: requested_object_id,
|
2013-06-04 12:52:56 +00:00
|
|
|
).destroy_all
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
=begin
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
return all history entries of an object
|
2013-06-14 08:05:42 +00:00
|
|
|
|
2016-01-26 14:25:32 +00:00
|
|
|
history_list = History.list('Ticket', 123)
|
2013-06-14 08:05:42 +00:00
|
|
|
|
2013-10-22 06:43:49 +00:00
|
|
|
returns
|
|
|
|
|
|
|
|
history_list = [
|
|
|
|
{ ... },
|
|
|
|
{ ... },
|
|
|
|
{ ... },
|
|
|
|
{ ... },
|
|
|
|
]
|
|
|
|
|
|
|
|
return all history entries of an object and it's related history objects
|
|
|
|
|
2021-03-16 08:59:32 +00:00
|
|
|
history_list = History.list('Ticket', 123, 'Ticket::Article')
|
2013-10-22 06:43:49 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
history_list = [
|
|
|
|
{ ... },
|
|
|
|
{ ... },
|
|
|
|
{ ... },
|
|
|
|
{ ... },
|
|
|
|
]
|
|
|
|
|
|
|
|
return all history entries of an object and it's assets
|
|
|
|
|
2021-03-16 08:59:32 +00:00
|
|
|
history = History.list('Ticket', 123, nil, ['Ticket::Article'])
|
2013-10-22 06:43:49 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
history = {
|
2016-01-26 14:25:32 +00:00
|
|
|
list: list,
|
|
|
|
assets: assets,
|
2013-10-22 06:43:49 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 08:05:42 +00:00
|
|
|
=end
|
|
|
|
|
2021-03-16 08:59:32 +00:00
|
|
|
def self.list(requested_object, requested_object_id, related_history_object = [], assets = nil)
|
2019-06-28 13:07:14 +00:00
|
|
|
histories = History.where(
|
|
|
|
history_object_id: object_lookup(requested_object).id,
|
|
|
|
o_id: requested_object_id
|
|
|
|
)
|
|
|
|
|
|
|
|
if related_history_object.present?
|
2021-03-16 08:59:32 +00:00
|
|
|
object_ids = []
|
|
|
|
Array(related_history_object).each do |object|
|
|
|
|
object_ids << object_lookup(object).id
|
|
|
|
end
|
|
|
|
|
2019-06-28 13:07:14 +00:00
|
|
|
histories = histories.or(
|
|
|
|
History.where(
|
2021-03-16 08:59:32 +00:00
|
|
|
history_object_id: object_ids,
|
2019-06-28 13:07:14 +00:00
|
|
|
related_o_id: requested_object_id
|
|
|
|
)
|
2015-04-30 17:51:31 +00:00
|
|
|
)
|
2012-07-10 08:09:58 +00:00
|
|
|
end
|
2013-10-22 06:43:49 +00:00
|
|
|
|
2021-02-17 16:19:01 +00:00
|
|
|
histories = histories.order(:created_at, :id)
|
2013-10-22 06:43:49 +00:00
|
|
|
|
2019-06-28 13:07:14 +00:00
|
|
|
list = histories.map(&:attributes).each do |data|
|
|
|
|
data['object'] = History::Object.lookup(id: data.delete('history_object_id'))&.name
|
|
|
|
data['type'] = History::Type.lookup(id: data.delete('history_type_id'))&.name
|
2013-09-29 16:40:42 +00:00
|
|
|
|
|
|
|
if data['history_attribute_id']
|
2019-06-28 13:07:14 +00:00
|
|
|
data['attribute'] = History::Attribute.lookup(id: data.delete('history_attribute_id'))&.name
|
|
|
|
end
|
|
|
|
|
|
|
|
if data['related_history_object_id']
|
|
|
|
data['related_object'] = History::Object.lookup(id: data.delete('related_history_object_id'))&.name
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
|
|
|
|
2016-01-26 14:25:32 +00:00
|
|
|
data.delete('updated_at')
|
2019-06-28 13:07:14 +00:00
|
|
|
data.delete('related_o_id') if data['related_o_id'].nil?
|
|
|
|
|
2015-05-07 10:11:45 +00:00
|
|
|
if data['id_to'].nil? && data['id_from'].nil?
|
2016-01-26 14:25:32 +00:00
|
|
|
data.delete('id_from')
|
2019-06-28 13:07:14 +00:00
|
|
|
data.delete('id_to')
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
2019-06-28 13:07:14 +00:00
|
|
|
|
2015-05-07 10:11:45 +00:00
|
|
|
if data['value_to'].nil? && data['value_from'].nil?
|
2016-01-26 14:25:32 +00:00
|
|
|
data.delete('value_from')
|
2019-06-28 13:07:14 +00:00
|
|
|
data.delete('value_to')
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2019-06-28 13:07:14 +00:00
|
|
|
return list if !assets
|
|
|
|
|
|
|
|
{
|
|
|
|
list: list,
|
|
|
|
assets: histories.reduce({}) { |memo, obj| obj.assets(memo) }
|
|
|
|
}
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2016-01-26 14:25:32 +00:00
|
|
|
def self.type_lookup(name)
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
2016-01-26 14:25:32 +00:00
|
|
|
history_type = History::Type.lookup(name: name)
|
2018-12-31 19:15:17 +00:00
|
|
|
return history_type if history_type
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
2018-12-31 19:15:17 +00:00
|
|
|
History::Type.create!(name: name)
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2016-01-26 14:25:32 +00:00
|
|
|
def self.object_lookup(name)
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
2016-01-26 14:25:32 +00:00
|
|
|
history_object = History::Object.lookup(name: name)
|
2018-12-31 19:15:17 +00:00
|
|
|
return history_object if history_object
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
2018-12-31 19:15:17 +00:00
|
|
|
History::Object.create!(name: name)
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-11-07 23:47:05 +00:00
|
|
|
|
2016-01-26 14:25:32 +00:00
|
|
|
def self.attribute_lookup(name)
|
2013-06-12 15:59:58 +00:00
|
|
|
# lookup
|
2016-01-26 14:25:32 +00:00
|
|
|
history_attribute = History::Attribute.lookup(name: name)
|
2018-12-31 19:15:17 +00:00
|
|
|
return history_attribute if history_attribute
|
2012-04-10 14:06:46 +00:00
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# create
|
2018-12-31 19:15:17 +00:00
|
|
|
History::Attribute.create!(name: name)
|
2013-06-12 15:59:58 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|