mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #7512 from Matt-Yorkley/package-refactor
Package update
This commit is contained in:
@@ -75,28 +75,13 @@ module OrderManagement
|
||||
end
|
||||
end
|
||||
|
||||
def currency
|
||||
# TODO calculate from first variant?
|
||||
end
|
||||
|
||||
# Returns all existing shipping categories.
|
||||
# It disables the matching of product shipping category with shipping method's category
|
||||
# It allows checkout of products with categories that are not the ship method's categories
|
||||
#
|
||||
# @return [Array<Spree::ShippingCategory>]
|
||||
def shipping_categories
|
||||
Spree::ShippingCategory.all
|
||||
end
|
||||
|
||||
# Skips the methods that are not used by the order's distributor
|
||||
# Returns the shipping methods that are enabled by the order's distributor
|
||||
#
|
||||
# @return [Array<Spree::ShippingMethod>]
|
||||
def shipping_methods
|
||||
available_shipping_methods = shipping_categories.flat_map(&:shipping_methods).uniq.to_a
|
||||
return [] unless order.distributor.present?
|
||||
|
||||
available_shipping_methods.keep_if do |shipping_method|
|
||||
ships_with?(order.distributor.shipping_methods.to_a, shipping_method)
|
||||
end
|
||||
order.distributor.shipping_methods.uniq.to_a
|
||||
end
|
||||
|
||||
def inspect
|
||||
@@ -124,17 +109,6 @@ module OrderManagement
|
||||
|
||||
shipment
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Checks whether the given distributor provides the specified shipping method
|
||||
#
|
||||
# @param shipping_methods [Array<Spree::ShippingMethod>]
|
||||
# @param shipping_method [Spree::ShippingMethod]
|
||||
# @return [Boolean]
|
||||
def ships_with?(shipping_methods, shipping_method)
|
||||
shipping_methods.include?(shipping_method)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -158,20 +158,23 @@ module OrderManagement
|
||||
|
||||
let(:shipping_method1) { create(:shipping_method, distributors: [enterprise]) }
|
||||
let(:shipping_method2) { create(:shipping_method, distributors: [other_enterprise]) }
|
||||
let!(:shipping_method3) {
|
||||
create(:shipping_method, distributors: [enterprise], deleted_at: Time.zone.now)
|
||||
}
|
||||
|
||||
describe '#shipping_methods' do
|
||||
it 'does not return shipping methods not used by the package\'s order distributor' do
|
||||
describe "#shipping_methods" do
|
||||
it "does not return shipping methods not used by the package's order distributor" do
|
||||
expect(package.shipping_methods).to eq [shipping_method1]
|
||||
end
|
||||
end
|
||||
|
||||
describe '#shipping_categories' do
|
||||
it "returns ship categories that are not the ship categories of the order's products" do
|
||||
package
|
||||
other_shipping_category = Spree::ShippingCategory.create(name: "Custom")
|
||||
it "does not return soft-deleted shipping methods" do
|
||||
expect(package.shipping_methods).to_not include shipping_method3
|
||||
end
|
||||
|
||||
expect(package.shipping_categories).to eq [shipping_method1.shipping_categories.first,
|
||||
other_shipping_category]
|
||||
it "returns an empty array if distributor is nil" do
|
||||
allow(order).to receive(:distributor) { nil }
|
||||
|
||||
expect(package.shipping_methods).to eq []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user