Change :available_shipping_methods helper to use new OrderAvailableShippingMethods service.

This commit is contained in:
Cillian O'Ruanaidh
2022-06-08 21:19:04 +01:00
committed by Filipe
parent 855ec1a708
commit bff7650b35
2 changed files with 1 additions and 143 deletions

View File

@@ -14,15 +14,7 @@ module EnterprisesHelper
end
def available_shipping_methods
return [] if current_distributor.blank?
shipping_methods = current_distributor.shipping_methods.frontend.to_a
applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor,
"FilterShippingMethods", current_customer&.tag_list)
applicator.filter!(shipping_methods)
shipping_methods.uniq
OrderAvailableShippingMethods.new(current_order, current_customer).to_a
end
def available_payment_methods

View File

@@ -9,140 +9,6 @@ describe EnterprisesHelper, type: :helper do
before { allow(helper).to receive(:spree_current_user) { user } }
describe "loading available shipping methods" do
let!(:distributor_shipping_method) {
create(:shipping_method, require_ship_address: false, distributors: [distributor])
}
let!(:other_distributor_shipping_method) {
create(:shipping_method, require_ship_address: false, distributors: [some_other_distributor])
}
context "when the order has no current_distributor" do
before do
allow(helper).to receive(:current_distributor) { nil }
end
it "returns an empty array" do
expect(helper.available_shipping_methods).to eq []
end
end
context "when no tag rules are in effect" do
before { allow(helper).to receive(:current_distributor) { distributor } }
it "finds the shipping methods for the current distributor" do
expect(helper.available_shipping_methods).to_not include other_distributor_shipping_method
expect(helper.available_shipping_methods).to include distributor_shipping_method
end
it "does not return 'back office only' shipping method" do
backoffice_only_shipping_method = create(:shipping_method, require_ship_address: false,
distributors: [distributor], display_on: 'back_end')
expect(helper.available_shipping_methods).to_not include backoffice_only_shipping_method
expect(helper.available_shipping_methods).to_not include other_distributor_shipping_method
expect(helper.available_shipping_methods).to include distributor_shipping_method
end
end
context "when FilterShippingMethods tag rules are in effect" do
let(:customer) { create(:customer, user: user, enterprise: distributor) }
let!(:tag_rule) {
create(:filter_shipping_methods_tag_rule,
enterprise: distributor,
preferred_customer_tags: "local",
preferred_shipping_method_tags: "local-delivery")
}
let!(:default_tag_rule) {
create(:filter_shipping_methods_tag_rule,
enterprise: distributor,
is_default: true,
preferred_shipping_method_tags: "local-delivery")
}
let!(:tagged_sm) { distributor_shipping_method }
let!(:untagged_sm) { other_distributor_shipping_method }
before do
tagged_sm.update_attribute(:tag_list, 'local-delivery')
distributor.shipping_methods = [tagged_sm, untagged_sm]
allow(helper).to receive(:current_distributor) { distributor }
end
context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do
before {
tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility, 'visible')
}
before {
default_tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility,
'hidden')
}
context "when the customer is nil" do
it "applies default action (hide)" do
expect(helper.current_customer).to be nil
expect(helper.available_shipping_methods).to include untagged_sm
expect(helper.available_shipping_methods).to_not include tagged_sm
end
end
context "when the customer's tags match" do
before { customer.update_attribute(:tag_list, 'local') }
it "applies the action (show)" do
expect(helper.current_customer).to eq customer
expect(helper.available_shipping_methods).to include tagged_sm, untagged_sm
end
end
context "when the customer's tags don't match" do
before { customer.update_attribute(:tag_list, 'something') }
it "applies the default action (hide)" do
expect(helper.current_customer).to eq customer
expect(helper.available_shipping_methods).to include untagged_sm
expect(helper.available_shipping_methods).to_not include tagged_sm
end
end
end
context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do
before {
tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility, 'hidden')
}
before {
default_tag_rule.update_attribute(:preferred_matched_shipping_methods_visibility,
'visible')
}
context "when the customer is nil" do
it "applies default action (show)" do
expect(helper.current_customer).to be nil
expect(helper.available_shipping_methods).to include tagged_sm, untagged_sm
end
end
context "when the customer's tags match" do
before { customer.update_attribute(:tag_list, 'local') }
it "applies the action (hide)" do
expect(helper.current_customer).to eq customer
expect(helper.available_shipping_methods).to include untagged_sm
expect(helper.available_shipping_methods).to_not include tagged_sm
end
end
context "when the customer's tags don't match" do
before { customer.update_attribute(:tag_list, 'something') }
it "applies the default action (show)" do
expect(helper.current_customer).to eq customer
expect(helper.available_shipping_methods).to include tagged_sm, untagged_sm
end
end
end
end
end
describe "loading available payment methods" do
let!(:pm1) { create(:payment_method, distributors: [distributor]) }
let!(:pm2) { create(:payment_method, distributors: [some_other_distributor]) }