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 for adjustments in ItemAdjustments #1401

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
19 changes: 19 additions & 0 deletions core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class Adjustment < Spree::Base
validates :amount, numericality: true
validates :promotion_code, presence: true, if: :require_promotion_code?

# We need to use `after_commit` here because otherwise it's too early to
# tell if any repair is needed.
after_commit :repair_adjustments_associations_on_create, on: [:create]
after_commit :repair_adjustments_associations_on_destroy, on: [:destroy]

scope :not_finalized, -> { where(finalized: false) }
scope :open, -> do
Spree::Deprecation.warn "Adjustment.open is deprecated. Instead use Adjustment.not_finalized", caller
Expand Down Expand Up @@ -177,5 +182,19 @@ def update!(target = nil)
def require_promotion_code?
promotion? && source.promotion.codes.any?
end

def repair_adjustments_associations_on_create
if adjustable.adjustments.loaded? && !adjustable.adjustments.include?(self)
Spree::Deprecation.warn("Adjustment was not added to #{adjustable.class}. Add adjustments via `adjustable.adjustments.create!`. Partial call stack: #{caller.select { |line| line =~ %r(/(app|spec)/) }}.", caller)
adjustable.adjustments.proxy_association.add_to_target(self)
end
end

def repair_adjustments_associations_on_destroy
if adjustable.adjustments.loaded? && adjustable.adjustments.include?(self)
Spree::Deprecation.warn("Adjustment was not removed from #{adjustable.class}. Remove adjustments via `adjustable.adjustments.destroy`. Partial call stack: #{caller.select { |line| line =~ %r(/(app|spec)/) }}.", caller)
adjustable.adjustments.proxy_association.target.delete(self)
end
end
end
end
6 changes: 1 addition & 5 deletions core/app/models/spree/item_adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ def update
private

def adjustments
# This is done intentionally to avoid loading the association. If the
# association is loaded, the records may become stale due to code
# elsewhere in spree. When that is remedied, this should be changed to
# just item.adjustments
@adjustments ||= item.adjustments.all.to_a
@adjustments ||= item.adjustments.to_a
end
end
end
3 changes: 1 addition & 2 deletions core/app/models/spree/promotion/actions/create_adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ def perform(options = {})
return if promotion_credit_exists?(order)

