Skip to content
This repository has been archived by the owner on May 5, 2018. It is now read-only.

Commit

Permalink
Trying to save shopping cart line items to orders
Browse files Browse the repository at this point in the history
  • Loading branch information
florida committed Nov 20, 2012
1 parent 5c73be8 commit 2da50ed
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
8 changes: 6 additions & 2 deletions app/controllers/store_controller.rb
Expand Up @@ -5,6 +5,7 @@ def index

def add_to_cart
product = Product.find(params[:id])
get_order
@cart = get_cart
@cart.add_line_item(product)

Expand Down Expand Up @@ -40,7 +41,8 @@ def checkout
if @items.empty?
redirect_to root_path("There's nothing in your cart")
else
@order = Order.new
@cart.empty_cart_contents!
@order = @cart.get_order
end
end

Expand All @@ -50,6 +52,8 @@ def get_cart
session[:cart] ||= Cart.new
end


def get_order
session[:order] ||= Order.new
end

end
6 changes: 5 additions & 1 deletion app/models/cart.rb
Expand Up @@ -10,6 +10,7 @@ def initialize
@items = []
@total_price = 0.0
@taxes = 0.0
@order = session[:order]
end


Expand All @@ -21,7 +22,8 @@ def add_line_item(product)
if item
item.quantity += 1
else
item = Lineitem.add_product(product)
item = Lineitem.add_product(product, @order)

@items << item

end
Expand All @@ -33,5 +35,7 @@ def empty_cart_contents!
@items = []
@total_price = 0.0
@taxes = 0.0
@order = Order.new
end

end
4 changes: 2 additions & 2 deletions app/models/lineitem.rb
Expand Up @@ -5,8 +5,8 @@ class Lineitem < ActiveRecord::Base

# # # # next feature, receiving quantity...

def self.add_product(product)
item = self.new
def self.add_product(product, order)
item = order.lineitems.build
item.quantity = 1
item.product = product
#record the price during the sale
Expand Down
2 changes: 1 addition & 1 deletion app/views/store/checkout.html.erb
Expand Up @@ -2,7 +2,7 @@

<%= render :partial => 'items_in_cart', :collection => @items, :as => 'item' %>
<%= form_for [:admin, @order] do |f| %>
<section class="field">
<%= f.label :first_name %>
Expand Down
1 change: 1 addition & 0 deletions config/environment.rb
Expand Up @@ -3,3 +3,4 @@

# Initialize the rails application
Elmorfstore::Application.initialize!
config.action_controller.session_store = :active_record_store

0 comments on commit 2da50ed

Please sign in to comment.