diff --git a/app/controllers/line_items_controller.rb b/app/controllers/line_items_controller.rb
index b41c2f1..0cfe8aa 100644
--- a/app/controllers/line_items_controller.rb
+++ b/app/controllers/line_items_controller.rb
@@ -29,7 +29,7 @@ class LineItemsController < ApplicationController
# POST /line_items.json
def create
product = Product.find(params[:product_id])
- @line_item = @cart.line_items.build(product: product)
+ @line_item = @cart.add_product(product)
respond_to do |format|
if @line_item.save
diff --git a/app/models/cart.rb b/app/models/cart.rb
index 6ecba63..6d0ffc9 100644
--- a/app/models/cart.rb
+++ b/app/models/cart.rb
@@ -1,3 +1,13 @@
class Cart < ApplicationRecord
has_many :line_items, dependent: :destroy
+
+ def add_product(product)
+ 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
diff --git a/app/views/carts/show.html.erb b/app/views/carts/show.html.erb
index a3d8b09..935301b 100644
--- a/app/views/carts/show.html.erb
+++ b/app/views/carts/show.html.erb
@@ -4,6 +4,6 @@
<% @cart.line_items.each do |item| %>
- - <%= item.product.title %>
+ - <%= item.quantity %> × <%= item.product.title %>
<% end %>
diff --git a/db/migrate/20161116085812_add_quantity_to_line_items.rb b/db/migrate/20161116085812_add_quantity_to_line_items.rb
new file mode 100644
index 0000000..f112a29
--- /dev/null
+++ b/db/migrate/20161116085812_add_quantity_to_line_items.rb
@@ -0,0 +1,5 @@
+class AddQuantityToLineItems < ActiveRecord::Migration[5.0]
+ def change
+ add_column :line_items, :quantity, :integer, default: 1
+ end
+end
diff --git a/db/migrate/20161116091835_combine_items_in_cart.rb b/db/migrate/20161116091835_combine_items_in_cart.rb
new file mode 100644
index 0000000..1e9a329
--- /dev/null
+++ b/db/migrate/20161116091835_combine_items_in_cart.rb
@@ -0,0 +1,30 @@
+class CombineItemsInCart < ActiveRecord::Migration[5.0]
+ def up
+ Cart.all.each do |cart|
+ sums = cart.line_items.group(:product_id).sum(:quantity)
+
+ sums.each do |product_id, quantity|
+ if quantity > 1
+ cart.line_items.where(product_id: product_id).delete_all
+
+ item = cart.line_items.build(product_id: product_id)
+ item.quantity = quantity
+ item.save!
+ end
+ end
+ end
+ end
+
+ def down
+ LineItem.where('quantity > 1').each do |li|
+ li.quantity.times do
+ # item = Cart.find_by(:cart_id li.cart_id).line_items.build(product_id: li.product_id)
+ # item.save!
+ LineItem.create(cart_id: li.cart_id,
+ product_id: li.product_id,
+ quantity: 1)
+ end
+ li.destroy
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index ee07383..4cffb0a 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20161115211138) do
+ActiveRecord::Schema.define(version: 20161116091835) do
create_table "carts", force: :cascade do |t|
t.datetime "created_at", null: false
@@ -20,8 +20,9 @@ ActiveRecord::Schema.define(version: 20161115211138) do
create_table "line_items", force: :cascade 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
t.index ["cart_id"], name: "index_line_items_on_cart_id"
t.index ["product_id"], name: "index_line_items_on_product_id"
end
diff --git a/test/controllers/line_items_controller_test.rb b/test/controllers/line_items_controller_test.rb
index 2999263..5103e92 100644
--- a/test/controllers/line_items_controller_test.rb
+++ b/test/controllers/line_items_controller_test.rb
@@ -23,7 +23,7 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest
follow_redirect!
assert_select 'h2', 'Your Pragmatic Cart'
- assert_select 'li', 'Programming Ruby 1.9'
+ assert_select 'li', "1 \u00D7 Programming Ruby 1.9"
end
test "should show line_item" do