mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Simplest version of a cart that supports multiple orders.
This commit is contained in:
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
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"
|
||||
|
||||
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