From ac9ecdfcbc5e77f6ca8fee91313cf74f7f508fc3 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 7 Feb 2021 13:07:33 +0000 Subject: [PATCH] Introduce Spree::ItemAdjustments class --- app/models/spree/item_adjustments.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/models/spree/item_adjustments.rb diff --git a/app/models/spree/item_adjustments.rb b/app/models/spree/item_adjustments.rb new file mode 100644 index 0000000000..c6cb1238c4 --- /dev/null +++ b/app/models/spree/item_adjustments.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Spree + # Manage (recalculate) item (LineItem or Shipment) adjustments + class ItemAdjustments + attr_reader :item + + delegate :adjustments, :order, to: :item + + def initialize(item) + @item = item + end + + def update + update_adjustments if item.persisted? + item + end + + def update_adjustments + adjustment_total = adjustments.map(&:update!).compact.sum + + item.update_columns( + adjustment_total: adjustment_total, + updated_at: Time.zone.now + ) + end + end +end