Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Optimize Order#pre_tax_item_amount and add Order#display_pre_tax_item_amount

Damian Legawiec0418874a7a4f
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 39909531bd..ad6ef9dfca 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -22,7 +22,7 @@ module Spree
money_methods :outstanding_balance, :item_total, :adjustment_total,
:included_tax_total, :additional_tax_total, :tax_total,
:shipment_total, :promo_total, :total,
- :cart_promo_total
+ :cart_promo_total, :pre_tax_item_amount
alias display_ship_total display_shipment_total
alias_attribute :ship_total, :shipment_total
@@ -174,7 +174,7 @@ module Spree
# Sum of all line item amounts pre-tax
def pre_tax_item_amount
- line_items.to_a.sum(&:pre_tax_amount)
+ line_items.sum(:pre_tax_amount)
end
def shipping_discount
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 8e820daf37..61e815b57f 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -839,13 +839,25 @@ describe Spree::Order, type: :model do
end
describe '#pre_tax_item_amount' do
+ let(:order) { create(:order) }
+
+ before do
+ line_item = create(:line_item, order: order, price: 10, quantity: 2)
+ line_item_2 = create(:line_item, order: order, price: 30, quantity: 1)
+
+ line_item.update(pre_tax_amount: 5.0)
+ line_item_2.update(pre_tax_amount: 14.0)
+ end
+
it "sums all of the line items' pre tax amounts" do
- subject.line_items = [
- Spree::LineItem.new(price: 10, quantity: 2, pre_tax_amount: 5.0),
- Spree::LineItem.new(price: 30, quantity: 1, pre_tax_amount: 14.0)
- ]
+ expect(order.pre_tax_item_amount).to eq BigDecimal(19)
+ end
+ end
- expect(subject.pre_tax_item_amount).to eq 19.0
+ describe '#display_pre_tax_item_amount' do
+ it 'returns the value as a spree money' do
+ allow(order).to receive(:pre_tax_item_amount).and_return(10.55)
+ expect(order.display_pre_tax_item_amount).to eq(Spree::Money.new(10.55))
end
end