Massaging the form to push orders to the cart: some refactoring still required

This commit is contained in:
Will Marshall
2013-12-19 15:51:51 +11:00
parent 1048bab303
commit 985cebb44a
4 changed files with 45 additions and 5 deletions

View File

@@ -12,6 +12,8 @@ Spree::OrdersController.class_eval do
end
populator = Spree::OrderPopulator.new(current_order(true), current_currency)
params[:distributor_id] = current_order.distributor.id
params[:order_cycle_id] = current_order_cycle.id
if populator.populate(params.slice(:products, :variants, :quantity, :distributor_id, :order_cycle_id))
fire_event('spree.cart.add')
fire_event('spree.order.contents_changed')

View File

@@ -1,5 +1,5 @@
%products{"ng-controller" => "ProductsCtrl"}
= form_for :order, :url => populate_orders_path, :class => "custom" do
= form_for :order, :url => populate_orders_path, html: {:class => "custom"} do
%input.button.right{type: :submit, value: "Check Out"}
%table
%thead
@@ -21,7 +21,7 @@
%td.notes {{ product.description | truncate:250 }}
%td {{ product.master.options_text }}
%td
%input{type: :number, value: 0, min: 0, name: "quantity_variant_{{product.master.id}}"}
%input{type: :number, value: 0, min: 0, name: "variants[{{product.master.id}}]"}
%td.group_buy
%span{"ng-show" => "product.group_buy"}
Available
@@ -34,7 +34,7 @@
%td{colspan: 3}
%td {{variant.options_text}}
%td
%input{type: :number, value: 0, min: 0, name: "quantity_variant_{{variant.id}}"}
%input{type: :number, value: 0, min: 0, name: "variants[{{variant.id}}]"}
%td.group_buy
%span{"ng-show" => "product.group_buy"}
Available

View File

@@ -141,6 +141,28 @@ describe Spree::OrdersController do
end
end
context "#populate" do
let(:user) { create(:user) }
let(:order) { mock_model(Spree::Order, :number => "R123", :reload => nil, :save! => true, :coupon_code => nil, :user => user, :completed? => false, :currency => "USD", :token => 'a1b2c3d4')}
let(:populator) { double('OrderPopulator') }
before do
order.stub(:last_ip_address=)
Spree::Order.stub(:find).and_return(order)
Spree::OrderPopulator.should_receive(:new).and_return(populator)
Spree::Order.stub(:new).and_return(order)
if Spree::BaseController.spree_responders[:OrdersController].present?
Spree::BaseController.spree_responders[:OrdersController].clear
end
end
context "with Variant" do
it "should handle multiple variants, each with their own quantity" do
populator.should_receive(:populate).with("variants" => { 1 => "10", 3 => "7" }).and_return(true)
spree_post :populate, { order_id: order.id, :variants => {1 => 10, 3 => 7} }
end
end
end
private
def num_items_in_cart

View File

@@ -54,6 +54,7 @@ feature "As a consumer I want to shop with a distributor", js: true do
it "allows us to select an order cycle" do
select "frogs", :from => "order_cycle_id"
Spree::Order.last.order_cycle.should == nil
page.should have_selector "products"
page.should have_content "Orders close #{oc1.orders_close_at.strftime('%A %m')}"
Spree::Order.last.order_cycle.should == oc1
@@ -76,8 +77,23 @@ feature "As a consumer I want to shop with a distributor", js: true do
end
describe "adding products to cart" do
it "should let us add products to our cart"
it "should redirect to the checkout page"
let(:oc) { create(:simple_order_cycle, distributors: [distributor]) }
let(:product) { create(:simple_product) }
let(:variant) { create(:variant, product: product) }
before do
exchange = Exchange.find(oc.exchanges.to_enterprises(distributor).outgoing.first.id)
exchange.update_attribute :pickup_time, "frogs"
exchange.variants << product.master
exchange.variants << variant
visit shop_path
select "frogs", :from => "order_cycle_id"
end
it "should let us add products to our cart" do
fill_in "quantity_variant_#{variant.id}", with: "1"
find("form.custom > input.button.right:first-child").click
current_path.should == "/cart"
page.should have_content product.name
end
end
context "when no order cycles are available" do