[v3.0] Fix app and tests to work with ActiveRecord.has_many_inverse
#4098
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backports 55bd0e4
Rails 6.1 added a new default
has_many_inversing
(seerails/rails#34533 &
rails/rails#37429), which is enabled by default
on new applications. This setting broke some tests and created a couple
of bugs on Solidus. This commit should fix all of this. CI will only
examine the case when the new setting is enabled, but a local run
confirmed the suite is green when it's off.
A couple of the modifications introduced are attributable to two
different bugs in Rails discovered during the process:
collection=
for ahas_many
association doesn't persist added records already referencing the parent whenhas_many_inversing
is enabled rails/rails#42102 forces us to useSpree::Order#shipments#push
instead ofSpree::Order#shipments#=
whenthe proposed shipments get created from the order.
CollectionProxy
withhas_many_inversing
that has been coerced to an array, and the given value is equal to the field's default, doesn't work rails/rails#42094 only applies to a rarecorner case, but it hit us in the test case
takes into account a passed in scope
forSpree::Core::Search::Variant
.We also fixed two bugs on our side:
Spree::Variant#product
Rails association was configured as theinverse of
Spree::Product#variants
. However,Spree::Product#variants
has a custom scope which filters out mastervariants
(
solidus/core/app/models/spree/product.rb
Line 47 in 2ea8296
Instead,
Spree::Product#variants_including_master
is now used.Spree::Stock::SimpleCoordinator#build_shipments
is meant to buildshipments presented as an option to the user. To calculate their
associated shipping rates, it delegates the shipments to
Spree::Stock::Estimator
. This service needs to know the orderinstance those shipments would be assigned to. With the current
implementation, this information is taken from the shipment itself as
it's already associated to that order when it's initialized on
Spree::Stock::Package
(
solidus/core/app/models/spree/stock/package.rb
Line 127 in 2ea8296
Before the
has_many_inversing
feature, this initialization wasn'treflected in the inverse association
Spree::Order#shipments
, butit's no longer the case. For this reason, after we have calculated the
shipping rates, we need to remove the shipping instances through
order.shipments = order.shipments - shipment
. Follow-up work couldclean this up if we pass the order as a parameter to the estimator.
Still, it would have backward compatibility issues for user-created
estimators as the interface would change. The following snippet
isolates the issue:
Other than that, other minor modifications done in the test suite
include:
#reload
calls are needed to refresh the new and improved cache.children. We haven't investigated this point further. It could be due
to some undocumented change of behavior in Rails or another minor bug.