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
Changes from 1 commit
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
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