浏览代码

Iteration E3.

master
父节点
当前提交
8c97fb8f7c
共有 8 个文件被更改,包括 45 次插入10 次删除
  1. +10
    -0
      app/assets/stylesheets/carts.scss
  2. +3
    -2
      app/controllers/carts_controller.rb
  3. +1
    -1
      app/controllers/line_items_controller.rb
  4. +5
    -0
      app/models/cart.rb
  5. +4
    -0
      app/models/line_item.rb
  6. +16
    -4
      app/views/carts/show.html.erb
  7. +4
    -1
      test/controllers/carts_controller_test.rb
  8. +2
    -2
      test/controllers/line_items_controller_test.rb

+ 10
- 0
app/assets/stylesheets/carts.scss 查看文件

@@ -1,3 +1,13 @@
// Place all the styles related to the Carts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.carts {
.item_price, .total_line {
text-align: right;
}

.total_line .total_cll {
font-weight: bold;
border-top: 1px solid #595
}
}

+ 3
- 2
app/controllers/carts_controller.rb 查看文件

@@ -56,9 +56,10 @@ class CartsController < ApplicationController
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart.destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to carts_url, notice: 'Cart was successfully destroyed.' }
format.html { redirect_to store_index_url, notice: 'Your cart is currently empty.' }
format.json { head :no_content }
end
end


+ 1
- 1
app/controllers/line_items_controller.rb 查看文件

@@ -33,7 +33,7 @@ class LineItemsController < ApplicationController

respond_to do |format|
if @line_item.save
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
format.html { redirect_to @line_item.cart }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }


+ 5
- 0
app/models/cart.rb 查看文件

@@ -10,4 +10,9 @@ class Cart < ApplicationRecord
end
current_item
end

def total_price
# QUESTION: How and why? :-D
line_items.to_a.sum { |item| item.total_price }
end
end

+ 4
- 0
app/models/line_item.rb 查看文件

@@ -1,4 +1,8 @@
class LineItem < ApplicationRecord
belongs_to :product
belongs_to :cart

def total_price
product.price * quantity
end
end

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

@@ -1,9 +1,21 @@
<p id="notice"><%= notice %></p>

<h2>Your Pragmatic Cart</h2>
<h2>Your Cart</h2>

<ul>
<table>
<% @cart.line_items.each do |item| %>
<li><%= item.quantity %> &times; <%= item.product.title %></li>
<tr>
<td><%= item.quantity %> &times;</td>
<td><%= item.product.title %></td>
<td class="item_price"><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>
</ul>

<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>

</table>

<%= button_to 'Empty Cart', @cart, method: :delete, data: { confirm: 'Are you sure?'} %>

+ 4
- 1
test/controllers/carts_controller_test.rb 查看文件

@@ -39,10 +39,13 @@ class CartsControllerTest < ActionDispatch::IntegrationTest
end

test "should destroy cart" do
post line_items_url, params: { product_id: products(:ruby).id }
@cart = Cart.find(session[:cart_id])

assert_difference('Cart.count', -1) do
delete cart_url(@cart)
end

assert_redirected_to carts_url
assert_redirected_to store_index_url
end
end

+ 2
- 2
test/controllers/line_items_controller_test.rb 查看文件

@@ -22,8 +22,8 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest

follow_redirect!

assert_select 'h2', 'Your Pragmatic Cart'
assert_select 'li', "1 \u00D7 Programming Ruby 1.9"
assert_select 'h2', 'Your Cart'
assert_select 'td', 'Programming Ruby 1.9'
end

test "should show line_item" do


正在加载...
取消
保存