mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-30 21:27:17 +00:00
Show line items for each order in multi-cart.
This commit is contained in:
@@ -17,8 +17,3 @@ angular.module('store', ['ngResource']).
|
||||
.config(['$httpProvider', ($httpProvider) ->
|
||||
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
|
||||
])
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
3
app/views/open_food_web/line_items/index.v1.rabl
Normal file
3
app/views/open_food_web/line_items/index.v1.rabl
Normal file
@@ -0,0 +1,3 @@
|
||||
collection @line_items
|
||||
|
||||
extends "open_food_web/line_items/show"
|
||||
4
app/views/open_food_web/line_items/show.v1.rabl
Normal file
4
app/views/open_food_web/line_items/show.v1.rabl
Normal file
@@ -0,0 +1,4 @@
|
||||
object @line_item
|
||||
attributes :id, :quantity
|
||||
|
||||
node(:name) { |p| p.variant.name }
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user