Add simple angular cart to the main page.

This commit is contained in:
Andrew Spinks
2013-08-08 18:21:36 +10:00
parent ac37dff946
commit bc57364c7e
16 changed files with 122 additions and 62 deletions

View File

@@ -9,5 +9,7 @@
//= require store/spree_core
//= require store/spree_auth
//= require store/spree_promo
//= require shared/angular
//= require shared/angular-resource
//= require_tree .

View File

@@ -0,0 +1,18 @@
'use strict'
angular.module('store', ['ngResource']).
controller 'CartCtrl', ($scope, $window, CartFactory) ->
$scope.loadCart = ->
$scope.cart = CartFactory.load(1)
$scope.addVariant = (variant, quantity) ->
.config(['$httpProvider', ($httpProvider) ->
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
])

View File

@@ -0,0 +1,12 @@
'use strict'
angular.module('store').
factory 'CartFactory', ($resource, $window, $http) ->
Cart = $resource '/open_food_web/cart/:cart_id.json', {},
{ 'show': { method: 'GET'} }
load: (id, callback) ->
Cart.show {cart_id: id}, (cart) ->
callback(cart)

View File

@@ -1,21 +0,0 @@
class CartController < Spree::Api::BaseController
respond_to :json
def new
@cart = Cart.new(current_api_user)
if @cart.save
respond_with(@cart, :status => 201)
else
invalid_resource!(@cart)
end
end
def show
@cart = Cart.find(params[:id])
respond_with(@cart)
end
def add_product
end
end

View File

@@ -0,0 +1,25 @@
module OpenFoodWeb
class CartController < ApplicationController
respond_to :json
# before_filter :authorize_read!, :except => [:index, :search, :create]
def new
@cart = Cart.new(current_api_user)
if @cart.save
respond_with(@cart, :status => 201)
else
invalid_resource!(@cart)
end
end
def show
@cart = Cart.find(params[:id])
respond_with(@cart)
end
def add_product
end
end
end

View File

@@ -0,0 +1,9 @@
Deface::Override.new(:virtual_path => "spree/products/index",
:insert_after => "[data-hook='homepage_products']",
:partial => 'spree/shared/multi_cart.html',
:name => 'multi_cart_home')
Deface::Override.new(:virtual_path => "spree/home/index",
:insert_after => "[data-hook='homepage_products']",
:partial => 'spree/shared/multi_cart.html',
:name => 'multi_cart_products')

View File

@@ -1,6 +0,0 @@
object @cart
attributes :id
node( :orders ) do |p|
partial '/orders/index', object: p.orders
end

View File

@@ -0,0 +1,8 @@
/ %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();" }
{{cart}}
%ul
%li(ng-repeat="order in cart.orders")
{{order.distributor}}

View File

@@ -0,0 +1,6 @@
object @cart
attributes :id
node( :orders ) do |p|
partial '/open_food_web/orders/index', object: p.orders
end

View File

@@ -0,0 +1,3 @@
collection @orders
extends "open_food_web/orders/show"

View File

@@ -1,3 +0,0 @@
collection @orders
extends "orders/show"

View File

@@ -0,0 +1,3 @@
- if OpenFoodWeb::FeatureToggle.enabled? :multi_cart
= render :partial => 'open_food_web/cart/show'

View File

@@ -23,6 +23,10 @@ Openfoodweb::Application.routes.draw do
get "new_landing_page", :controller => 'home', :action => "new_landing_page"
namespace :open_food_web do
resources :cart
end
# Mount Spree's routes
mount Spree::Core::Engine, :at => '/'
end

View File

@@ -4,13 +4,13 @@ module OpenFoodWeb
features[feature]
end
private
def self.features
{eaterprises: true,
local_organics: false,
order_cycles: false,
multi_cart: false,
enterprises_distributor_info_rich_text: false}
end
end

View File

@@ -1,47 +1,47 @@
require 'spec_helper'
require 'spree/api/testing_support/helpers'
describe CartController do
include Spree::Api::TestingSupport::Helpers
render_views
module OpenFoodWeb
describe CartController do
render_views
let(:current_api_user) { stub_model(Spree.user_class, :email => "spree@example.com") }
let!(:product1) { FactoryGirl.create(:product) }
let!(:cart) { Cart.create(user: current_api_user) }
let(:user) { FactoryGirl.create(:user) }
let(:product1) { FactoryGirl.create(:product) }
let(:cart) { Cart.create(user: user) }
before do
stub_authentication!
Spree.user_class.stub :find_by_spree_api_key => current_api_user
end
before do
end
context "as a normal user" do
context "as a normal user" do
context 'with an existing cart' do
context 'with an existing cart' do
it "retrieves an empty cart" do
get :show, {id: cart, :format => :json }
json_response = JSON.parse(response.body)
it "retrieves an empty cart" do
spree_get :show, {id: cart, :format => :json }
json_response["id"].should == cart.id
json_response['orders'].size.should == 0
end
context 'with an order' do
let(:order) { FactoryGirl.create(:order_with_totals_and_distributor) }
before(:each) do
cart.orders << order
cart.save!
json_response['id'].should == cart.id
json_response['orders'].size.should == 0
end
it "retrieves a cart with a single order and line item" do
spree_get :show, {id: cart, :format => :json }
context 'with an order' do
json_response['orders'].size.should == 1
json_response['orders'].first['distributor'].should == order.distributor.name
let(:order) { FactoryGirl.create(:order_with_totals_and_distributor) }
before(:each) do
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
end
end
end
end
end
end
end