mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-01 12:24:28 +01:00
refactor chainable/footprint protected models to use inheritance
This commit is contained in:
parent
d3a41903cd
commit
6fa9780ad5
@ -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
|
||||
|
@ -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
|
||||
|
@ -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?
|
||||
|
@ -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?
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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(', ')} ]"
|
||||
|
Loading…
Reference in New Issue
Block a user