Add multi-distributor scope to shipping and payment methods

This commit is contained in:
Kristina Lim
2018-10-16 16:47:21 +08:00
committed by luisramos0
parent 24ab2e7fb0
commit 5fce9d0a7d
4 changed files with 53 additions and 6 deletions

View File

@@ -23,6 +23,11 @@ Spree::PaymentMethod.class_eval do
end
}
scope :for_distributors, ->(distributors) {
non_unique_matches = unscoped.joins(:distributors).where(enterprises: { id: distributors })
where(id: non_unique_matches.map(&:id))
}
scope :for_distributor, lambda { |distributor|
joins(:distributors).
where('enterprises.id = ?', distributor)

View File

@@ -20,6 +20,10 @@ Spree::ShippingMethod.class_eval do
end
}
scope :for_distributors, ->(distributors) {
non_unique_matches = unscoped.joins(:distributors).where(enterprises: { id: distributors })
where(id: non_unique_matches.map(&:id))
}
scope :for_distributor, lambda { |distributor|
joins(:distributors).
where('enterprises.id = ?', distributor)

View File

@@ -39,5 +39,24 @@ module Spree
order.add_variant(product.master)
expect(transaction.compute_amount(order)).to eq 2.0
end
describe "scope" do
describe "filtering to specified distributors" do
let!(:distributor_a) { create(:distributor_enterprise) }
let!(:distributor_b) { create(:distributor_enterprise) }
let!(:distributor_c) { create(:distributor_enterprise) }
let!(:payment_method_a) { create(:payment_method, distributors: [distributor_a, distributor_b]) }
let!(:payment_method_b) { create(:payment_method, distributors: [distributor_b]) }
let!(:payment_method_c) { create(:payment_method, distributors: [distributor_c]) }
it "includes only unique records under specified distributors" do
result = described_class.for_distributors([distributor_a, distributor_b])
expect(result.length).to eq(2)
expect(result).to include(payment_method_a)
expect(result).to include(payment_method_b)
end
end
end
end
end

View File

@@ -18,13 +18,32 @@ module Spree
sm.reload.distributors.should match_array [d1, d2]
end
it "finds shipping methods for a particular distributor" do
d1 = create(:distributor_enterprise)
d2 = create(:distributor_enterprise)
sm1 = create(:shipping_method, distributors: [d1])
sm2 = create(:shipping_method, distributors: [d2])
describe "scope" do
describe "filtering to specified distributors" do
let!(:distributor_a) { create(:distributor_enterprise) }
let!(:distributor_b) { create(:distributor_enterprise) }
let!(:distributor_c) { create(:distributor_enterprise) }
ShippingMethod.for_distributor(d1).should == [sm1]
let!(:shipping_method_a) { create(:shipping_method, distributors: [distributor_a, distributor_b]) }
let!(:shipping_method_b) { create(:shipping_method, distributors: [distributor_b]) }
let!(:shipping_method_c) { create(:shipping_method, distributors: [distributor_c]) }
it "includes only unique records under specified distributors" do
result = described_class.for_distributors([distributor_a, distributor_b])
expect(result.length).to eq(2)
expect(result).to include(shipping_method_a)
expect(result).to include(shipping_method_b)
end
end
it "finds shipping methods for a particular distributor" do
d1 = create(:distributor_enterprise)
d2 = create(:distributor_enterprise)
sm1 = create(:shipping_method, distributors: [d1])
sm2 = create(:shipping_method, distributors: [d2])
ShippingMethod.for_distributor(d1).should == [sm1]
end
end
it "orders shipping methods by name" do