Skip to content

Commit

Permalink
pass apply_tags and checkout params to methods; fix openfoodfoundatio…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewpbrett committed Oct 11, 2020
1 parent 0d2f92f commit 037f8fc
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ def update

OrderWorkflow.new(@order).complete

@order.shipments.map(&:refresh_rates)
@order.shipments.map {
|shipment| shipment.refresh_rates(checkout: false, apply_tags: false)
}
flash[:success] = Spree.t('customer_details_updated')
redirect_to admin_order_customer_path(@order)
else
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/spree/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def new
end

def edit
@order.shipments.map(&:refresh_rates)
@order.shipments.map {
|shipment| shipment.refresh_rates(checkout: false, apply_tags: false)
}

OrderWorkflow.new(@order).complete

Expand Down
4 changes: 3 additions & 1 deletion app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def create_proposed_shipments
adjustments.shipping.delete_all
shipments.destroy_all

packages = OrderManagement::Stock::Coordinator.new(self).packages
packages = OrderManagement::Stock::Coordinator.new(self).packages(
checkout: true, apply_tags: true
)
packages.each do |package|
shipments << package.to_shipment
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ def selected_shipping_rate_id=(id)
save!
end

def refresh_rates(apply_tags = true)
def refresh_rates(checkout: true, apply_tags: true)
return shipping_rates if shipped?

# The call to Stock::Estimator below will replace the current shipping_method
original_shipping_method_id = shipping_method.try(:id)
self.shipping_rates =
OrderManagement::Stock::Estimator.new(order).shipping_rates(to_package, true, apply_tags)
self.shipping_rates = OrderManagement::Stock::Estimator.new(order).shipping_rates(
package: to_package, checkout: checkout, apply_tags: apply_tags)

keep_original_shipping_method_selection(original_shipping_method_id)

Expand Down
2 changes: 1 addition & 1 deletion app/services/distributor_shipping_methods.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class DistributorShippingMethods
def self.shipping_methods(distributor:, checkout: false, apply_tags: true, customer: nil)
def self.shipping_methods(distributor:, customer: nil, checkout: false, apply_tags: true)
return [] if distributor.blank?

shipping_methods = distributor.shipping_methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ def initialize(order)
@order = order
end

def packages
def packages(checkout: true, apply_tags: true)
packages = build_packages
packages = prioritize_packages(packages)
estimate_packages(packages)
estimate_packages(packages: packages, checkout: checkout, apply_tags: apply_tags)
end

# Build package with default stock location
Expand All @@ -32,10 +32,12 @@ def prioritize_packages(packages)
prioritizer.prioritized_packages
end

def estimate_packages(packages)
def estimate_packages(packages:, checkout: true, apply_tags: true)
estimator = OrderManagement::Stock::Estimator.new(order)
packages.each do |package|
package.shipping_rates = estimator.shipping_rates(package)
package.shipping_rates = estimator.shipping_rates(
package: package, checkout: checkout, apply_tags: apply_tags
)
end
packages
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ def initialize(order)
@currency = order.currency
end

def shipping_rates(package, frontend_only = true, apply_tags = true)
def shipping_rates(package:, checkout: true, apply_tags: true)
shipping_rates = []
shipping_methods = shipping_methods(package, apply_tags)
shipping_methods =
shipping_methods(package: package, checkout: checkout, apply_tags: apply_tags)
return [] unless shipping_methods

shipping_methods.each do |shipping_method|
Expand All @@ -23,7 +24,7 @@ def shipping_rates(package, frontend_only = true, apply_tags = true)
shipping_rates.sort_by! { |r| r.cost || 0 }

unless shipping_rates.empty?
if frontend_only
if checkout
shipping_rates.each do |rate|
if rate.shipping_method.frontend?
rate.selected = true
Expand All @@ -40,12 +41,8 @@ def shipping_rates(package, frontend_only = true, apply_tags = true)

private

def shipping_methods(package, apply_tags)
shipping_methods = if apply_tags
package.filtered_shipping_methods
else
package.shipping_methods
end
def shipping_methods(package:, checkout: true, apply_tags: true)
shipping_methods = package.shipping_methods(checkout: checkout, apply_tags: apply_tags)
shipping_methods.delete_if { |ship_method| !ship_method.calculator.available?(package) }
shipping_methods.delete_if { |ship_method| !ship_method.include?(order.ship_address) }
shipping_methods.delete_if { |ship_method|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,12 @@ def currency
# TODO calculate from first variant?
end

def shipping_methods
def shipping_methods(checkout: true, apply_tags: true)
DistributorShippingMethods.shipping_methods(
distributor: order.distributor,
checkout: false,
checkout: checkout,
customer: order.customer,
apply_tags: false
)
end

def filtered_shipping_methods
DistributorShippingMethods.shipping_methods(
distributor: order.distributor,
checkout: false,
customer: order.customer,
apply_tags: true
apply_tags: apply_tags
)
end

Expand Down

0 comments on commit 037f8fc

Please sign in to comment.