mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Add simple angular cart to the main page.
This commit is contained in:
@@ -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 .
|
||||
|
||||
18
app/assets/javascripts/store/controllers/cart.js.coffee
Normal file
18
app/assets/javascripts/store/controllers/cart.js.coffee
Normal 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')
|
||||
])
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
12
app/assets/javascripts/store/factories/cart.js.coffee
Normal file
12
app/assets/javascripts/store/factories/cart.js.coffee
Normal 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)
|
||||
|
||||
|
||||
@@ -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
|
||||
25
app/controllers/open_food_web/cart_controller.rb
Normal file
25
app/controllers/open_food_web/cart_controller.rb
Normal 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
|
||||
9
app/overrides/add_multi_cart_to_home.rb
Normal file
9
app/overrides/add_multi_cart_to_home.rb
Normal 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')
|
||||
@@ -1,6 +0,0 @@
|
||||
object @cart
|
||||
attributes :id
|
||||
|
||||
node( :orders ) do |p|
|
||||
partial '/orders/index', object: p.orders
|
||||
end
|
||||
8
app/views/open_food_web/cart/_show.html.haml
Normal file
8
app/views/open_food_web/cart/_show.html.haml
Normal 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}}
|
||||
6
app/views/open_food_web/cart/show.v1.rabl
Normal file
6
app/views/open_food_web/cart/show.v1.rabl
Normal file
@@ -0,0 +1,6 @@
|
||||
object @cart
|
||||
attributes :id
|
||||
|
||||
node( :orders ) do |p|
|
||||
partial '/open_food_web/orders/index', object: p.orders
|
||||
end
|
||||
3
app/views/open_food_web/orders/index.v1.rabl
Normal file
3
app/views/open_food_web/orders/index.v1.rabl
Normal file
@@ -0,0 +1,3 @@
|
||||
collection @orders
|
||||
|
||||
extends "open_food_web/orders/show"
|
||||
@@ -1,3 +0,0 @@
|
||||
collection @orders
|
||||
|
||||
extends "orders/show"
|
||||
3
app/views/spree/shared/_multi_cart.html.haml
Normal file
3
app/views/spree/shared/_multi_cart.html.haml
Normal file
@@ -0,0 +1,3 @@
|
||||
|
||||
- if OpenFoodWeb::FeatureToggle.enabled? :multi_cart
|
||||
= render :partial => 'open_food_web/cart/show'
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user