amount = compute_amount(order)
Spree::Adjustment.create!(
order.adjustments.create!(
amount: amount,
order: order,
adjustable: order,
source: self,
promotion_code: options[:promotion_code],
label: "#{Spree.t(:promotion)} (#{promotion.name})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def compute_amount(adjustable)
def create_adjustment(adjustable, order, promotion_code)
amount = compute_amount(adjustable)
return if amount == 0
adjustments.create!(
adjustable.adjustments.create!(
source: self,
amount: amount,
adjustable: adjustable,
order: order,
promotion_code: promotion_code,
label: "#{Spree.t(:promotion)} (#{promotion.name})"
Expand Down
4 changes: 3 additions & 1 deletion core/app/models/spree/tax/order_adjuster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def initialize(order)
def adjust!
return unless order_tax_zone(order)

order.all_adjustments.tax.destroy_all
[order, *order.line_items, *order.shipments].each do |item|
item.adjustments.destroy(item.adjustments.select(&:tax?))
end
Copy link
Contributor

@jhawthorn jhawthorn Aug 24, 2016

Choose a reason for hiding this comment

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

Instead of this we should probably fix this line https://github.com/solidusio/solidus/blob/master/core/app/models/spree/tax/item_adjuster.rb#L31

and then remove the skip_destroy_adjustments option

orders here should never have tax adjustments (they've been only on items since spree 2.2), so I don't think we need to consider that

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 thanks, updated!


(order.line_items + order.shipments).each do |item|
ItemAdjuster.new(item, order_wide_options).adjust!
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/spree/tax_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ def adjust(order_tax_zone, item)

included = included_in_price && amount > 0

adjustments.create!({
adjustable: item,
item.adjustments.create!(
source: self,
amount: amount,
order_id: item.order_id,
label: adjustment_label(amount),
included: included
})
)
end

# This method is used by Adjustment#update to recalculate the cost.
Expand Down
8 changes: 4 additions & 4 deletions core/app/models/spree/unit_cancel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ class Spree::UnitCancel < Spree::Base
DEFAULT_REASON = 'Cancel'

belongs_to :inventory_unit
has_one :adjustment, as: :source, dependent: :destroy
has_many :adjustments, as: :source, dependent: :destroy
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this change would merit an entry in the CHANGELOG?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bbuchalter good point. I updated to @jhawthorn's solution though, so I've reverted back to has_one.


validates :inventory_unit, presence: true

# Creates necessary cancel adjustments for the line item.
def adjust!
raise "Adjustment is already created" if adjustment
raise "Adjustment is already created" if adjustments.exists?

amount = compute_amount(inventory_unit.line_item)

create_adjustment!(
adjustable: inventory_unit.line_item,
inventory_unit.line_item.adjustments.create!(
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, I think this could be.

self.adjustment = inventory_unit.line_item.adjustments.create!(

This should keep both associations fresh without performing an extra query (no query run since source will already be set correctly). This avoids the need to change the has_one to a has_many

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. Thanks!

source: self,
amount: amount,
order: inventory_unit.order,
label: "#{Spree.t(:cancellation)} - #{reason}",
Expand Down
33 changes: 19 additions & 14 deletions core/lib/spree/testing_support/factories/adjustment_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@
label 'Shipping'
association(:source, factory: :tax_rate)
eligible true
end

factory :tax_adjustment, class: Spree::Adjustment do
order { adjustable.order }
association(:adjustable, factory: :line_item)
amount 10.0
label 'VAT 5%'
association(:source, factory: :tax_rate)
eligible true
after(:build) do |adjustment|
adjustments = adjustment.adjustable.adjustments
if adjustments.loaded? && !adjustments.include?(adjustment)
adjustments.proxy_association.add_to_target(adjustment)
end
end

factory :tax_adjustment, class: Spree::Adjustment do
order { adjustable.order }
association(:adjustable, factory: :line_item)
amount 10.0
label 'VAT 5%'

after(:create) do |adjustment|
# Set correct tax category, so that adjustment amount is not 0
if adjustment.adjustable.is_a?(Spree::LineItem)
adjustment.source.tax_category = adjustment.adjustable.tax_category
adjustment.source.save
adjustment.update!
after(:create) do |adjustment|
# Set correct tax category, so that adjustment amount is not 0
if adjustment.adjustable.is_a?(Spree::LineItem)
adjustment.source.tax_category = adjustment.adjustable.tax_category
adjustment.source.save
adjustment.update!
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/lib/tasks/order_capturing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

context "with a mix of canceled and shipped inventory" do
before do
Spree::OrderCancellations.new(order).short_ship([order.inventory_units.first])
Spree::OrderCancellations.new(order).short_ship([order.line_items.first.inventory_units.first])
order.shipping.ship_shipment(order.shipments.first)
order.update_attributes!(payment_state: 'balance_due')
end
Expand Down
10 changes: 6 additions & 4 deletions core/spec/models/spree/item_adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ def create_adjustment(label, amount)
context "multiple updates" do
let(:adjustment) { create(:tax_adjustment, amount: -10) }
let(:item) { adjustment.adjustable }
# we need to get this from the line item so that we're modifying the same
# tax rate that is cached by line_item.adjustments
let(:source) { item.adjustments.to_a.first.source }

def update
described_class.new(item).update
Expand All @@ -283,18 +286,17 @@ def db_record
end

it "persists each change" do
adjustment.source.update_attributes!(amount: 0.1)
source.update_attributes!(amount: 0.1)
update
expect(item).not_to be_changed
expect(db_record).to have_attributes(adjustment_total: 1)

adjustment.source.update_attributes!(amount: 0.20)
item.reload
source.update_attributes!(amount: 0.20)
update
expect(item).not_to be_changed
expect(db_record).to have_attributes(adjustment_total: 2)

adjustment.source.update_attributes!(amount: 0.10)
source.update_attributes!(amount: 0.10)
update
expect(item).not_to be_changed
expect(db_record).to have_attributes(adjustment_total: 1)
Expand Down
4 changes: 2 additions & 2 deletions core/spec/models/spree/order_cancellations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@
order.contents.add(line_item.variant)

# make the total $1.67 so it divides unevenly
Spree::Adjustment.tax.create!(
line_item.adjustments.create!(
source_type: 'Spree::TaxRate',
order: order,
adjustable: line_item,
amount: 0.01,
label: 'some fake tax',
finalized: true
Expand Down
3 changes: 1 addition & 2 deletions core/spec/models/spree/tax/item_adjuster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Spree::Tax::ItemAdjuster do
subject(:adjuster) { described_class.new(item) }
let(:order) { Spree::Order.new }
let(:order) { create(:order) }
let(:item) { Spree::LineItem.new(order: order) }

before do
Expand Down Expand Up @@ -64,7 +64,6 @@
before { allow(item).to receive(:tax_category).and_return(item_tax_category) }

it 'creates an adjustment for every matching rate' do
expect(rate_1).to receive_message_chain(:adjustments, :create!)
expect(adjuster.adjust!.length).to eq(1)
end
end
Expand Down