Skip to content

Commit

Permalink
fix styles and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewpbrett committed Oct 11, 2020
1 parent 4e16887 commit 0d2f92f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
10 changes: 6 additions & 4 deletions app/services/distributor_shipping_methods.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
53 changes: 32 additions & 21 deletions spec/services/distributor_shipping_methods_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

describe DistributorShippingMethods do
Expand All @@ -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
Expand All @@ -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

0 comments on commit 0d2f92f

Please sign in to comment.