From 0d2f92f013ec0d79ba8395a46b1928587ed85d55 Mon Sep 17 00:00:00 2001 From: Andy Brett Date: Sun, 11 Oct 2020 12:38:54 -0700 Subject: [PATCH] fix styles and specs --- app/services/distributor_shipping_methods.rb | 10 ++-- .../distributor_shipping_methods_spec.rb | 53 +++++++++++-------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/app/services/distributor_shipping_methods.rb b/app/services/distributor_shipping_methods.rb index 04e4b4503599..cb589ba97e7b 100644 --- a/app/services/distributor_shipping_methods.rb +++ b/app/services/distributor_shipping_methods.rb @@ -1,16 +1,18 @@ # frozen_string_literal: true class DistributorShippingMethods - def self.shipping_methods(distributor, checkout = false, customer = nil) + def self.shipping_methods(distributor:, checkout: false, apply_tags: true, customer: nil) return [] if distributor.blank? shipping_methods = distributor.shipping_methods shipping_methods = shipping_methods.display_on_checkout if checkout shipping_methods = shipping_methods.to_a - OpenFoodNetwork::TagRuleApplicator.new( - distributor, "FilterShippingMethods", customer&.tag_list - ).filter!(shipping_methods) + if apply_tags + OpenFoodNetwork::TagRuleApplicator.new( + distributor, "FilterShippingMethods", customer&.tag_list + ).filter!(shipping_methods) + end shipping_methods.uniq end diff --git a/spec/services/distributor_shipping_methods_spec.rb b/spec/services/distributor_shipping_methods_spec.rb index 930568680f7f..489c55d5e3bb 100644 --- a/spec/services/distributor_shipping_methods_spec.rb +++ b/spec/services/distributor_shipping_methods_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe DistributorShippingMethods do @@ -6,33 +8,38 @@ let!(:member_shipping_method) { create(:shipping_method, distributors: [enterprise]) } let!(:backend_shipping_method) { create(:shipping_method, distributors: [enterprise]) } let!(:customer) { create(:customer) } - let!(:sm_tag_rule) { create( - :filter_shipping_methods_tag_rule, - enterprise: enterprise, - priority: 2, - preferred_customer_tags: "member", - preferred_shipping_method_tags: "member", - preferred_matched_shipping_methods_visibility: "visible" - ) } + let!(:sm_tag_rule) { + create( + :filter_shipping_methods_tag_rule, + enterprise: enterprise, + priority: 2, + preferred_customer_tags: "member", + preferred_shipping_method_tags: "member", + preferred_matched_shipping_methods_visibility: "visible" + ) + } - let!(:default_sm_tag_rule) { create( - :filter_shipping_methods_tag_rule, - enterprise: enterprise, - priority: 1, - is_default: true, - preferred_shipping_method_tags: [], - preferred_customer_tags: [], - preferred_matched_shipping_methods_visibility: "hidden" - ) } + let!(:default_sm_tag_rule) { + create( + :filter_shipping_methods_tag_rule, + enterprise: enterprise, + priority: 1, + is_default: true, + preferred_shipping_method_tags: [], + preferred_customer_tags: [], + preferred_matched_shipping_methods_visibility: "hidden" + ) + } it "returns all shipping methods for a distributor" do - expect(DistributorShippingMethods.shipping_methods(enterprise).count).to eq(3) + expect(DistributorShippingMethods.shipping_methods(distributor: enterprise).count).to eq(3) end it "does not return a shipping method tagged as 'member' for a customer without that tag" do member_shipping_method.tag_list << "member" member_shipping_method.save - result = DistributorShippingMethods.shipping_methods(enterprise, false, customer) + result = + DistributorShippingMethods.shipping_methods(distributor: enterprise, customer: customer) expect(result).to include(shipping_method) expect(result).to include(backend_shipping_method) end @@ -43,14 +50,18 @@ member_customer.save member_shipping_method.tag_list << "member" member_shipping_method.save - result = DistributorShippingMethods.shipping_methods(enterprise, false, member_customer) + result = DistributorShippingMethods.shipping_methods( + distributor: enterprise, customer: member_customer + ) expect(result).to include(member_shipping_method) end it "does not return a non-checkout shipping method if passed checkout=true" do backend_shipping_method.display_on = "back_end" backend_shipping_method.save - result = DistributorShippingMethods.shipping_methods(enterprise, true, customer) + result = DistributorShippingMethods.shipping_methods( + distributor: enterprise, checkout: true, customer: customer + ) expect(result).to include(shipping_method) end end