Refactor OrderPopulator, add outside level spec

This commit is contained in:
Rohan Mitchell
2013-06-05 14:44:57 +10:00
parent 98f3197c08
commit fb4f533976
2 changed files with 105 additions and 11 deletions

View File

@@ -1,11 +1,8 @@
Spree::OrderPopulator.class_eval do
def populate_with_distribution_validation(from_hash)
@distributor = from_hash[:distributor_id].present? ?
Enterprise.is_distributor.find(from_hash[:distributor_id]) : nil
@order_cycle = from_hash[:order_cycle_id].present? ?
OrderCycle.find(from_hash[:order_cycle_id]) : nil
@distributor, @order_cycle = load_distributor_and_order_cycle(from_hash)
if @distributor && !DistributionChangeValidator.new(@order).can_change_to_distributor?(@distributor)
if !distributor_can_supply_products_in_cart(@distributor)
errors.add(:base, "That distributor can't supply all the products in your cart. Please choose another.")
end
@@ -13,12 +10,7 @@ Spree::OrderPopulator.class_eval do
# Set order distributor and order cycle
if valid?
# Using @order.reload or not performing any reload causes totals fields (ie. item_total)
# to be set to zero
@order = Spree::Order.find @order.id
@order.set_distributor! @distributor
@order.set_order_cycle! @order_cycle if @order_cycle
set_cart_distributor_and_order_cycle @distributor, @order_cycle
end
valid?
@@ -40,6 +32,31 @@ Spree::OrderPopulator.class_eval do
end
end
private
def load_distributor_and_order_cycle(from_hash)
distributor = from_hash[:distributor_id].present? ?
Enterprise.is_distributor.find(from_hash[:distributor_id]) : nil
order_cycle = from_hash[:order_cycle_id].present? ?
OrderCycle.find(from_hash[:order_cycle_id]) : nil
[distributor, order_cycle]
end
def distributor_can_supply_products_in_cart(distributor)
!distributor || DistributionChangeValidator.new(@order).can_change_to_distributor?(distributor)
end
def set_cart_distributor_and_order_cycle(distributor, order_cycle)
# Using @order.reload or not performing any reload causes totals fields (ie. item_total)
# to be set to zero
@order = Spree::Order.find @order.id
@order.set_distributor! distributor
@order.set_order_cycle! order_cycle if order_cycle
end
def check_distribution_provided_for(variant)
order_cycle_required = order_cycle_required_for(variant)
distribution_provided =

View File

@@ -0,0 +1,77 @@
require 'spec_helper'
module Spree
describe OrderPopulator do
let(:order) { double(:order) }
let(:currency) { double(:currency) }
let(:params) { double(:params) }
let(:distributor) { double(:distributor) }
let(:order_cycle) { double(:order_cycle) }
describe "populate" do
it "checks that distributor can supply all products in the cart" do
op = OrderPopulator.new(order, currency)
op.should_receive(:load_distributor_and_order_cycle).with(params).
and_return([distributor, order_cycle])
op.should_receive(:distributor_can_supply_products_in_cart).with(distributor).
and_return(false)
op.should_receive(:populate_without_distribution_validation).never
op.should_receive(:set_cart_distributor_and_order_cycle).never
op.populate(params).should be_false
op.errors.to_a.should == ["That distributor can't supply all the products in your cart. Please choose another."]
end
it "doesn't set cart distributor and order cycle if populate fails" do
op = OrderPopulator.new(order, currency)
op.should_receive(:load_distributor_and_order_cycle).with(params).
and_return([distributor, order_cycle])
op.should_receive(:distributor_can_supply_products_in_cart).with(distributor).
and_return(true)
op.class_eval do
def populate_without_distribution_validation(from_hash)
errors.add(:base, "Something went wrong.")
end
end
op.should_receive(:set_cart_distributor_and_order_cycle).never
op.populate(params).should be_false
op.errors.to_a.should == ["Something went wrong."]
end
it "sets cart distributor and order cycle when populate succeeds" do
op = OrderPopulator.new(order, currency)
op.should_receive(:load_distributor_and_order_cycle).with(params).
and_return([distributor, order_cycle])
op.should_receive(:distributor_can_supply_products_in_cart).with(distributor).
and_return(true)
op.should_receive(:populate_without_distribution_validation).with(params)
op.should_receive(:set_cart_distributor_and_order_cycle).with(distributor, order_cycle)
op.populate(params).should be_true
end
end
describe "attempt_cart_add" do
it "validates distribution is provided"
it "validates variant is available under distributor"
end
describe "validations" do
describe "checking distribution is provided for a variant" do
end
describe "checking variant is available under the distributor" do
end
describe "order cycle required for variant" do
end
end
end
end