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

Add ability to remove promotions via Promotion#remove_from #1451

Merged

Conversation

jordan-brough
Copy link
Contributor

Extracted from #1433 as per a chat with @jhawthorn.

This will allow promotions to be removed from orders and allows promotion actions to define how to reverse their side effects on an order.

For now this provides a default remove_from on the PromotionAction base class, with a deprecation warning that subclasses should define their own remove_from method.

@jordan-brough jordan-brough force-pushed the add-promotion-remove-from branch from ad06809 to 0be285f Compare September 17, 2016 14:49
# Removes a promotion and any adjustments or other side effects from an
# order.
# @param order [Spree::Order] the order to remove the promotion from.
# @return [undefined]
Copy link
Member

Choose a reason for hiding this comment

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

If you don't care about the return value @return [void] should be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gmacdougall thanks! Updated and opened a PR to remove more of these: #1454

@@ -39,6 +39,16 @@ def compute_amount(calculable)
[(calculable.item_total + calculable.ship_total), amount].min * -1
end

# Removes any adjustments generated by this action from the order.
# @param order [Spree::Order] the order to remove the action from.
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

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.

let(:promotion) { create(:promotion, :with_line_item_adjustment, adjustment_rate: adjustment_amount) }
let(:adjustment_amount) { 10 }
let(:action) { promotion.actions.first! }
let!(:line_item) { create(:line_item, order: order) }
let(:line_item) { order.line_items.to_a.first }
Copy link
Member

Choose a reason for hiding this comment

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

The to_a shouldn't be needed

Copy link
Contributor Author

@jordan-brough jordan-brough Sep 19, 2016

Choose a reason for hiding this comment

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

@gmacdougall this ensures that line_item refers to the same object that OrderUpdater and the promo code & etc will modify. i.e. order.line_items.first.object_id == order.line_items.to_a.first.object_id is not always true in general.

Our current implementation of the order factory happens to preload order.line_items such that the to_a isn't needed here, but that's not tested and I'm not sure it was intentional either. Perhaps I could remove it here but make an additional PR that tests this explicitly in order_factory_spec?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also prefer keeping the to_a.first

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good

let(:action) { promotion.actions.first! }
let(:promotion) { create(:promotion, :with_order_adjustment) }

# this adjustment should not get removed
Copy link
Member

Choose a reason for hiding this comment

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

Could we name the other_adjustment something that implies this more directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gmacdougall sure, I've updated it to unrelated_adjustment.

Copy link
Member

@gmacdougall gmacdougall left a comment

Choose a reason for hiding this comment

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

Thanks Jordan!

Copy link
Contributor

@cbrunsdon cbrunsdon left a comment

Choose a reason for hiding this comment

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

I definitely like the idea that a promo would be able to remove its own side effects, as its definitely something i've had to hack in in the past 👍

@jordan-brough jordan-brough merged commit 8f8a2f0 into solidusio:master Sep 29, 2016
jordan-brough added a commit that referenced this pull request Sep 29, 2016
@jordan-brough jordan-brough deleted the add-promotion-remove-from branch September 29, 2016 17:58
@flyfy1
Copy link
Contributor

flyfy1 commented Nov 22, 2016

Currently I can see the remove_from being defined.. however, where is this method called? I searched through the solidus core but I cannot find a single place where this remove_from method being called... (except in spec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants