diff --git a/backend/app/controllers/spree/admin/adjustments_controller.rb b/backend/app/controllers/spree/admin/adjustments_controller.rb index 50a5c553a71..611ec539adc 100644 --- a/backend/app/controllers/spree/admin/adjustments_controller.rb +++ b/backend/app/controllers/spree/admin/adjustments_controller.rb @@ -25,7 +25,7 @@ def find_adjustment end def update_totals - @order.reload.update! + @order.reload.recalculate end # Override method used to create a new instance to correctly diff --git a/backend/spec/features/admin/orders/adjustments_spec.rb b/backend/spec/features/admin/orders/adjustments_spec.rb index 312394f734c..0a85c84f763 100644 --- a/backend/spec/features/admin/orders/adjustments_spec.rb +++ b/backend/spec/features/admin/orders/adjustments_spec.rb @@ -22,7 +22,7 @@ let!(:adjustment) { order.adjustments.create!(order: order, label: 'Rebate', amount: 10) } before(:each) do - order.update! + order.recalculate visit spree.admin_path click_link "Orders" diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb index 4fd8fdd062a..32344c2804a 100644 --- a/core/app/models/spree/order.rb +++ b/core/app/models/spree/order.rb @@ -254,10 +254,19 @@ def updater @updater ||= Spree::OrderUpdater.new(self) end - def update! + def recalculate updater.update end + def update!(*args) + if args.empty? + Spree::Deprecation.warn "Calling order.update! with no arguments as a way to invoke the OrderUpdater is deprecated, since it conflicts with AR::Base#update! Please use order.recalculate instead" + recalculate + else + super + end + end + def assign_billing_to_shipping_address self.ship_address = bill_address if bill_address true @@ -471,7 +480,7 @@ def empty! shipments.destroy_all order_promotions.destroy_all - update! + recalculate end alias_method :has_step?, :has_checkout_step? @@ -529,7 +538,7 @@ def create_proposed_shipments def apply_free_shipping_promotions Spree::PromotionHandler::FreeShipping.new(self).activate - update! + recalculate end # Clean shipments and make order back to address state @@ -565,7 +574,7 @@ def shipping_eq_billing_address? def set_shipments_cost shipments.each(&:update_amounts) - update! + recalculate end deprecate set_shipments_cost: :update!, deprecator: Spree::Deprecation @@ -806,7 +815,7 @@ def ensure_promotions_eligible end if adjustment_changed restart_checkout_flow - update! + recalculate errors.add(:base, Spree.t(:promotion_total_changed_before_complete)) end errors.empty? @@ -838,7 +847,7 @@ def after_cancel payments.store_credits.pending.each(&:void_transaction!) send_cancel_email - update! + recalculate end def send_cancel_email diff --git a/core/app/models/spree/order/checkout.rb b/core/app/models/spree/order/checkout.rb index f0b97ec1fde..d8650a9021b 100644 --- a/core/app/models/spree/order/checkout.rb +++ b/core/app/models/spree/order/checkout.rb @@ -120,7 +120,7 @@ def define_state_machine! after_transition to: :canceled, do: :after_cancel after_transition from: any - :cart, to: any - [:confirm, :complete] do |order| - order.update! + order.recalculate end after_transition do |order, transition| diff --git a/core/app/models/spree/order_cancellations.rb b/core/app/models/spree/order_cancellations.rb index a7bb043db83..aa27b2f679a 100644 --- a/core/app/models/spree/order_cancellations.rb +++ b/core/app/models/spree/order_cancellations.rb @@ -41,7 +41,7 @@ def short_ship(inventory_units, whodunnit:nil) Spree::OrderMailer.inventory_cancellation_email(@order, inventory_units.to_a).deliver_later if Spree::OrderCancellations.send_cancellation_mailer end - @order.update! + @order.recalculate if short_ship_tax_notifier short_ship_tax_notifier.call(unit_cancels) diff --git a/core/app/models/spree/order_shipping.rb b/core/app/models/spree/order_shipping.rb index e87b5509743..c3690a451d5 100644 --- a/core/app/models/spree/order_shipping.rb +++ b/core/app/models/spree/order_shipping.rb @@ -73,7 +73,7 @@ def ship(inventory_units:, stock_location:, address:, shipping_method:, send_shipment_emails(carton) if stock_location.fulfillable? && !suppress_mailer # e.g. digital gift cards that aren't actually shipped fulfill_order_stock_locations(stock_location) - @order.update! + @order.recalculate carton end diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb index bbfa8e9aafc..115a0177bbc 100644 --- a/core/app/models/spree/payment.rb +++ b/core/app/models/spree/payment.rb @@ -240,7 +240,7 @@ def invalidate_old_payments def update_order if order.completed? || completed? || void? - order.update! + order.recalculate end end diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb index 61521a98936..025c0683228 100644 --- a/core/app/models/spree/promotion.rb +++ b/core/app/models/spree/promotion.rb @@ -119,7 +119,7 @@ def activate(order:, line_item: nil, user: nil, path: nil, promotion_code: nil) action_taken end - # called anytime order.update! happens + # called anytime order.recalculate happens def eligible?(promotable, promotion_code: nil) return false if inactive? return false if usage_limit_exceeded? diff --git a/core/app/models/spree/promotion_handler/coupon.rb b/core/app/models/spree/promotion_handler/coupon.rb index cd752a5a0c5..08eb729c2bc 100644 --- a/core/app/models/spree/promotion_handler/coupon.rb +++ b/core/app/models/spree/promotion_handler/coupon.rb @@ -95,7 +95,7 @@ def determine_promotion_application_result discount ||= order.adjustments.promotion.detect(&detector) if discount && discount.eligible - order.update! + order.recalculate set_success_code :coupon_code_applied elsif order.promotions.with_coupon_code(order.coupon_code) # if the promotion exists on an order, but wasn't found above, diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb index 50a737f6987..1f5bb961ecf 100644 --- a/core/app/models/spree/shipment.rb +++ b/core/app/models/spree/shipment.rb @@ -297,9 +297,9 @@ def update_attributes_and_order(params = {}) if update_attributes params if params.key? :selected_shipping_rate_id # Changing the selected Shipping Rate won't update the cost (for now) - # so we persist the Shipment#cost before running `order.update!` + # so we persist the Shipment#cost before running `order.recalculate` update_amounts - order.update! + order.recalculate end true diff --git a/core/lib/spree/testing_support/factories/order_factory.rb b/core/lib/spree/testing_support/factories/order_factory.rb index 63eababb097..33f487c4318 100644 --- a/core/lib/spree/testing_support/factories/order_factory.rb +++ b/core/lib/spree/testing_support/factories/order_factory.rb @@ -53,7 +53,7 @@ create(:shipment, order: order, cost: evaluator.shipment_cost, shipping_method: evaluator.shipping_method, stock_location: evaluator.stock_location) order.shipments.reload - order.update! + order.recalculate end factory :completed_order_with_promotion do diff --git a/core/spec/models/spree/order/checkout_spec.rb b/core/spec/models/spree/order/checkout_spec.rb index 125e2ff336e..ea70b0d6877 100644 --- a/core/spec/models/spree/order/checkout_spec.rb +++ b/core/spec/models/spree/order/checkout_spec.rb @@ -328,7 +328,7 @@ def assert_state_changed(order, from, to) context "with a shipment that has a price" do before do shipment.shipping_rates.first.update_column(:cost, 10) - order.update! + order.recalculate end it "transitions to payment" do @@ -595,7 +595,7 @@ def assert_state_changed(order, from, to) it 'can complete the order' do create(:payment, state: 'completed', order: order, amount: order.total) - order.update! + order.recalculate expect(order.complete).to eq(true) end end diff --git a/core/spec/models/spree/order/outstanding_balance_integration_spec.rb b/core/spec/models/spree/order/outstanding_balance_integration_spec.rb index 2cf9bc2125e..3ec709ec3bc 100644 --- a/core/spec/models/spree/order/outstanding_balance_integration_spec.rb +++ b/core/spec/models/spree/order/outstanding_balance_integration_spec.rb @@ -18,7 +18,7 @@ subject do order.reload - order.update! + order.recalculate order.outstanding_balance end diff --git a/core/spec/models/spree/order/payment_spec.rb b/core/spec/models/spree/order/payment_spec.rb index 2c1daa9b64d..5279d019004 100644 --- a/core/spec/models/spree/order/payment_spec.rb +++ b/core/spec/models/spree/order/payment_spec.rb @@ -176,7 +176,7 @@ module Spree payment: reimbursement.order.payments.first, reimbursement: reimbursement # Update the order totals so payment_total goes to 0 reflecting the refund.. - order.update! + order.recalculate end context "for canceled orders" do diff --git a/core/spec/models/spree/order/updating_spec.rb b/core/spec/models/spree/order/updating_spec.rb index 0772d4c35d3..d850b6c7775 100644 --- a/core/spec/models/spree/order/updating_spec.rb +++ b/core/spec/models/spree/order/updating_spec.rb @@ -9,7 +9,7 @@ after { Spree::Order.update_hooks.clear } it "should call each of the update hooks" do expect(order).to receive :foo - order.update! + order.recalculate end end end diff --git a/core/spec/models/spree/order_cancellations_spec.rb b/core/spec/models/spree/order_cancellations_spec.rb index b46961b53f7..18ea763a402 100644 --- a/core/spec/models/spree/order_cancellations_spec.rb +++ b/core/spec/models/spree/order_cancellations_spec.rb @@ -148,7 +148,7 @@ source: promotion_action, finalized: true, ) - order.update! + order.recalculate end it "generates the correct total amount" do diff --git a/core/spec/models/spree/order_capturing_spec.rb b/core/spec/models/spree/order_capturing_spec.rb index 9d358bde196..c61dcd06ea4 100644 --- a/core/spec/models/spree/order_capturing_spec.rb +++ b/core/spec/models/spree/order_capturing_spec.rb @@ -47,7 +47,7 @@ before do order.contents.add(variant, 3) - order.update! + order.recalculate @secondary_bogus_payment = create(:payment, order: order, amount: secondary_total, payment_method: secondary_payment_method.create!(name: 'So bogus')) @bogus_payment = create(:payment, order: order, amount: bogus_total) order.contents.advance diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index 982420f1a82..eb53d152be5 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -180,7 +180,7 @@ create(:shipment, order: order) create(:adjustment, adjustable: order, order: order) promotion.activate(order: order, promotion_code: code) - order.update! + order.recalculate # Make sure we are asserting changes expect(order.line_items).not_to be_empty @@ -313,7 +313,7 @@ def merge!(other_order, user = nil) it "calls hook during update" do order = create(:order) expect(order).to receive(:add_awesome_sauce) - order.update! + order.recalculate end it "calls hook during finalize" do @@ -1182,7 +1182,7 @@ def generate context "there is enough store credit to pay for the entire order" do let(:store_credit) { create(:store_credit, amount: order_total) } - let(:order) { create(:order_with_totals, user: store_credit.user, line_items_price: order_total).tap(&:update!) } + let(:order) { create(:order_with_totals, user: store_credit.user, line_items_price: order_total).tap(&:recalculate) } context "there are no other payments" do before do @@ -1210,7 +1210,7 @@ def generate let(:order_total) { 500 } let(:store_credit_total) { order_total - 100 } let(:store_credit) { create(:store_credit, amount: store_credit_total) } - let(:order) { create(:order_with_totals, user: store_credit.user, line_items_price: order_total).tap(&:update!) } + let(:order) { create(:order_with_totals, user: store_credit.user, line_items_price: order_total).tap(&:recalculate) } context "there are no other payments" do it "adds an error to the model" do @@ -1255,7 +1255,7 @@ def generate let(:amount_difference) { 100 } let!(:primary_store_credit) { create(:store_credit, amount: (order_total - amount_difference)) } let!(:secondary_store_credit) { create(:store_credit, amount: order_total, user: primary_store_credit.user, credit_type: create(:secondary_credit_type)) } - let(:order) { create(:order_with_totals, user: primary_store_credit.user, line_items_price: order_total).tap(&:update!) } + let(:order) { create(:order_with_totals, user: primary_store_credit.user, line_items_price: order_total).tap(&:recalculate) } before do subject diff --git a/core/spec/models/spree/order_updater_spec.rb b/core/spec/models/spree/order_updater_spec.rb index b32bb6b46d1..17b6db80dec 100644 --- a/core/spec/models/spree/order_updater_spec.rb +++ b/core/spec/models/spree/order_updater_spec.rb @@ -92,13 +92,13 @@ module Spree before do promotion.activate(order: order) - order.update! + order.recalculate line_item.update!(quantity: 2) end it 'updates the promotion amount' do expect { - order.update! + order.recalculate }.to change { line_item.promo_total }.from(-1).to(-2) @@ -117,7 +117,7 @@ def initialize(_adjustments) end it 'uses the defined promotion chooser' do - expect { order.update! }.to raise_error('Custom promotion chooser') + expect { order.recalculate }.to raise_error('Custom promotion chooser') end end @@ -156,7 +156,7 @@ def create_adjustment(label, amount) label: 'Some other credit') line_item.adjustments.each { |a| a.update_column(:eligible, true) } - order.update! + order.recalculate expect(line_item.adjustments.promotion.eligible.count).to eq(1) expect(line_item.adjustments.promotion.eligible.first.label).to eq('Promotion C') @@ -170,7 +170,7 @@ def create_adjustment(label, amount) end line_item.adjustments.each { |a| a.update_column(:eligible, true) } - order.update! + order.recalculate expect(line_item.adjustments.promotion.eligible.count).to eq(1) expect(line_item.adjustments.promotion.eligible.first.label).to eq('Promotion B') @@ -184,7 +184,7 @@ def create_adjustment(label, amount) end line_item.adjustments.each { |a| a.update_column(:eligible, true) } - order.update! + order.recalculate expect(line_item.adjustments.promotion.eligible.count).to eq(1) expect(line_item.adjustments.promotion.eligible.first.label).to eq('Promotion B') @@ -212,7 +212,7 @@ def create_adjustment(label, amount) order_promos[promo_sequence[0]].activate order: order order_promos[promo_sequence[1]].activate order: order - order.update! + order.recalculate order.reload expect(order.all_adjustments.count).to eq(2), 'Expected two adjustments' expect(order.all_adjustments.eligible.count).to eq(1), 'Expected one elegible adjustment' @@ -263,7 +263,7 @@ def create_adjustment(label, amount) # regression for https://github.com/spree/spree/issues/3274 it 'still makes the previous best eligible adjustment valid' do - order.update! + order.recalculate expect(line_item.adjustments.promotion.eligible.first.label).to eq('Promotion A') end end @@ -273,7 +273,7 @@ def create_adjustment(label, amount) create_adjustment('Promotion B', -200) create_adjustment('Promotion C', -200) - order.update! + order.recalculate expect(line_item.adjustments.promotion.eligible.count).to eq(1) expect(line_item.adjustments.promotion.eligible.first.amount.to_i).to eq(-200) @@ -305,7 +305,7 @@ def create_adjustment(label, amount) it 'updates the promotion amount' do expect { - order.update! + order.recalculate }.to change { line_item.additional_tax_total }.from(1).to(2) @@ -327,7 +327,7 @@ def create_adjustment(label, amount) Spree::Tax::OrderTax.new(order_id: order.id, line_item_taxes: [], shipment_taxes: []) ) - order.update! + order.recalculate end end end @@ -535,7 +535,7 @@ def create_adjustment(label, amount) expect(line_item.promo_total).to eq(0) expect(order.promo_total).to eq(0) - order.update! + order.recalculate expect(line_item.promo_total).to eq(-1) expect(order.promo_total).to eq(-1) @@ -548,7 +548,7 @@ def create_adjustment(label, amount) it "updates the totals" do line_item.update!(adjustment_total: 100) expect { - order.update! + order.recalculate }.to change { line_item.reload.adjustment_total }.from(100).to(0) end end diff --git a/core/spec/models/spree/promotion/actions/create_quantity_adjustments_spec.rb b/core/spec/models/spree/promotion/actions/create_quantity_adjustments_spec.rb index 27add6640cf..740a6317263 100644 --- a/core/spec/models/spree/promotion/actions/create_quantity_adjustments_spec.rb +++ b/core/spec/models/spree/promotion/actions/create_quantity_adjustments_spec.rb @@ -235,7 +235,7 @@ module Spree::Promotion::Actions before do action.perform(order: order, promotion: promotion) - order.update! + order.recalculate end it 'updates the order totals' do @@ -248,7 +248,7 @@ module Spree::Promotion::Actions context "after updating item quantity" do before do order.line_items.first.update!(quantity: 2, price: 30) - order.update! + order.recalculate end it 'updates the order totals' do @@ -262,7 +262,7 @@ module Spree::Promotion::Actions context "after updating promotion amount" do before do calculator.update!(preferred_amount: 5) - order.update! + order.recalculate end it 'updates the order totals' do diff --git a/core/spec/models/spree/promotion_handler/cart_spec.rb b/core/spec/models/spree/promotion_handler/cart_spec.rb index 0d53f0cae34..e95b3d1cbb2 100644 --- a/core/spec/models/spree/promotion_handler/cart_spec.rb +++ b/core/spec/models/spree/promotion_handler/cart_spec.rb @@ -110,7 +110,7 @@ module PromotionHandler before do Spree::OrderPromotion.create!(promotion: promotion, order: order, promotion_code: promotion_code) - order.update! + order.recalculate end include_context "creates the adjustment" diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb index 3c17ae4abca..160015d6b3f 100644 --- a/core/spec/models/spree/promotion_spec.rb +++ b/core/spec/models/spree/promotion_spec.rb @@ -764,7 +764,7 @@ context 'when the user has used this promo' do before do promotion.activate(order: order) - order.update! + order.recalculate order.completed_at = Time.current order.save! end @@ -817,7 +817,7 @@ expect(order.adjustment_total).to eq 0 promo.activate order: order, promotion_code: promotion_code - order.update! + order.recalculate expect(line_item.adjustments.size).to eq(1) expect(order.adjustment_total).to eq(-5) diff --git a/core/spec/models/spree/reimbursement_spec.rb b/core/spec/models/spree/reimbursement_spec.rb index 2e40d978c2e..58000b6abfe 100644 --- a/core/spec/models/spree/reimbursement_spec.rb +++ b/core/spec/models/spree/reimbursement_spec.rb @@ -73,7 +73,7 @@ shipment.update_column('state', 'shipped') end order.reload - order.update! + order.recalculate if payment payment.save! order.next! # confirm diff --git a/core/spec/models/spree/stock/coordinator_spec.rb b/core/spec/models/spree/stock/coordinator_spec.rb index 6f4bb8b441b..b98367e566a 100644 --- a/core/spec/models/spree/stock/coordinator_spec.rb +++ b/core/spec/models/spree/stock/coordinator_spec.rb @@ -41,7 +41,7 @@ module Stock it "does not unintentionally add shipments to the order" do subject.shipments expect { - order.update! + order.recalculate }.not_to change { order.shipments.count } diff --git a/core/spec/models/spree/tax/taxation_integration_spec.rb b/core/spec/models/spree/tax/taxation_integration_spec.rb index 1b2788fdf91..0184db4aa07 100644 --- a/core/spec/models/spree/tax/taxation_integration_spec.rb +++ b/core/spec/models/spree/tax/taxation_integration_spec.rb @@ -663,7 +663,7 @@ context 'when tax address is later cleared' do before do order.ship_address = nil - order.update! + order.recalculate end it 'removes all tax adjustments' do diff --git a/frontend/spec/features/checkout_spec.rb b/frontend/spec/features/checkout_spec.rb index 696c5b40649..97fd07f250a 100644 --- a/frontend/spec/features/checkout_spec.rb +++ b/frontend/spec/features/checkout_spec.rb @@ -93,7 +93,7 @@ order.reload order.user = user - order.update! + order.recalculate order end @@ -217,7 +217,7 @@ user = create(:user) order.user = user - order.update! + order.recalculate allow_any_instance_of(Spree::CheckoutController).to receive_messages(current_order: order) allow_any_instance_of(Spree::CheckoutController).to receive_messages(try_spree_current_user: user) @@ -245,7 +245,7 @@ order.reload order.user = user - order.update! + order.recalculate order end @@ -292,7 +292,7 @@ order = OrderWalkthrough.up_to(:delivery) allow(order).to receive_messages(available_payment_methods: [check_payment, credit_cart_payment]) order.user = create(:user) - order.update! + order.recalculate allow_any_instance_of(Spree::CheckoutController).to receive_messages(current_order: order) allow_any_instance_of(Spree::CheckoutController).to receive_messages(try_spree_current_user: order.user) diff --git a/frontend/spec/features/checkout_unshippable_spec.rb b/frontend/spec/features/checkout_unshippable_spec.rb index 63723ea4049..14342e21123 100644 --- a/frontend/spec/features/checkout_unshippable_spec.rb +++ b/frontend/spec/features/checkout_unshippable_spec.rb @@ -15,7 +15,7 @@ user = create(:user) order.user = user - order.update! + order.recalculate allow_any_instance_of(Spree::CheckoutController).to receive_messages(current_order: order) allow_any_instance_of(Spree::CheckoutController).to receive_messages(try_spree_current_user: user) diff --git a/sample/db/samples/payments.rb b/sample/db/samples/payments.rb index e5708d87599..af4c6aca9bf 100644 --- a/sample/db/samples/payments.rb +++ b/sample/db/samples/payments.rb @@ -9,7 +9,7 @@ name: 'Sean Schofield', gateway_customer_profile_id: 'BGS-1234') Spree::Order.all.each_with_index do |order, _index| - order.update! + order.recalculate payment = order.payments.create!(amount: order.total, source: creditcard.clone, payment_method: method) payment.update_columns(state: 'pending', response_code: '12345') end