Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use in-memory objects in OrderUpdater and remove Adjustment callbacks #1389

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/spec/models/spree/order_cancellations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
inventory_unit_2.line_item.reload # UnitCancel#compute_amount needs updated amounts
order.cancellations.short_ship([inventory_unit_2])
line_item.reload
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the change above it are unfortunate. :( See the commit message for details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an idea for getting OrderUpdater to operate on in-memory objects, to try to avoid problems like the above.

diff --git a/core/app/models/spree/order_updater.rb b/core/app/models/spree/order_updater.rb
index caff2a6..b21a70c 100644
--- a/core/app/models/spree/order_updater.rb
+++ b/core/app/models/spree/order_updater.rb
@@ -31,7 +31,15 @@ module Spree
     end

     def recalculate_adjustments
-      all_adjustments.includes(:adjustable).map(&:adjustable).uniq.each { |adjustable| Spree::ItemAdjustments.new(adjustable).update }
+      adjustables = Set.new
+
+      # Prefer using in-memory objects when possible:
+      adjustables << order
+      adjustables.merge(order.line_items)
+      adjustables.merge(order.shipments)
+      # In case someone is attaching adjustments to something other than LineItems and Shipments:
+      adjustables.merge(all_adjustments.includes(:adjustable).map(&:adjustable))
+
+      adjustables.each do |adjustable|
+        Spree::ItemAdjustments.new(adjustable).update
+      end
     end

Two risks I can think of:

  1. The in-memory objects may be stale. That might be an acceptable risk in the interest of performance.
  2. The above code will invoke ItemAdjustments#update on all shipments & line items, even if they don't currently have any adjustments on them. That will result in an extra select * from spree_adjustments where adjustable_id = ? for every line item/shipment that doesn't have at least one adjustment (extra compared to the existing code).

Any thoughts?

Copy link
Contributor

@jhawthorn jhawthorn Aug 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really want to do this.

I don't think we should concern ourselves with someone attaching adjustments to something other than LineItems and Shipments, since the rest of the methods in OrderUpdater won't take that into account.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhawthorn great point. Here's a PR (against this PR) to do that: jordan-brough#7 Shall I merge that into this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall I merge that into this PR?

👍

expect(line_item.adjustments.non_tax.sum(:amount)).to eq(-1.67)
expect(line_item.adjustments.map(&:amount)).to match_array([0.01, -0.83, -0.84])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[0.01, -0.83, -0.84].inject(:+) #=> -1.66

and that 0.01 is the tax adjustment above 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little strange, as the three values now add up to -1.66. Is there a tax adjustment hidden there?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Don't answer, Line 144-152 explain it all.

Copy link
Contributor Author

@jordan-brough jordan-brough Aug 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mamhoff I went ahead and updated a bit because this spec was hard for me to follow when I first read it and @jhawthorn also left a comment similar to yours.

expect(line_item.total).to eq 0
end
end
Expand Down