SplitProductsByDistribution deals with order cycles, and is tested by stubs

This commit is contained in:
Rohan Mitchell
2013-04-05 14:05:30 +11:00
parent 3aa48907a6
commit e735a709bf
2 changed files with 80 additions and 18 deletions

View File

@@ -5,15 +5,28 @@ module OpenFoodWeb
# Spree::BaseHelper decorator (for taxon counts).
module SplitProductsByDistribution
# If a distributor is provided, split the list of products into local (at that
# distributor) and remote (at another distributor). If a distributor is not
# If a distributor or order cycle is provided, split the list of products into local (at that
# distributor/order cycle) and remote (available elsewhere). If a distributor is not
# provided, perform no split.
def split_products_by_distribution(products, distributor, order_cycle)
products_local = products_remote = nil
if distributor
products_local = products.select { |p| p.distributors.include? distributor }
products_remote = products.reject { |p| p.distributors.include? distributor }
if distributor || order_cycle
selector = proc do |product|
# This should do the right thing, but is a little more mind-bending
# (!distributor || p.in_distributor?(distributor)) && (!order_cycle || p.in_order_cycle?(order_cycle))
if distributor && order_cycle
product.in_distributor?(distributor) && product.in_order_cycle?(order_cycle)
elsif distributor
product.in_distributor?(distributor)
else
product.in_order_cycle?(order_cycle)
end
end
products_local = products.select &selector
products_remote = products.reject &selector
products = nil
end

View File

@@ -1,4 +1,4 @@
require 'spec_helper'
require 'open_food_web/split_products_by_distribution'
describe OpenFoodWeb::SplitProductsByDistribution do
let(:products_splitter) { Class.new { include OpenFoodWeb::SplitProductsByDistribution } }
@@ -6,7 +6,7 @@ describe OpenFoodWeb::SplitProductsByDistribution do
it "does nothing when no distributor or order cycle is selected" do
orig_products = (1..3).map { |i| build(:product) }
orig_products = [double(:product)]
products, products_local, products_remote = subject.split_products_by_distribution orig_products, nil, nil
@@ -15,20 +15,69 @@ describe OpenFoodWeb::SplitProductsByDistribution do
products_remote.should be_nil
end
it "splits products by product distribution when a distributor is selected" do
d1 = build(:distributor_enterprise)
d2 = build(:distributor_enterprise)
orig_products = [build(:product, :distributors => [d1]),
build(:product, :distributors => [d2])]
it "splits products by distributor when a distributor is selected" do
distributor = double(:distributor)
local_product, remote_product = double(:product), double(:product)
local_product.should_receive(:in_distributor?).any_number_of_times.
with(distributor).and_return(true)
remote_product.should_receive(:in_distributor?).any_number_of_times.
with(distributor).and_return(false)
products, products_local, products_remote = subject.split_products_by_distribution orig_products, d1, nil
products, products_local, products_remote = subject.split_products_by_distribution [local_product, remote_product], distributor, nil
products.should be_nil
products_local.should == [orig_products[0]]
products_remote.should == [orig_products[1]]
products_local.should == [local_product]
products_remote.should == [remote_product]
end
it "splits products by order cycle distribution when a distributor is selected"
it "splits products by order cycle when an order cycle is selected"
it "splits products by both order cycle and distributor when both are selected"
it "splits products by order cycle when an order cycle is selected" do
order_cycle = double(:order_cycle)
local_product, remote_product = double(:product), double(:product)
local_product.should_receive(:in_order_cycle?).any_number_of_times.
with(order_cycle).and_return(true)
remote_product.should_receive(:in_order_cycle?).any_number_of_times.
with(order_cycle).and_return(false)
products, products_local, products_remote = subject.split_products_by_distribution [local_product, remote_product], nil, order_cycle
products.should be_nil
products_local.should == [local_product]
products_remote.should == [remote_product]
end
it "splits products by both order cycle and distributor when both are selected" do
distributor = double(:distributor)
order_cycle = double(:order_cycle)
neither_product, distributor_product, order_cycle_product, both_product =
double(:product), double(:product), double(:product), double(:product)
neither_product.should_receive(:in_distributor?).any_number_of_times.
with(distributor).and_return(false)
neither_product.should_receive(:in_order_cycle?).any_number_of_times.
with(order_cycle).and_return(false)
distributor_product.should_receive(:in_distributor?).any_number_of_times.
with(distributor).and_return(true)
distributor_product.should_receive(:in_order_cycle?).any_number_of_times.
with(order_cycle).and_return(false)
order_cycle_product.should_receive(:in_distributor?).any_number_of_times.
with(distributor).and_return(false)
order_cycle_product.should_receive(:in_order_cycle?).any_number_of_times.
with(order_cycle).and_return(true)
both_product.should_receive(:in_distributor?).any_number_of_times.
with(distributor).and_return(true)
both_product.should_receive(:in_order_cycle?).any_number_of_times.
with(order_cycle).and_return(true)
orig_products = [neither_product, distributor_product, order_cycle_product, both_product]
products, products_local, products_remote = subject.split_products_by_distribution orig_products, distributor, order_cycle
products.should be_nil
products_local.should == [both_product]
products_remote.should == [neither_product, distributor_product, order_cycle_product]
end
end