From 6fa9780ad548fa2d16a5c98019eb638b71900a54 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 22 Dec 2020 09:53:41 +0100 Subject: [PATCH] refactor chainable/footprint protected models to use inheritance --- app/models/footprintable.rb | 31 ++++++++++++++++++++++++++++- app/models/history_value.rb | 22 +++----------------- app/models/invoice.rb | 21 +------------------ app/models/invoice_item.rb | 26 +----------------------- app/models/payment_schedule.rb | 18 +---------------- app/models/payment_schedule_item.rb | 22 +------------------- app/services/footprint_service.rb | 2 +- 7 files changed, 38 insertions(+), 104 deletions(-) diff --git a/app/models/footprintable.rb b/app/models/footprintable.rb index a3f6b7921..3034a361e 100644 --- a/app/models/footprintable.rb +++ b/app/models/footprintable.rb @@ -1,5 +1,34 @@ -module Footprintable +# 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 + + def check_footprint + footprint == compute_footprint + end + + def chain_record(sort_on = 'id') + self.footprint = compute_footprint + save! + FootprintDebug.create!( + footprint: footprint, + data: FootprintService.footprint_data(self.class, self, sort_on), + klass: self.class.name + ) + end + + def debug_footprint + FootprintService.debug_footprint(self.class, self) + end + + protected + + def compute_footprint(sort_on = 'id') + FootprintService.compute_footprint(self.class, self, sort_on) + end end diff --git a/app/models/history_value.rb b/app/models/history_value.rb index e3658b623..eb3126708 100644 --- a/app/models/history_value.rb +++ b/app/models/history_value.rb @@ -3,30 +3,14 @@ require 'checksum' # Setting values, kept history of modifications -class HistoryValue < ApplicationRecord - include Footprintable - +class HistoryValue < Footprintable belongs_to :setting belongs_to :invoicing_profile after_create :chain_record def chain_record - self.footprint = compute_footprint - save! - FootprintDebug.create!( - footprint: footprint, - data: FootprintService.footprint_data(HistoryValue, self, 'created_at'), - klass: HistoryValue.name - ) - end - - def check_footprint - footprint == compute_footprint - end - - def debug_footprint - FootprintService.debug_footprint(HistoryValue, self) + super('created_at') end def user @@ -36,6 +20,6 @@ class HistoryValue < ApplicationRecord private def compute_footprint - FootprintService.compute_footprint(HistoryValue, self, 'created_at') + super('created_at') end end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 389a2336d..40feda25e 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -4,8 +4,7 @@ require 'checksum' # Invoice correspond to a single purchase made by an user. This purchase may # include reservation(s) and/or a subscription -class Invoice < ApplicationRecord - include Footprintable +class Invoice < Footprintable include NotifyWith::NotificationAttachedObject require 'fileutils' scope :only_invoice, -> { where(type: nil) } @@ -173,24 +172,10 @@ class Invoice < ApplicationRecord self.environment = Rails.env end - def chain_record - self.footprint = compute_footprint - save! - FootprintDebug.create!( - footprint: footprint, - data: FootprintService.footprint_data(Invoice, self), - klass: Invoice.name - ) - end - def check_footprint invoice_items.map(&:check_footprint).all? && footprint == compute_footprint end - def debug_footprint - FootprintService.debug_footprint(Invoice, self) - end - def set_wallet_transaction(amount, transaction_id) raise InvalidFootprintError unless check_footprint @@ -214,10 +199,6 @@ class Invoice < ApplicationRecord InvoiceWorker.perform_async(id, user&.subscription&.expired_at) end - def compute_footprint - FootprintService.compute_footprint(Invoice, self) - end - def log_changes return if Rails.env.test? return unless changed? diff --git a/app/models/invoice_item.rb b/app/models/invoice_item.rb index f03fc8c54..91e978418 100644 --- a/app/models/invoice_item.rb +++ b/app/models/invoice_item.rb @@ -3,9 +3,7 @@ require 'checksum' # A single line inside an invoice. Can be a subscription or a reservation -class InvoiceItem < ApplicationRecord - include Footprintable - +class InvoiceItem < Footprintable belongs_to :invoice belongs_to :subscription @@ -14,24 +12,6 @@ class InvoiceItem < ApplicationRecord after_create :chain_record after_update :log_changes - def chain_record - self.footprint = compute_footprint - save! - FootprintDebug.create!( - footprint: footprint, - data: FootprintService.footprint_data(InvoiceItem, self), - klass: InvoiceItem.name - ) - end - - def check_footprint - footprint == compute_footprint - end - - def debug_footprint - FootprintService.debug_footprint(InvoiceItem, self) - end - def amount_after_coupon # deduct coupon discount coupon_service = CouponService.new @@ -53,10 +33,6 @@ class InvoiceItem < ApplicationRecord private - def compute_footprint - FootprintService.compute_footprint(InvoiceItem, self) - end - def log_changes return if Rails.env.test? return unless changed? diff --git a/app/models/payment_schedule.rb b/app/models/payment_schedule.rb index f59b931b6..1bf0c3ced 100644 --- a/app/models/payment_schedule.rb +++ b/app/models/payment_schedule.rb @@ -2,9 +2,7 @@ # PaymentSchedule is a way for members to pay something (especially a Subscription) with multiple payment, # staged on a long period rather than with a single payment -class PaymentSchedule < ApplicationRecord - include Footprintable - +class PaymentSchedule < Footprintable belongs_to :scheduled, polymorphic: true belongs_to :wallet_transaction belongs_to :coupon @@ -42,20 +40,6 @@ class PaymentSchedule < ApplicationRecord chain_record end - def chain_record - self.footprint = compute_footprint - save! - FootprintDebug.create!( - footprint: footprint, - data: FootprintService.footprint_data(PaymentSchedule, self), - klass: PaymentSchedule.name - ) - end - - def compute_footprint - FootprintService.compute_footprint(PaymentSchedule, self) - end - def check_footprint payment_schedule_items.map(&:check_footprint).all? && footprint == compute_footprint end diff --git a/app/models/payment_schedule_item.rb b/app/models/payment_schedule_item.rb index d687a1d0a..f64d81bec 100644 --- a/app/models/payment_schedule_item.rb +++ b/app/models/payment_schedule_item.rb @@ -1,31 +1,11 @@ # frozen_string_literal: true # Represents a due date and the associated amount for a PaymentSchedule -class PaymentScheduleItem < ApplicationRecord - include Footprintable - +class PaymentScheduleItem < Footprintable belongs_to :payment_schedule belongs_to :invoice after_create :chain_record - def chain_record - self.footprint = compute_footprint - save! - FootprintDebug.create!( - footprint: footprint, - data: FootprintService.footprint_data(PaymentScheduleItem, self), - klass: PaymentScheduleItem.name - ) - end - - def check_footprint - footprint == compute_footprint - end - - def compute_footprint - FootprintService.compute_footprint(PaymentScheduleItem, self) - end - def self.columns_out_of_footprint %w[invoice_id] end diff --git a/app/services/footprint_service.rb b/app/services/footprint_service.rb index 049f05450..c4d60b651 100644 --- a/app/services/footprint_service.rb +++ b/app/services/footprint_service.rb @@ -38,7 +38,7 @@ class FootprintService def self.debug_footprint(klass, item) columns = FootprintService.footprint_columns(klass) current = FootprintService.footprint_data(klass, item) - saved = FootprintDebug.find_by(footprint: item.footprint, klass: klass) + saved = FootprintDebug.find_by(footprint: item.footprint, klass: klass.name) puts "Debug footprint for #{klass} [ id: #{item.id} ]" puts '-----------------------------------------' puts "columns: [ #{columns.join(', ')} ]"