Skip to content

Commit

Permalink
Pull out the item total updater
Browse files Browse the repository at this point in the history
We want to start breaking out some of the complex logic of the in memory
updater into smaller more focused classes.

Co-Authored-By: Adam Mueller <[email protected]>
Co-Authored-By: Benjamin Willems <[email protected]>
Co-Authored-By: Andrew Stewart <[email protected]>
Co-Authored-By: Harmony Bouvier <[email protected]>
Co-Authored-By: Jared Norman <[email protected]>
Co-Authored-By: Kendra Riga <[email protected]>
Co-Authored-By: Sofia Besenski <[email protected]>
Co-Authored-By: Chris Todorov <[email protected]>
Co-Authored-By: Tom Van Manen <[email protected]>
Co-Authored-By: Noah Silvera <[email protected]>
  • Loading branch information
11 people committed Dec 19, 2024
1 parent 0b8e26d commit fbf96ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 1 addition & 5 deletions core/app/models/spree/in_memory_order_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ def update_cancellations

def update_item_totals(persist:)
[*line_items, *shipments].each do |item|
# The cancellation_total isn't persisted anywhere but is included in
# the adjustment_total
item.adjustment_total = item.adjustments.
reject(&:included?).
sum(&:amount)
Spree::ItemTotalUpdater.recalculate(item)

if persist && item.changed?
item.update_columns(
Expand Down
9 changes: 9 additions & 0 deletions core/app/models/spree/item_total_updater.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Spree::ItemTotalUpdater
class << self
def recalculate(item)
item.adjustment_total = item.adjustments
.reject(&:included?)
.sum(&:amount)
end
end
end
18 changes: 18 additions & 0 deletions core/spec/models/spree/item_total_updater_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Spree::ItemTotalUpdater do
describe ".recalculate" do
subject { described_class.recalculate(item) }

let(:item) { create :line_item, adjustments: [adjustment] }
let(:adjustment) { create :adjustment, amount: 1}

it "sets the adjustment total on the item" do
expect { subject }
.to change { item.adjustment_total }
.from(0).to(1)
end
end
end

0 comments on commit fbf96ff

Please sign in to comment.