Skip to content

Commit

Permalink
Merge pull request #7036 from Matt-Yorkley/adjustments-callbacks
Browse files Browse the repository at this point in the history
[Adjustments] Reduce adjustments callbacks
  • Loading branch information
Matt-Yorkley authored Mar 11, 2021
2 parents 791a47d + 29c5703 commit 120a2c4
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 57 deletions.
32 changes: 13 additions & 19 deletions app/controllers/cart_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@ class CartController < BaseController
def populate
order = current_order(true)

# Without intervention, the Spree::Adjustment#update_adjustable callback is called many times
# during cart population, for both taxation and enterprise fees. This operation triggers a
# costly Spree::Order#update!, which only needs to be run once. We avoid this by disabling
# callbacks on Spree::Adjustment and then manually invoke Spree::Order#update! on success.
Spree::Adjustment.without_callbacks do
cart_service = CartService.new(order)
cart_service = CartService.new(order)

cart_service.populate(params.slice(:variants, :quantity), true)
if cart_service.valid?
order.recreate_all_fees!
order.cap_quantity_at_stock!
order.update!
cart_service.populate(params.slice(:variants, :quantity), true)
if cart_service.valid?
order.cap_quantity_at_stock!
order.recreate_all_fees!

variant_ids = variant_ids_in(cart_service.variants_h)
variant_ids = variant_ids_in(cart_service.variants_h)

render json: { error: false,
stock_levels: VariantsStockLevels.new.call(order, variant_ids) },
status: :ok
else
render json: { error: cart_service.errors.full_messages.join(",") },
status: :precondition_failed
end
render json: { error: false,
stock_levels: VariantsStockLevels.new.call(order, variant_ids) },
status: :ok
else
render json: { error: cart_service.errors.full_messages.join(",") },
status: :precondition_failed
end

populate_variant_attributes
end

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ def update
rescue StandardError => e
flash[:error] = I18n.t("checkout.failed")
action_failed(e)
ensure
@order.update!
end

# Clears the cached order. Required for #current_order to return a new order
Expand Down
1 change: 0 additions & 1 deletion app/controllers/spree/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def edit

def update
@order.recreate_all_fees!
@order.update!

unless order_params.present? && @order.update(order_params) && @order.line_items.present?
if @order.line_items.empty?
Expand Down
21 changes: 0 additions & 21 deletions app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class Adjustment < ActiveRecord::Base
validates :label, presence: true
validates :amount, numericality: true

after_save :update_adjustable
after_destroy :update_adjustable

state_machine :state, initial: :open do
event :close do
transition from: :open, to: :closed
Expand Down Expand Up @@ -144,29 +141,11 @@ def has_tax?
included_tax.positive?
end

def self.without_callbacks
skip_callback :save, :after, :update_adjustable
skip_callback :destroy, :after, :update_adjustable

result = yield
ensure
set_callback :save, :after, :update_adjustable
set_callback :destroy, :after, :update_adjustable

result
end

# Allow accessing soft-deleted originator objects
def originator
return if originator_type.blank?

originator_type.constantize.unscoped { super }
end

private

def update_adjustable
adjustable.update! if adjustable.is_a? Order
end
end
end
1 change: 1 addition & 0 deletions app/models/spree/return_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def process_return
credit.save

order.return if inventory_units.all?(&:returned?)
order.update!
end

def allow_receive?
Expand Down
2 changes: 2 additions & 0 deletions app/services/order_fees_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def recreate_all_fees!
create_line_item_fees!
create_order_fees!
end

order.update!
end

def create_line_item_fees!
Expand Down
1 change: 0 additions & 1 deletion spec/controllers/spree/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
order.line_items.last.update(variant_id: variant2.id)
while !order.completed? do break unless order.next! end
order.recreate_all_fees!
order.update!
order
end

Expand Down
1 change: 0 additions & 1 deletion spec/controllers/spree/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@
order.reload.line_items.last.update(variant_id: variant2.id)
while !order.completed? do break unless order.next! end
order.recreate_all_fees!
order.update!
order
end
let(:params) {
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/admin/orders_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
let(:order) { create(:order) }

it "selects eligible adjustments" do
adjustment = create(:adjustment, adjustable: order, amount: 1)
adjustment = create(:adjustment, order: order, adjustable: order, amount: 1)

expect(helper.order_adjustments_for_display(order)).to eq [adjustment]
end

it "filters shipping method adjustments" do
create(:adjustment, adjustable: order, amount: 1, originator_type: "Spree::ShippingMethod")
create(:adjustment, order: order, adjustable: order, amount: 1, originator_type: "Spree::ShippingMethod")

expect(helper.order_adjustments_for_display(order)).to eq []
end
Expand Down
1 change: 1 addition & 0 deletions spec/helpers/checkout_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
}

before do
order.update!
# Sanity check initial adjustments state
expect(order.shipment_adjustments.count).to eq 1
expect(order.adjustments.enterprise_fee.count).to eq 1
Expand Down
12 changes: 0 additions & 12 deletions spec/models/spree/adjustment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ module Spree
end
end

context "#save" do
it "should call order#update!" do
adjustment = Spree::Adjustment.new(
adjustable: order,
amount: 10,
label: "Foo"
)
expect(order).to receive(:update!)
adjustment.save
end
end

context "adjustment state" do
let(:adjustment) { create(:adjustment, state: 'open') }

Expand Down
1 change: 1 addition & 0 deletions spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@

before do
allow(subject).to receive(:fee_handler) { fee_handler }
allow(subject).to receive(:update!)
end

it "clears all enterprise fee adjustments on the order" do
Expand Down

0 comments on commit 120a2c4

Please sign in to comment.