mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
filter distributors before listing on checkout options
This commit is contained in:
@@ -36,6 +36,15 @@ module OrderCyclesHelper
|
||||
shipping_and_payment_methods: true
|
||||
end
|
||||
|
||||
def distributors_with_editable_shipping_and_payment_methods(order_cycle)
|
||||
return order_cycle.distributors if order_cycle.coordinator
|
||||
.in? Enterprise.managed_by(spree_current_user)
|
||||
|
||||
order_cycle.distributors.select do |distributor|
|
||||
distributor.in? Enterprise.managed_by(spree_current_user)
|
||||
end
|
||||
end
|
||||
|
||||
def order_cycle_status_class(order_cycle)
|
||||
if order_cycle.undated?
|
||||
'undated'
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
%th{ colspan: 2 }
|
||||
= t('.shipping_methods')
|
||||
= hidden_field_tag "order_cycle[selected_distributor_shipping_method_ids][]", ""
|
||||
- @order_cycle.distributors.each do |distributor|
|
||||
- distributors_with_editable_shipping_and_payment_methods(@order_cycle).each do |distributor|
|
||||
- distributor_shipping_methods = @order_cycle.attachable_distributor_shipping_methods.where("distributor_id = ?", distributor.id).includes(:shipping_method)
|
||||
%tr{ class: "distributor-#{distributor.id}-shipping-methods", "data-controller": "checked" }
|
||||
%td.text-center
|
||||
@@ -50,7 +50,7 @@
|
||||
%th{ colspan: 2 }
|
||||
= t('.payment_methods')
|
||||
= hidden_field_tag "order_cycle[selected_distributor_payment_method_ids][]", ""
|
||||
- @order_cycle.distributors.each do |distributor|
|
||||
- distributors_with_editable_shipping_and_payment_methods(@order_cycle).each do |distributor|
|
||||
- distributor_payment_methods = @order_cycle.attachable_distributor_payment_methods.where("distributor_id = ?", distributor.id).includes(:payment_method)
|
||||
%tr{ class: "distributor-#{distributor.id}-payment-methods", "data-controller": "checked" }
|
||||
%td.text-center
|
||||
|
||||
@@ -92,4 +92,41 @@ describe OrderCyclesHelper, type: :helper do
|
||||
expect(helper.pickup_time(oc2)).to eq("turtles")
|
||||
end
|
||||
end
|
||||
|
||||
describe "distibutors that have editable shipping/payment methods" do
|
||||
let(:hub1) { create(:distributor_enterprise, name: 'hub1') }
|
||||
let(:hub2) { create(:distributor_enterprise, name: 'hub2') }
|
||||
let(:supplier){ create(:supplier_enterprise, name: 'supplier') }
|
||||
let(:coordinator){ create(:distributor_enterprise, name: 'coordinator') }
|
||||
before do
|
||||
allow(oc).to receive(:coordinator).and_return coordinator
|
||||
allow(oc).to receive(:suppliers).and_return [supplier]
|
||||
allow(oc).to receive(:distributors).and_return [hub1, hub2]
|
||||
end
|
||||
context 'current user is a coordinator' do
|
||||
before do
|
||||
allow(helper).to receive(:spree_current_user).and_return coordinator.owner
|
||||
end
|
||||
|
||||
it 'returns all distributors' do
|
||||
expect(helper.distributors_with_editable_shipping_and_payment_methods(oc)).to eq [hub1, hub2]
|
||||
end
|
||||
end
|
||||
context 'current user is a producer' do
|
||||
before do
|
||||
allow(helper).to receive(:spree_current_user).and_return supplier.owner
|
||||
end
|
||||
it "doesn't return any distributors" do
|
||||
expect(helper.distributors_with_editable_shipping_and_payment_methods(oc)).to eq []
|
||||
end
|
||||
end
|
||||
context 'current user is a hub' do
|
||||
before do
|
||||
allow(helper).to receive(:spree_current_user).and_return hub1.owner
|
||||
end
|
||||
it "returns only the hubs of the current user" do
|
||||
expect(helper.distributors_with_editable_shipping_and_payment_methods(oc)).to eq [hub1]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user