浏览代码

Iteration E1.

master
父节点
当前提交
b564db45ca
共有 7 个文件被更改,包括 52 次插入6 次删除
  1. +1
    -1
      app/controllers/line_items_controller.rb
  2. +10
    -0
      app/models/cart.rb
  3. +1
    -1
      app/views/carts/show.html.erb
  4. +5
    -0
      db/migrate/20161116085812_add_quantity_to_line_items.rb
  5. +30
    -0
      db/migrate/20161116091835_combine_items_in_cart.rb
  6. +4
    -3
      db/schema.rb
  7. +1
    -1
      test/controllers/line_items_controller_test.rb

+ 1
- 1
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


+ 10
- 0
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

+ 1
- 1
app/views/carts/show.html.erb 查看文件

@@ -4,6 +4,6 @@

<ul>
<% @cart.line_items.each do |item| %>
<li><%= item.product.title %></li>
<li><%= item.quantity %> &times; <%= item.product.title %></li>
<% end %>
</ul>

+ 5
- 0
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

+ 30
- 0
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

+ 4
- 3
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


+ 1
- 1
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


正在加载...
取消
保存