1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/models/footprintable.rb

46 lines
899 B
Ruby
Raw Normal View History

# frozen_string_literal: true
# SuperClass for models that are secured by chained footprints.
class Footprintable < ApplicationRecord
self.abstract_class = true
def self.columns_out_of_footprint
[]
end
2021-04-16 16:03:10 +02:00
def footprint_children
[]
end
def sort_on_field
'id'
end
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?
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
)
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
end
2023-03-23 17:39:06 +01:00
def debug_footprint
FootprintService.debug_footprint(self.class, self)
end
end