| @@ -29,7 +29,7 @@ class LineItemsController < ApplicationController | |||||
| # POST /line_items.json | # POST /line_items.json | ||||
| def create | def create | ||||
| product = Product.find(params[:product_id]) | product = Product.find(params[:product_id]) | ||||
| @line_item = @cart.line_items.build(product: product) | |||||
| @line_item = @cart.add_product(product) | |||||
| respond_to do |format| | respond_to do |format| | ||||
| if @line_item.save | if @line_item.save | ||||
| @@ -1,3 +1,13 @@ | |||||
| class Cart < ApplicationRecord | class Cart < ApplicationRecord | ||||
| has_many :line_items, dependent: :destroy | 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 | end | ||||
| @@ -4,6 +4,6 @@ | |||||
| <ul> | <ul> | ||||
| <% @cart.line_items.each do |item| %> | <% @cart.line_items.each do |item| %> | ||||
| <li><%= item.product.title %></li> | |||||
| <li><%= item.quantity %> × <%= item.product.title %></li> | |||||
| <% end %> | <% end %> | ||||
| </ul> | </ul> | ||||
| @@ -0,0 +1,5 @@ | |||||
| class AddQuantityToLineItems < ActiveRecord::Migration[5.0] | |||||
| def change | |||||
| add_column :line_items, :quantity, :integer, default: 1 | |||||
| end | |||||
| end | |||||
| @@ -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 | |||||
| @@ -10,7 +10,7 @@ | |||||
| # | # | ||||
| # It's strongly recommended that you check this file into your version control system. | # 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| | create_table "carts", force: :cascade do |t| | ||||
| t.datetime "created_at", null: false | t.datetime "created_at", null: false | ||||
| @@ -20,8 +20,9 @@ ActiveRecord::Schema.define(version: 20161115211138) do | |||||
| create_table "line_items", force: :cascade do |t| | create_table "line_items", force: :cascade do |t| | ||||
| t.integer "product_id" | t.integer "product_id" | ||||
| t.integer "cart_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 ["cart_id"], name: "index_line_items_on_cart_id" | ||||
| t.index ["product_id"], name: "index_line_items_on_product_id" | t.index ["product_id"], name: "index_line_items_on_product_id" | ||||
| end | end | ||||
| @@ -23,7 +23,7 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest | |||||
| follow_redirect! | follow_redirect! | ||||
| assert_select 'h2', 'Your Pragmatic Cart' | assert_select 'h2', 'Your Pragmatic Cart' | ||||
| assert_select 'li', 'Programming Ruby 1.9' | |||||
| assert_select 'li', "1 \u00D7 Programming Ruby 1.9" | |||||
| end | end | ||||
| test "should show line_item" do | test "should show line_item" do | ||||