From 985cebb44a686afde29758f06c0df106ed4219e0 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Thu, 19 Dec 2013 15:51:51 +1100 Subject: [PATCH] Massaging the form to push orders to the cart: some refactoring still required --- .../spree/orders_controller_decorator.rb | 2 ++ app/views/shop/_products.html.haml | 6 ++--- .../spree/orders_controller_spec.rb | 22 +++++++++++++++++++ spec/features/consumer/shopping_spec.rb | 20 +++++++++++++++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 308aa55046..aceeeaab17 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -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') diff --git a/app/views/shop/_products.html.haml b/app/views/shop/_products.html.haml index c418b90e8f..3e50e929a9 100644 --- a/app/views/shop/_products.html.haml +++ b/app/views/shop/_products.html.haml @@ -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 diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 2d5dd64576..a8869cd63e 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -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 diff --git a/spec/features/consumer/shopping_spec.rb b/spec/features/consumer/shopping_spec.rb index 68d8983ca2..e5bd0c3cc7 100644 --- a/spec/features/consumer/shopping_spec.rb +++ b/spec/features/consumer/shopping_spec.rb @@ -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