diff --git a/app/assets/javascripts/store/controllers/cart.js.coffee b/app/assets/javascripts/store/controllers/cart.js.coffee index 319418d548..18f876eaa5 100644 --- a/app/assets/javascripts/store/controllers/cart.js.coffee +++ b/app/assets/javascripts/store/controllers/cart.js.coffee @@ -17,8 +17,3 @@ angular.module('store', ['ngResource']). .config(['$httpProvider', ($httpProvider) -> $httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content') ]) - - - - - diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index b6be15431d..a3b38840f2 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -7,7 +7,7 @@ Spree::OrdersController.class_eval do # Patch Orders#populate to provide distributor_id and order_cycle_id to OrderPopulator def populate if OpenFoodWeb::FeatureToggle.enabled? :multi_cart - populate_cart params[:variants] + populate_cart params.slice(:products, :variants, :quantity, :distributor_id, :order_cycle_id) end populator = Spree::OrderPopulator.new(current_order(true), current_currency) @@ -81,7 +81,7 @@ Spree::OrdersController.class_eval do end end - def populate_cart variants + def populate_cart hash if spree_current_user unless spree_current_user.cart spree_current_user.build_cart @@ -89,10 +89,7 @@ Spree::OrdersController.class_eval do spree_current_user.cart = cart spree_current_user.save end - variants.each do |variant_id, quantity| - variant = Spree::Variant.find(variant_id) - spree_current_user.cart.add_variant variant, quantity - end + spree_current_user.cart.add_products hash, current_currency end end end diff --git a/app/models/cart.rb b/app/models/cart.rb index 32a0ad33da..f604869ee5 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -1,12 +1,15 @@ class Cart < ActiveRecord::Base - has_many :orders, :class_name => 'Spree::Order' - belongs_to :user, :class_name => Spree.user_class + has_many :orders, :class_name => 'Spree::Order' + belongs_to :user, :class_name => Spree.user_class - def add_variant variant, quantity + def add_products hash, currency if orders.empty? order = Spree::Order.create - order.add_variant(variant, quantity) orders << order end + + order = orders.first + populator = Spree::OrderPopulator.new(order, currency) + populator.populate(hash) end end diff --git a/app/views/open_food_web/cart/_show.html.haml b/app/views/open_food_web/cart/_show.html.haml index 73f136b9db..b9c6da6e72 100644 --- a/app/views/open_food_web/cart/_show.html.haml +++ b/app/views/open_food_web/cart/_show.html.haml @@ -1,8 +1,18 @@ / %script = Spree.api_key = raw(try_spree_current_user.try(:spree_api_key).to_s.inspect) -Hello -%div{ 'ng-app' => 'store', 'ng-controller' => 'CartCtrl', 'ng-init' => "loadCart(#{try_spree_current_user.andand.cart.andand.id});" } +Current cart for: += spree_current_user.andand.email +%div{ 'ng-app' => 'store', 'ng-controller' => 'CartCtrl', 'ng-init' => "loadCart(#{spree_current_user.andand.cart.andand.id});" } {{cart}} {{state}} %ul %li(ng-repeat="order in cart.orders") - {{order.distributor}} + Distributor: {{order.distributor}} + %br + Order cycle: {{order.order_cycle.andand.name}} + %br + From: {{order.order_cycle.andand.orders_open_at}} To: {{order.order_cycle.andand.orders_close_at}} + %ul + %li(ng-repeat="line_item in order.line_items") + Product: {{line_item.name}} Quantity: {{line_item.quantity}} + %button Buy me + diff --git a/app/views/open_food_web/line_items/index.v1.rabl b/app/views/open_food_web/line_items/index.v1.rabl new file mode 100644 index 0000000000..f3b5e4cb18 --- /dev/null +++ b/app/views/open_food_web/line_items/index.v1.rabl @@ -0,0 +1,3 @@ +collection @line_items + +extends "open_food_web/line_items/show" \ No newline at end of file diff --git a/app/views/open_food_web/line_items/show.v1.rabl b/app/views/open_food_web/line_items/show.v1.rabl new file mode 100644 index 0000000000..cf790085fe --- /dev/null +++ b/app/views/open_food_web/line_items/show.v1.rabl @@ -0,0 +1,4 @@ +object @line_item +attributes :id, :quantity + +node(:name) { |p| p.variant.name } diff --git a/app/views/open_food_web/orders/show.v1.rabl b/app/views/open_food_web/orders/show.v1.rabl index d3812cb9b8..084ef3055f 100644 --- a/app/views/open_food_web/orders/show.v1.rabl +++ b/app/views/open_food_web/orders/show.v1.rabl @@ -2,3 +2,6 @@ object @order attributes :id node( :distributor ) { |p| p.distributor.blank? ? "" : p.distributor.name } +node( :line_items ) do |p| + partial '/open_food_web/line_items/index', object: p.line_items +end \ No newline at end of file diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb index 2657c37509..1f17515ee4 100644 --- a/spec/controllers/cart_controller_spec.rb +++ b/spec/controllers/cart_controller_spec.rb @@ -8,6 +8,7 @@ module OpenFoodWeb let(:user) { FactoryGirl.create(:user) } let(:product1) { FactoryGirl.create(:product) } let(:cart) { Cart.create(user: user) } + let(:distributor) { FactoryGirl.create(:distributor_enterprise) } before do end @@ -24,9 +25,8 @@ module OpenFoodWeb json_response['orders'].size.should == 0 end - context 'with an order' do - - let(:order) { FactoryGirl.create(:order_with_totals_and_distributor) } + context 'with an empty order' do + let(:order) { FactoryGirl.create(:order, distributor: distributor) } before(:each) do cart.orders << order @@ -39,8 +39,34 @@ module OpenFoodWeb json_response['orders'].size.should == 1 json_response['orders'].first['distributor'].should == order.distributor.name + json_response['orders'].first['line_items'].size.should == 0 end end + + context 'an order with line items' do + let(:product) { FactoryGirl.create(:product, distributors: [ distributor ]) } + let(:order) { FactoryGirl.create(:order, { distributor: distributor } ) } + let(:line_item) { FactoryGirl.create(:line_item, { variant: product.master }) } + + before(:each) do + order.line_items << line_item + order.save + cart.orders << order + cart.save! + end + + it "retrieves a cart with a single order and line item" do + get :show, {id: cart, :format => :json } + json_response = JSON.parse(response.body) + + json_response['orders'].size.should == 1 + json_response['orders'].first['distributor'].should == order.distributor.name + json_response['orders'].first['line_items'].first["name"].should == product.name + json_response['orders'].first['line_items'].first["quantity"].should == line_item.quantity + end + end + + context 'an order for an order cycle' end end end