mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-01 21:47:16 +00:00
It would take ages to go through all files now and assess all belongs_to associations. So I just declare the old default and then we can move on and apply the new default for the application while these classes still use the old one. All new models will then use the new default which is the goal of this excercise and we can refactor old classes when we touch them anyway.
26 lines
621 B
Ruby
26 lines
621 B
Ruby
# frozen_string_literal: true
|
|
|
|
class Invoice < ApplicationRecord
|
|
self.belongs_to_required_by_default = false
|
|
|
|
belongs_to :order, class_name: 'Spree::Order'
|
|
serialize :data, Hash
|
|
before_validation :serialize_order
|
|
after_create :cancel_previous_invoices
|
|
default_scope { order(created_at: :desc) }
|
|
|
|
def presenter
|
|
@presenter ||= Invoice::DataPresenter.new(self)
|
|
end
|
|
|
|
def serialize_order
|
|
return data unless data.empty?
|
|
|
|
self.data = Invoice::OrderSerializer.new(order).serializable_hash
|
|
end
|
|
|
|
def cancel_previous_invoices
|
|
order.invoices.where.not(id:).update_all(cancelled: true)
|
|
end
|
|
end
|