From 7425ad5c4aeea43e60fb5ea4f5f7a4ea99fb8ef8 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 8 Feb 2021 17:58:29 +0000 Subject: [PATCH] Include ItemAdjustments handling in Adjustment class Currently only applies to shipments, but will later include line items, etc --- app/models/spree/adjustment.rb | 8 ++++++++ app/models/spree/item_adjustments.rb | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/spree/adjustment.rb b/app/models/spree/adjustment.rb index a0bd3e1541..164f292bbf 100644 --- a/app/models/spree/adjustment.rb +++ b/app/models/spree/adjustment.rb @@ -47,6 +47,8 @@ module Spree validates :label, presence: true validates :amount, numericality: true + after_create :update_adjustable_adjustment_total + state_machine :state, initial: :open do event :close do transition from: :open, to: :closed @@ -149,5 +151,11 @@ module Spree originator_type.constantize.unscoped { super } end + + private + + def update_adjustable_adjustment_total + Spree::ItemAdjustments.new(adjustable).update if adjustable + end end end diff --git a/app/models/spree/item_adjustments.rb b/app/models/spree/item_adjustments.rb index 1dd334f47d..aedd8f0fbf 100644 --- a/app/models/spree/item_adjustments.rb +++ b/app/models/spree/item_adjustments.rb @@ -12,7 +12,7 @@ module Spree end def update - update_adjustments if item.persisted? + update_adjustments if updatable_totals? item end @@ -31,6 +31,10 @@ module Spree private + def updatable_totals? + item.persisted? && item.is_a?(Spree::Shipment) + end + def tax_adjustments (item.respond_to?(:all_adjustments) ? item.all_adjustments : item.adjustments).tax end