mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-03 22:06:07 +00:00
Merge branch 'master' of github.com:eaterprises/openfoodweb
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)
|
||||
|
||||
|
||||
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
|
||||
12
app/models/cart.rb
Normal file
12
app/models/cart.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
class Cart < ActiveRecord::Base
|
||||
has_many :orders, :class_name => 'Spree::Order'
|
||||
belongs_to :user, :class_name => Spree.user_class
|
||||
|
||||
def add_variant variant, quantity
|
||||
if orders.empty?
|
||||
order = Spree::Order.create
|
||||
order.add_variant(variant, quantity)
|
||||
orders << order
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,6 +3,7 @@ require 'open_food_web/distribution_change_validator'
|
||||
Spree::Order.class_eval do
|
||||
belongs_to :order_cycle
|
||||
belongs_to :distributor, :class_name => 'Enterprise'
|
||||
belongs_to :cart
|
||||
|
||||
before_validation :shipping_address_from_distributor
|
||||
validate :products_available_from_new_distribution, :if => lambda { distributor_id_changed? || order_cycle_id_changed? }
|
||||
|
||||
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')
|
||||
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"
|
||||
4
app/views/open_food_web/orders/show.v1.rabl
Normal file
4
app/views/open_food_web/orders/show.v1.rabl
Normal file
@@ -0,0 +1,4 @@
|
||||
object @order
|
||||
attributes :id
|
||||
|
||||
node( :distributor ) { |p| p.distributor.blank? ? "" : p.distributor.name }
|
||||
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
|
||||
|
||||
9
db/migrate/20130807230834_add_cart.rb
Normal file
9
db/migrate/20130807230834_add_cart.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
class AddCart < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :carts do |t|
|
||||
t.integer :user_id
|
||||
end
|
||||
|
||||
add_column :spree_orders, :cart_id, :integer
|
||||
end
|
||||
end
|
||||
@@ -11,7 +11,11 @@
|
||||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20130807002915) do
|
||||
ActiveRecord::Schema.define(:version => 20130807230834) do
|
||||
|
||||
create_table "carts", :force => true do |t|
|
||||
t.integer "user_id"
|
||||
end
|
||||
|
||||
create_table "cms_blocks", :force => true do |t|
|
||||
t.integer "page_id", :null => false
|
||||
@@ -447,6 +451,7 @@ ActiveRecord::Schema.define(:version => 20130807002915) do
|
||||
t.string "currency"
|
||||
t.string "last_ip_address"
|
||||
t.integer "order_cycle_id"
|
||||
t.integer "cart_id"
|
||||
end
|
||||
|
||||
add_index "spree_orders", ["number"], :name => "index_orders_on_number"
|
||||
|
||||
@@ -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
|
||||
|
||||
47
spec/controllers/cart_controller_spec.rb
Normal file
47
spec/controllers/cart_controller_spec.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'spec_helper'
|
||||
require 'spree/api/testing_support/helpers'
|
||||
|
||||
module OpenFoodWeb
|
||||
describe CartController do
|
||||
render_views
|
||||
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let(:product1) { FactoryGirl.create(:product) }
|
||||
let(:cart) { Cart.create(user: user) }
|
||||
|
||||
before do
|
||||
end
|
||||
|
||||
context "as a normal user" 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)
|
||||
|
||||
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!
|
||||
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
|
||||
32
spec/models/cart_spec.rb
Normal file
32
spec/models/cart_spec.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Cart do
|
||||
|
||||
describe "associations" do
|
||||
it { should have_many(:orders) }
|
||||
end
|
||||
|
||||
describe 'adding a product' do
|
||||
|
||||
let(:product) { create(:product) }
|
||||
|
||||
it 'when there are no orders in the cart, create one when a product is added' do
|
||||
subject.add_variant product.master, 3
|
||||
|
||||
subject.orders.size.should == 1
|
||||
subject.orders.first.line_items.first.product.should == product
|
||||
end
|
||||
|
||||
it 'should create an order when a product from a new distributor is added'
|
||||
|
||||
it 'should create an order when a product from a new order cycle is added'
|
||||
|
||||
it 'should create line items in an order for added product, when in the same distributor'
|
||||
|
||||
it 'should create line items in an order for added product, when in the same distributor and order cycle'
|
||||
|
||||
it 'should not create line items in an order, if the product is in a different order cycle to the order'
|
||||
|
||||
it 'should not create line items in an order, if the product is in a different distributor to the order'
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user