2019-03-20 11:01:53 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'checksum'
|
|
|
|
|
|
|
|
# Setting values, kept history of modifications
|
2018-12-17 16:02:02 +01:00
|
|
|
class HistoryValue < ActiveRecord::Base
|
|
|
|
belongs_to :setting
|
2019-06-03 16:51:43 +02:00
|
|
|
belongs_to :invoicing_profile
|
2019-03-20 11:01:53 +01:00
|
|
|
|
2019-04-23 13:04:50 +02:00
|
|
|
after_create :chain_record
|
|
|
|
|
2019-03-20 11:01:53 +01:00
|
|
|
def chain_record
|
|
|
|
self.footprint = compute_footprint
|
|
|
|
save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_footprint
|
|
|
|
footprint == compute_footprint
|
|
|
|
end
|
|
|
|
|
2019-06-03 16:51:43 +02:00
|
|
|
def user
|
|
|
|
invoicing_profile.user
|
|
|
|
end
|
|
|
|
|
2019-03-20 11:01:53 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def compute_footprint
|
|
|
|
max_date = created_at || Time.current
|
|
|
|
previous = HistoryValue.where('created_at < ?', max_date)
|
|
|
|
.order('created_at DESC')
|
|
|
|
.limit(1)
|
|
|
|
|
|
|
|
columns = HistoryValue.columns.map(&:name)
|
|
|
|
.delete_if { |c| %w[footprint updated_at].include? c }
|
|
|
|
|
|
|
|
Checksum.text("#{columns.map { |c| self[c] }.join}#{previous.first ? previous.first.footprint : ''}")
|
|
|
|
end
|
2018-12-17 16:02:02 +01:00
|
|
|
end
|