2020-12-22 09:53:41 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# SuperClass for models that are secured by chained footprints.
|
|
|
|
class Footprintable < ApplicationRecord
|
|
|
|
self.abstract_class = true
|
|
|
|
|
2020-12-21 17:37:58 +01:00
|
|
|
def self.columns_out_of_footprint
|
|
|
|
[]
|
|
|
|
end
|
2020-12-22 09:53:41 +01:00
|
|
|
|
2021-04-16 16:03:10 +02:00
|
|
|
def footprint_children
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_on_field
|
|
|
|
'id'
|
|
|
|
end
|
|
|
|
|
2020-12-22 09:53:41 +01:00
|
|
|
def check_footprint
|
2023-03-23 17:39:06 +01:00
|
|
|
return false unless persisted?
|
|
|
|
|
2023-03-24 17:21:44 +01:00
|
|
|
reload
|
2023-03-23 17:39:06 +01:00
|
|
|
footprint_children.map(&:check_footprint).all? && !chained_element.corrupted?
|
2020-12-22 09:53:41 +01:00
|
|
|
end
|
|
|
|
|
2023-03-23 17:39:06 +01:00
|
|
|
# @return [ChainedElement]
|
2021-04-16 16:03:10 +02:00
|
|
|
def chain_record
|
2023-03-23 17:39:06 +01:00
|
|
|
ChainedElement.create!(
|
|
|
|
element: self,
|
|
|
|
previous: previous_record&.chained_element
|
2020-12-22 09:53:41 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-03-23 17:39:06 +01:00
|
|
|
# @return [Footprintable,NilClass]
|
|
|
|
def previous_record
|
|
|
|
self.class.where("#{sort_on_field} < ?", self[sort_on_field])
|
|
|
|
.order("#{sort_on_field} DESC")
|
|
|
|
.limit(1)
|
|
|
|
.first
|
2020-12-22 09:53:41 +01:00
|
|
|
end
|
|
|
|
|
2023-03-23 17:39:06 +01:00
|
|
|
def debug_footprint
|
|
|
|
FootprintService.debug_footprint(self.class, self)
|
2020-12-22 09:53:41 +01:00
|
|
|
end
|
2020-12-21 17:37:58 +01:00
|
|
|
end
|