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

Make Splitter::Base.build_package and Shipment.to_package use Config::package_factory #39

Merged

Conversation

luisramos0
Copy link

@luisramos0 luisramos0 commented Mar 14, 2019

This PR fixes: openfoodfoundation/openfoodnetwork#3607

Description

This PR is related to coopdevs#6
We are replacing the usage of Spree::Stock::Package with a call to the package factory.
In both cases below, for OFN, the shipping methods were not being filtered by distributor, which is done in the OFN package implementation injected in package_factory.

Splitter::Base

The change in Splitter::Base makes all splitters use the injected Package, not the default Spree Package class when building the split packages. This was breaking all splitters that required the custom package factory.

This is related to openfoodfoundation/openfoodnetwork#3604
This has no impact on OFN after 3604 because this build_package method is called by all splitters except the Base splitter which is the only one that OFN will use for now.

Shipment.to_package

The change in Shipment.to_package uses the injected Package factory to convert every shipment to a package.
This was creating a bug in OFN during order.refresh_rates that happens when loading shipping_methods in the order edit page. See openfoodfoundation/openfoodnetwork#3607

@luisramos0 luisramos0 self-assigned this Mar 14, 2019
def initialize(stock_location, order, splitters) end
end

packer.package_factory = CustomPackage
Copy link
Author

Choose a reason for hiding this comment

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

For some reason I couldn't mock package_factory with either
before { Spree::Config.package_factory = CustomPackage }
or
allow(Spree::Config).to receive(:package_factory) { CustomPackage }

I ended up making package_factory writable (attr_accessor instead of attr_reader above).

Choose a reason for hiding this comment

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

could it be related to the fact that there is no ; between the initialize and the end in line 28 above? AFAIK that is not valid Ruby.

Copy link
Member

Choose a reason for hiding this comment

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

This seems to work:

Suggested change
packer.package_factory = CustomPackage
allow(Spree::Config).to receive(:package_factory) { CustomPackage }

Copy link
Author

Choose a reason for hiding this comment

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

yeah, I must have been doing something wrong, it just works now...
@sauloperez the missing ; was not causing the problem but I added it anyway

@luisramos0 luisramos0 changed the title Make Base splitter build packages using the package factory set in the Packer Make Splitter::Base.build_package and Shipment.to_package use Config::package_factory Mar 15, 2019
def initialize(stock_location, order, splitters) end
end

packer.package_factory = CustomPackage

Choose a reason for hiding this comment

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

could it be related to the fact that there is no ; between the initialize and the end in line 28 above? AFAIK that is not valid Ruby.

@@ -222,7 +222,8 @@ def inventory_units_for(variant)
end

def to_package
package = Stock::Package.new(stock_location, order)
package_factory = Spree::Stock::Coordinator.new(self).packer(stock_location).package_factory

Choose a reason for hiding this comment

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

Is there any reason why don't get the factory from Spree::Config.package_factory?

Copy link
Author

Choose a reason for hiding this comment

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

no, thanks! I changed it.

@sauloperez
Copy link

Good catch! I don't know how I missed this calls to the package class 🙈

Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Good detective work. I'm looking forward to your replies.

def initialize(stock_location, order, splitters) end
end

packer.package_factory = CustomPackage
Copy link
Member

Choose a reason for hiding this comment

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

This seems to work:

Suggested change
packer.package_factory = CustomPackage
allow(Spree::Config).to receive(:package_factory) { CustomPackage }

…instanciating Spree::Stock::Package

This ensures the injected package factory is used when converting a shipment to a package
…e Packer

This will make the splitter use the injected Package, not the default Spree Package class
@luisramos0
Copy link
Author

awesome, I incorporated your feedback, thanks guys!
ready for re-review.

@sauloperez sauloperez merged commit 060d0a5 into openfoodfoundation:2-0-4-stable Mar 22, 2019
@sauloperez
Copy link

Perhaps you wanted to manually test it?

@luisramos0 luisramos0 deleted the 2-0-fix-packer-bug branch March 22, 2019 10:08
@luisramos0
Copy link
Author

it's fine, we test it on the ofn PR

luisramos0 added a commit to luisramos0/openfoodnetwork that referenced this pull request Mar 6, 2020
luisramos0 added a commit to luisramos0/openfoodnetwork that referenced this pull request Mar 6, 2020
luisramos0 added a commit to luisramos0/openfoodnetwork that referenced this pull request Mar 12, 2020
sauloperez added a commit to openfoodfoundation/openfoodnetwork that referenced this pull request Mar 17, 2020
[Spree 2.1] Use latest version of spree which includes PR openfoodfoundation/spree#39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants