Skip to content

Commit

Permalink
Smarter Cart--with quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
jezreljane committed Aug 22, 2012
1 parent 5c8cd75 commit 0772dab
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
5 changes: 3 additions & 2 deletions app/controllers/line_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ def edit
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(product_id: product)
@line_item.product = product
#@line_item = @cart.line_items.build(product_id: product)
@line_item = @cart.add_product(product.id)
#@line_item = @cart.add_product(product_id: product.id)

respond_to do |format|
if @line_item.save
Expand Down
10 changes: 10 additions & 0 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,15 @@ class Cart < ActiveRecord::Base
# attr_accessible :title, :body

has_many :line_items, dependent: :destroy

def add_product(product_id)
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
end

2 changes: 1 addition & 1 deletion app/models/line_item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class LineItem < ActiveRecord::Base
#attr_accessible :cart_id, :product_id

attr_accessible :product_id, :id, :cart_id, :created_at, :updated_at
attr_accessible :id, :cart_id, :product_id, :created_at, :updated_at, :product

belongs_to :product
belongs_to :cart
Expand Down
2 changes: 1 addition & 1 deletion app/views/carts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h2>Your Pragmatic Cart</h2>
<ul>
<% @cart.line_items.each do |item| %>
<li><%= item.product.title %></li>
<li><%= item.quantity %> &times; <%= item.product.title %></li>
<% end %>
</ul>

6 changes: 6 additions & 0 deletions db/migrate/20120822003344_add_quantity_to_line_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddQuantityToLineItems < ActiveRecord::Migration
def change
add_column :line_items, :quantity, :integer, default: 1
end
end

37 changes: 37 additions & 0 deletions db/migrate/20120822005458_combine_items_in_cart.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class CombineItemsInCart < ActiveRecord::Migration

def up
# replace multiple items for a single product in a cart with a single item
Cart.all.each do |cart|
# count the number of each product in the cart
sums = cart.line_items.group(:product_id).sum(:quantity)

sums.each do |product_id, quantity|
if quantity > 1
# remove individual items
cart.line_items.where(product_id: product_id).delete_all

# replace with a single item
item = cart.line_items.build(product_id: product_id)
item.quantity = quantity
item.save!
end
end
end
end

def down
# split items with quantity>1 into multiple items
LineItem.where("quantity>1").each do |line_item|
# add individual items
line_item.quantity.times do
LineItem.create cart_id: line_item.cart_id,
product_id: line_item.product_id
end

# remove original item
line_item.destroy
end
end
end

7 changes: 4 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120822001052) do
ActiveRecord::Schema.define(:version => 20120822005458) do

create_table "carts", :force => true do |t|
t.datetime "created_at", :null => false
Expand All @@ -21,8 +21,9 @@
create_table "line_items", :force => true do |t|
t.integer "product_id"
t.integer "cart_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "quantity", :default => 1
end

create_table "products", :force => true do |t|
Expand Down

0 comments on commit 0772dab

Please sign in to comment.