Permalink
Browse files

Was inheriting from active record when I shouldn't have

  • Loading branch information...
florida committed Nov 16, 2012
1 parent 1e1a225 commit 590f302ad32e1745477478017660aea3dfaa3d67
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
end
@@ -7,7 +7,8 @@ def add_to_cart
product = Product.find(params[:id])
@cart = get_cart
@cart.add_line_item(product)
- #display the cart
+
+ redirect_to store_path
end
def show_cart
View
@@ -1,3 +1,29 @@
-class Cart < ActiveRecord::Base
- # attr_accessible :title, :body
+class Cart
+ #Model not persisted in the database
+ #Cart will have items, cart will have a total price
+ # and tax
+
+ #feature to add... adding line items with quantity
+
+ attr_reader :items, :total_price, :taxes
+ def initialize
+ @items = []
+ @total_price = 0.0
+ @taxes = 0.0
+ end
+
+
+
+ def add_line_item(product)
+ #finds if item is already in the cart
+ item = @items.find {|i| i.product_id == product.id }
+
+ if item
+ item.quantiy += 1
+ else
+ item = LineItem.add_product(product)
+ @item << item
+ end
+ @total_price += product.price
+ end
end
View
@@ -2,4 +2,14 @@ class Lineitem < ActiveRecord::Base
attr_accessible :order_id, :product_id, :quantity, :unit_price
belongs_to :product
belongs_to :order
+
+ # # # # next feature, receiving quantity...
+
+ def self.add_product(product)
+ item = self.new
+ item.quantity = 1
+ item.product = product
+ #record the price during the sale
+ item.unit_price = product.price
+ end
end
@@ -7,5 +7,7 @@
<span class="label">Price</span>
<span class="value"><%= product.price %></span>
</section>
- <footer><%= link_to 'Buy Now', :action => 'add_to_cart', :id => product %></footer>
+ <footer>
+ <%= link_to 'Buy Now', add_to_cart_path(product) %>
+ </footer>
</section>
@@ -0,0 +1,5 @@
+<h1>Display Cart Check</h1>
+
+<p>
+ Cart contains <%= @items.size %> items.
+</p>
View
@@ -1,6 +1,7 @@
Elmorfstore::Application.routes.draw do
match 'store' => 'store#index', :as => 'store', :via => :get
-
+ match 'shoppingcart' => 'store#show_cart', :as => 'shoppingcart', :via => :get
+ match 'add_to_cart/:id' => 'store#add_to_cart', :as => 'add_to_cart'
# get "store/index"
root :to => 'store#index'

0 comments on commit 590f302

Please sign in to comment.