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

Refactor line_item.rb #522

Merged
merged 4 commits into from Jan 20, 2016
Merged

Refactor line_item.rb #522

merged 4 commits into from Jan 20, 2016

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Nov 19, 2015

This PR was supposed to be preparation for the VAT stuff, but turned out to be more of a housekeeping exercise: Some naming improvements and a little bit of code style, plus different tests for creating line items.

In order to improve readability of the file, I sorted the code by what it does (associations, callbacks, methods, private methods).

Nevertheless, I do think this is an improvement, so I'm keeping the PR open.


private
# Sets the quantity to zero if it is nil or less than zero.
def invalid_quantity_check
Copy link
Member

Choose a reason for hiding this comment

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

If we're already renaming and moving stuff (what I like), we should rename this method into normalize_quantity, because that's what it does.

PS: And please add a new line after the private. My dear eyes are bleeding.

@mamhoff
Copy link
Contributor Author

mamhoff commented Nov 20, 2015

The initial idea for this PR is not working out - there are many ways of setting a line item's price, and directly hooking into all of them them for deducing the merchant's home VAT (like I did it in Spree) is not feasible or beautiful. As a consequence, this is a pretty pure refactoring of the line item, adding mostly readability for the time being. Next up: Think deeply about a good way of modifying line item prices.

This PR is now ready to merge. Btw: I did have to force push twice to account for random failures in the back end suite.

@tvdeyen tvdeyen mentioned this pull request Nov 23, 2015
@cbrunsdon
Copy link
Contributor

I'm not sure how (or if?) you're going to utilize any of these changes in the VAT code but I'm still 👍 on the commit.

This just moves lines up and down so it's easier to see where
associations, AR callbacks, configuration and actual methods are.

I've also moved to methods to private which are not called from
outside the line item instance. I think they were mostly public because
their tests required that, so I reorganized the tests for them a little.

I've also de-indented the private methods. Hound will be happy!

I'd like to have this in as preparation for the VAT work.
Before, there was two private methods for copying various attributes from the variant into the line item. This commit moves them into one.
@mamhoff
Copy link
Contributor Author

mamhoff commented Jan 9, 2016

Rebased. @cbrunsdon it helps, because having easier-to-read code when refactoring things helps. :)

@jhawthorn jhawthorn added this to the 1.3.0 milestone Jan 11, 2016
@jhawthorn
Copy link
Contributor

I love this change, but am a little concerned about changing copy_price, which is often overridden as an extension point. A quick github search shows how common this is. However, this method is definitely misnamed.

Talked about this IRL with @cbrunsdon who suggested we could check this and warn users if the copy_price method was defined, which I think is a clever solution. It would be even better if we could make this change and at the same time provide a solution for #390. Tagging for 1.3 in hopes that we come up with a solution for that at the same time.

Many projects have overridden the #copy_price hook. In order
for them to easily upgrade, re-extract setting the price-related
attributes to a private method #set_pricing_attributes and
notify about the name change if #copy_price is set.
@mamhoff
Copy link
Contributor Author

mamhoff commented Jan 12, 2016

OK, totally understand the concern about copy_price. I'm re-introducing it as a private method set_pricing_attributes and notify people who override it with an ActiveSupport::Deprecation.

I don't think tackling #390 in this PR makes much sense, as this PR doesn't address changes in functionality. The idea is really just to fix naming and housekeeping.

@jhawthorn
Copy link
Contributor

👍 (and clarke is here and says 👍)

jhawthorn added a commit that referenced this pull request Jan 20, 2016
@jhawthorn jhawthorn merged commit 3dd7095 into solidusio:master Jan 20, 2016
@mamhoff mamhoff deleted the refactor-line-item branch May 24, 2016 19:42
it 'should run the user-defined copy_price method' do
expect_any_instance_of(Spree::LineItem).to receive(:copy_price).and_call_original
ActiveSupport::Deprecation.silence do
Spree::LineItem.new(variant: variant, order: order)
Copy link
Contributor

@flyfy1 flyfy1 Nov 16, 2016

Choose a reason for hiding this comment

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

Spree::LineItem.new(variant: variant, order: order) it self doesn't call the #copy_price method, but build the order object does.

Instead, one should do:

let(:order) { create :order }
# ...
Spree::LineItem.new(variant: variant, order: order).save

(with a clean order created, without calling copy_price)

See #1591

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