Skip to content

Commit fbf96ff

Browse files
AlistairNormanadammathysbenjaminwilstewartHarmony Bouvier
committed
Pull out the item total updater
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 <adam@super.gd> Co-Authored-By: Benjamin Willems <benjamin@super.gd> Co-Authored-By: Andrew Stewart <andrew@super.gd> Co-Authored-By: Harmony Bouvier <harmony@super.gd> Co-Authored-By: Jared Norman <jared@super.gd> Co-Authored-By: Kendra Riga <kendar@super.gd> Co-Authored-By: Sofia Besenski <sofia@super.gd> Co-Authored-By: Chris Todorov <chris@super.gd> Co-Authored-By: Tom Van Manen <tom@super.gd> Co-Authored-By: Noah Silvera <noah@super.gd>
1 parent 0b8e26d commit fbf96ff

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

core/app/models/spree/in_memory_order_updater.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,7 @@ def update_cancellations
180180

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

189185
if persist && item.changed?
190186
item.update_columns(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Spree::ItemTotalUpdater
2+
class << self
3+
def recalculate(item)
4+
item.adjustment_total = item.adjustments
5+
.reject(&:included?)
6+
.sum(&:amount)
7+
end
8+
end
9+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe Spree::ItemTotalUpdater do
6+
describe ".recalculate" do
7+
subject { described_class.recalculate(item) }
8+
9+
let(:item) { create :line_item, adjustments: [adjustment] }
10+
let(:adjustment) { create :adjustment, amount: 1}
11+
12+
it "sets the adjustment total on the item" do
13+
expect { subject }
14+
.to change { item.adjustment_total }
15+
.from(0).to(1)
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)