mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-02 06:51:40 +00:00
Look up the shipping services (pickup, delivery) that different hubs provide
This commit is contained in:
@@ -25,6 +25,22 @@ Spree::ShippingMethod.class_eval do
|
||||
|
||||
scope :by_name, order('spree_shipping_methods.name ASC')
|
||||
|
||||
|
||||
# Return the services (pickup, delivery) that different distributors provide, in the format:
|
||||
# {distributor_id => {pickup: true, delivery: false}, ...}
|
||||
def self.services
|
||||
Hash[
|
||||
Spree::ShippingMethod.
|
||||
joins(:distributor_shipping_methods).
|
||||
group('distributor_id').
|
||||
select("distributor_id").
|
||||
select("BOOL_OR(spree_shipping_methods.require_ship_address = 'f') AS pickup").
|
||||
select("BOOL_OR(spree_shipping_methods.require_ship_address = 't') AS delivery").
|
||||
map { |sm| [sm.distributor_id.to_i, {pickup: sm.pickup == 't', delivery: sm.delivery == 't'}] }
|
||||
]
|
||||
end
|
||||
|
||||
|
||||
def available_to_order_with_distributor_check?(order, display_on=nil)
|
||||
available_to_order_without_distributor_check?(order, display_on) &&
|
||||
self.distributors.include?(order.distributor)
|
||||
|
||||
@@ -55,5 +55,32 @@ module Spree
|
||||
sm.should be_available_to_order o
|
||||
end
|
||||
end
|
||||
|
||||
describe "finding services offered by all distributors" do
|
||||
let!(:d1) { create(:distributor_enterprise) }
|
||||
let!(:d2) { create(:distributor_enterprise) }
|
||||
let!(:d3) { create(:distributor_enterprise) }
|
||||
let!(:d4) { create(:distributor_enterprise) }
|
||||
let!(:d1_pickup) { create(:shipping_method, require_ship_address: false, distributors: [d1]) }
|
||||
let!(:d1_delivery) { create(:shipping_method, require_ship_address: true, distributors: [d1]) }
|
||||
let!(:d2_pickup) { create(:shipping_method, require_ship_address: false, distributors: [d2]) }
|
||||
let!(:d3_delivery) { create(:shipping_method, require_ship_address: true, distributors: [d3]) }
|
||||
|
||||
it "reports when the services are available" do
|
||||
ShippingMethod.services[d1.id].should == {pickup: true, delivery: true}
|
||||
end
|
||||
|
||||
it "reports when only pickup is available" do
|
||||
ShippingMethod.services[d2.id].should == {pickup: true, delivery: false}
|
||||
end
|
||||
|
||||
it "reports when only delivery is available" do
|
||||
ShippingMethod.services[d3.id].should == {pickup: false, delivery: true}
|
||||
end
|
||||
|
||||
it "returns no entry when no service is available" do
|
||||
ShippingMethod.services[d4.id].should be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user