Skip to content

Commit

Permalink
Add eager loading to shipping methods in Estimator
Browse files Browse the repository at this point in the history
Thanks to Package.shipping_methods now returning an AR::Relation, we are
able to eager load the associated records required for estimating
shipping rates. This should reduce a bunch of per-shipping method
queries to only run once.
  • Loading branch information
jhawthorn committed Feb 25, 2016
1 parent fa75165 commit 17e8ea5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion core/app/models/spree/stock/estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def calculate_shipping_rates(package)
end

def shipping_methods(package)
package.shipping_methods.select do |ship_method|
package.shipping_methods
.includes(:calculator, zones: :zone_members, tax_category: :tax_rates)
.to_a
.select do |ship_method|
calculator = ship_method.calculator
ship_method.include?(order.ship_address) &&
calculator.available?(package) &&
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/stock/estimator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Stock
allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :preferences).and_return({ currency: currency })
allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :marked_for_destruction?)

allow(package).to receive_messages(shipping_methods: [shipping_method])
allow(package).to receive_message_chain(:shipping_methods, includes: [shipping_method])
end

let(:currency) { "USD" }
Expand Down

0 comments on commit 17e8ea5

Please sign in to comment.