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 Jan 9, 2021
1 parent 60e9ff0 commit e171bf1
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 40 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 spree.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 @@ -32,7 +32,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
7 changes: 4 additions & 3 deletions app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,14 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ module Stock

context "the order's ship address is in the same zone" do
it "returns shipping rates from a shipping method" do
shipping_rates = subject.shipping_rates(package)
shipping_rates = subject.shipping_rates(package: package)
expect(shipping_rates.first.cost).to eq 4.00
end
end

context "the order's ship address is in a different zone" do
it "still returns shipping rates from a shipping method" do
shipping_method.zones.each{ |z| z.members.delete_all }
shipping_rates = subject.shipping_rates(package)
shipping_rates = subject.shipping_rates(package: package)
expect(shipping_rates.first.cost).to eq 4.00
end
end
Expand All @@ -45,22 +45,22 @@ module Stock
it "does not return shipping rates from a shipping method" do
allow_any_instance_of(Spree::ShippingMethod).
to receive_message_chain(:calculator, :available?).and_return(false)
shipping_rates = subject.shipping_rates(package)
shipping_rates = subject.shipping_rates(package: package)
expect(shipping_rates).to eq []
end
end

context "the currency matches the order's currency" do
it "returns shipping rates from a shipping method" do
shipping_rates = subject.shipping_rates(package)
shipping_rates = subject.shipping_rates(package: package)
expect(shipping_rates.first.cost).to eq 4.00
end
end

context "the currency is different than the order's currency" do
it "does not return shipping rates from a shipping method" do
order.currency = "USD"
shipping_rates = subject.shipping_rates(package)
shipping_rates = subject.shipping_rates(package: package)
expect(shipping_rates).to eq []
end
end
Expand All @@ -77,7 +77,7 @@ module Stock
allow(subject).to receive(:shipping_methods).and_return(shipping_methods)

expected_costs = %w[3.00 4.00 5.00].map(&BigDecimal.method(:new))
expect(subject.shipping_rates(package).map(&:cost)).to eq expected_costs
expect(subject.shipping_rates(package: package).map(&:cost)).to eq expected_costs
end

context "general shipping methods" do
Expand All @@ -91,7 +91,7 @@ module Stock

allow(subject).to receive(:shipping_methods).and_return(shipping_methods)

shipping_rates = subject.shipping_rates(package)
shipping_rates = subject.shipping_rates(package: package)
expect(shipping_rates.sort_by(&:cost).map(&:selected)).to eq [true, false]
end

Expand All @@ -103,7 +103,7 @@ module Stock

allow(subject).to receive(:shipping_methods).and_return(shipping_methods)

subject.shipping_rates(package)
subject.shipping_rates(package: package)
end
end

Expand All @@ -119,7 +119,7 @@ module Stock
allow(subject).
to receive(:shipping_methods).and_return([backend_method, generic_method])

expect(subject.shipping_rates(package).map(&:selected)).to eq [false, true]
expect(subject.shipping_rates(package: package).map(&:selected)).to eq [false, true]
end
end
end
Expand Down

0 comments on commit e171bf1

Please sign in to comment.