From 7fe1aab9038eeb75433533d75586c5b16806936f Mon Sep 17 00:00:00 2001 From: Andrew Spinks Date: Thu, 8 Aug 2013 11:17:26 +1000 Subject: [PATCH] Simplest version of a cart that supports multiple orders. --- app/models/cart.rb | 12 ++++++++++ app/models/spree/order_decorator.rb | 1 + db/migrate/20130807230834_add_cart.rb | 9 ++++++++ db/schema.rb | 7 +++++- spec/models/cart_spec.rb | 32 +++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 app/models/cart.rb create mode 100644 db/migrate/20130807230834_add_cart.rb create mode 100644 spec/models/cart_spec.rb diff --git a/app/models/cart.rb b/app/models/cart.rb new file mode 100644 index 0000000000..32a0ad33da --- /dev/null +++ b/app/models/cart.rb @@ -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 diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 458b3eb7df..a5f5f87c0d 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -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? } diff --git a/db/migrate/20130807230834_add_cart.rb b/db/migrate/20130807230834_add_cart.rb new file mode 100644 index 0000000000..38d94a7c8e --- /dev/null +++ b/db/migrate/20130807230834_add_cart.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index c1b850281c..ac8c7f6946 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/models/cart_spec.rb b/spec/models/cart_spec.rb new file mode 100644 index 0000000000..1c8997f15b --- /dev/null +++ b/spec/models/cart_spec.rb @@ -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