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

[WIP] Import count_on_hand methods for Spree 2 #2417

Closed
wants to merge 1 commit into from
Closed

[WIP] Import count_on_hand methods for Spree 2 #2417

wants to merge 1 commit into from

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Jun 29, 2018

The added methods will be gone in Spree 2. I imported them and added
logic to work with new and old Spree.

What? Why?

Helps with #2014.

This is a proposal for discussion, possibly at the next Spree update call. It adds logic to deal with current and new Spree code and is therefore a new approach to the Spree upgrade.

When thinking about refactoring our uses of count_on_hand, I saw a lot of work. But all this work could be obsolete when we come to the network feature and get rid of VariantOverride. That's why I propose to add compatibility code instead of refactoring everything now.

I think that this a very quick and easy solution to Spree moving the stock logic into another model. It also allows us to merge this change now. That should avoid merge conflicts. More importantly, it makes every developer working with the current code aware of changes in this area and that they should not use count_on_hand in SQL queries or as direct attribute assignment. After the Spree upgrade, we can and should simplify that code. That's the downside. It also needs testing with the new Spree version.

What should we test?

Everywhere where variants are used and their stock is shown. Examples:

  • Shop front
  • Product import
  • Reports
  • Viewing and editing orders

Release notes

Added more compatibility to Spree 2.0.

How is this related to the Spree upgrade?

This is to avoid conflicts with the Spree upgrade.

The added methods will be gone in Spree 2. I imported them and added
logic to work with new and old Spree.
@Matt-Yorkley
Copy link
Contributor

I really like this approach. It could potentially allow the Spree upgrade to get finished a lot quicker, whilst also giving more breathing room in terms of decoupling existing code from Spree.

@kirstenalarsen
Copy link
Contributor

kirstenalarsen commented Jul 16, 2018 via email

@Matt-Yorkley
Copy link
Contributor

If we could use similar techniques to get us up to the point where Solidus was forked we might be able to avoid reinventing the wheel with our own set of hooks throughout the Spree codebase.

@Matt-Yorkley
Copy link
Contributor

@sauloperez What's you opinion on this solution? ^

error = 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false'
raise error unless Spree::Config[:track_inventory_levels]

self.count_on_hand = new_level unless on_demand
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 directly affected by openfoodfoundation/spree#8

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, that's where I saw this code before! Thank you. So this pull request would override Frank's change.

@sauloperez
Copy link
Contributor

sauloperez commented Jul 16, 2018

I think this is worth discussing in the next Spree upgrade catch up. See my comment in https://openfoodnetwork.slack.com/archives/C4NDJT3FY/p1531749287000116.

While it makes it upgrade shorter as we won't refactor all the calls to old methods, it also brings an extra considerable level of complexity we will have to deal with.


# Overriding `Spree::Variant.count_on_hand=` in old Spree versions.
# It doesn't exist in newer Spree versions.
def count_on_hand=(new_level)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these methods necessary? Spree::Variant doesn't have count_on_hand or count_on_hand=methods, it uses the on_hand methods as getters and setters.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or is this related to Variant Overrides?

Copy link
Member Author

Choose a reason for hiding this comment

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

In Spree 1.3 the variants table has a count_on_hand column. So ActiveRecord adds these methods automatically and we have a few calls to these methods in our code base. In order to keep the old API alive, we need these methods.

@sauloperez
Copy link
Contributor

Also related to #2450

@mkllnk
Copy link
Member Author

mkllnk commented Aug 2, 2018

We decided to keep the old Spree API (on_hand methods etc.), but implement only the code for the Spree upgrade. We will create a new pull request against the 2-0-stable branch.

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.

4 participants