Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Extract the packaging code into a service object #21

Merged
merged 14 commits into from
Aug 31, 2016

Conversation

fredericboivin
Copy link
Contributor

Logic related to building the ActiveShipping::Packages needed for the find rates is extracted into a service object.

The goal would be for it to be an extension point where different "packager" could be used. Code is the same for now, it's a straight extraction so no refactoring yet

Max weight is delegated to the calculator through Forwardable since max_weight_for_country is a protected method.

Next step would be to determine how to better handle the max_weight since it's determined on the calculator level and is currently riddled with bugs

Note : build is currently failing because USPS is returning 503

@fredericboivin fredericboivin force-pushed the extract_service branch 4 times, most recently from 8ea8376 to 6fc9252 Compare June 1, 2016 22:02

attr_reader :shipping_calculator

def_instance_delegator :shipping_calculator, :max_weight_for_country
Copy link
Contributor

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 we can't initialise the PackageBuilder with the max_weight_for_county so it doesn't have to be coupled with the calculator?

@fredericboivin fredericboivin force-pushed the extract_service branch 3 times, most recently from d7947c8 to 9ede5c5 Compare June 2, 2016 17:47
@@ -15,11 +15,26 @@ def process(package)
packages(package)
end

# Configuration
def units
Copy link
Contributor

Choose a reason for hiding this comment

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

Are any of these going to be part of the public interface of the PackageBuilder? If not, i'd prefer if they were private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To my knowledge, they should not be used elsewhere, so good catch

@fredericboivin
Copy link
Contributor Author

fredericboivin commented Jun 2, 2016

Last commit breaks the tests since the behavior is different, currently working on adding spec to the package_builder (that I will amend later) so we can kill/salvage the weight_limits_spec and kill the weight-related code in the base calculator

active_shipping_packages = []
weights = convert_package_to_weights_array(solidus_package)

ci_without_boxes = []
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer if we didn't introduce a new name for product_packages here as boxes. We also generally avoid abbreviating things in names. It isn't immediately clear what ci is when reading this code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, fixed it

@fredericboivin fredericboivin force-pushed the extract_service branch 3 times, most recently from 79ccc7f to 77c8682 Compare June 3, 2016 00:04
@fredericboivin
Copy link
Contributor Author

fredericboivin commented Jun 3, 2016

@jhawthorn : I'd really appreciate if you could take a look and provide some feedback if you have the time (outside of style issues that I will rubocop and the stuff Chris already mentionned)

@fredericboivin fredericboivin force-pushed the extract_service branch 6 times, most recently from 7743361 to 92239bc Compare June 3, 2016 18:25
@fredericboivin fredericboivin changed the title WIP Extract the packaging code into a service object Extract the packaging code into a service object Jun 28, 2016
@fredericboivin
Copy link
Contributor Author

Should be ready for another round of review if build is green

@fredericboivin
Copy link
Contributor Author

Tests are broken in general now on master because of Solidus PR#1138

Going to fix this in another PR

@fredericboivin
Copy link
Contributor Author

Will need to be rebased on #29

@forkata
Copy link
Contributor

forkata commented Jul 5, 2016

#29 is merged so we can rebase this to get the tests 🍏 again!

@fredericboivin fredericboivin force-pushed the extract_service branch 2 times, most recently from 10f7d64 to f0cc228 Compare July 5, 2016 17:19
@forkata
Copy link
Contributor

forkata commented Aug 31, 2016

This guy has been around for a while so I think it's time we take another look over it and merge 🎉

@forkata
Copy link
Contributor

forkata commented Aug 31, 2016

@dangerdogz When you have a chance can you rebase this on top of master and push up so it can run specs again against new Solidus master and v2.0 branch.

Extract the packaging related code into a new service object.
Goal is for it to be an extension point which can support various
calculators related to different metrics. For now the code/logic
is exactly the same.
Extract methods for quickly building inventory units and content items
into PackageHelper so it can be reused between spec
Centralize the definition and use of the config values so it can
be easily updated and usage is the same everywhere
Add comments to the method and simplify the logic. Compute it once
then reuse the result throughout the process
Delete the calculator::weight namespace and move the PackageBuilder
to the default Spree::
Use the methods provided by ContentItem to avoid chaining associations
to get variant_weight + ContentItem. ContentItem also has a quantity
of one so we don't need the convoluted logic around fitting a max
quantity of item into a package.
packages method was accessing the properties of ProductPackages
through an array index which is unreliable, opted to use the object
itself instead of an array of numbers and access the data through the
properties. Also remove the hardcoded imperial units
Rename variables so it's easier to understand what is coming from
Solidus and what is returned by ActiveShipping
Previously, product with associated product packages were computed twice
(once based on weight and added in the general packages) and once for
each product_packages. This commit aims to split the generation so
product are added once based on the existence of associated
product_packages
It's full of holes and it should be replace by package_builder_spec
instead.
The code was extracted to package_builder.rb
Before, the call to max_weight_for_country was delegated to the
calculator but the max_weight was computed by the package builder.
It is out of scope for the PackageBuilder since the max_weight
is determined at the calculator level. This commit remove the delegation
and make it a parameter of the process function. Also included : small
tweaks to the calculator to compute and send the parameters and changes
to the test suite.
Find indentation and various style issues
@fredericboivin
Copy link
Contributor Author

Rebased

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

Successfully merging this pull request may close these issues.

3 participants