| @@ -1,3 +1,13 @@ | |||||
| // Place all the styles related to the Carts controller here. | // Place all the styles related to the Carts controller here. | ||||
| // They will automatically be included in application.css. | // They will automatically be included in application.css. | ||||
| // You can use Sass (SCSS) here: http://sass-lang.com/ | // 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 | |||||
| } | |||||
| } | |||||
| @@ -56,9 +56,10 @@ class CartsController < ApplicationController | |||||
| # DELETE /carts/1 | # DELETE /carts/1 | ||||
| # DELETE /carts/1.json | # DELETE /carts/1.json | ||||
| def destroy | def destroy | ||||
| @cart.destroy | |||||
| @cart.destroy if @cart.id == session[:cart_id] | |||||
| session[:cart_id] = nil | |||||
| respond_to do |format| | 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 } | format.json { head :no_content } | ||||
| end | end | ||||
| end | end | ||||
| @@ -33,7 +33,7 @@ class LineItemsController < ApplicationController | |||||
| respond_to do |format| | respond_to do |format| | ||||
| if @line_item.save | 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 } | format.json { render :show, status: :created, location: @line_item } | ||||
| else | else | ||||
| format.html { render :new } | format.html { render :new } | ||||
| @@ -10,4 +10,9 @@ class Cart < ApplicationRecord | |||||
| end | end | ||||
| current_item | current_item | ||||
| end | end | ||||
| def total_price | |||||
| # QUESTION: How and why? :-D | |||||
| line_items.to_a.sum { |item| item.total_price } | |||||
| end | |||||
| end | end | ||||
| @@ -1,4 +1,8 @@ | |||||
| class LineItem < ApplicationRecord | class LineItem < ApplicationRecord | ||||
| belongs_to :product | belongs_to :product | ||||
| belongs_to :cart | belongs_to :cart | ||||
| def total_price | |||||
| product.price * quantity | |||||
| end | |||||
| end | end | ||||
| @@ -1,9 +1,21 @@ | |||||
| <p id="notice"><%= notice %></p> | <p id="notice"><%= notice %></p> | ||||
| <h2>Your Pragmatic Cart</h2> | |||||
| <h2>Your Cart</h2> | |||||
| <ul> | |||||
| <table> | |||||
| <% @cart.line_items.each do |item| %> | <% @cart.line_items.each do |item| %> | ||||
| <li><%= item.quantity %> × <%= item.product.title %></li> | |||||
| <tr> | |||||
| <td><%= item.quantity %> ×</td> | |||||
| <td><%= item.product.title %></td> | |||||
| <td class="item_price"><%= number_to_currency(item.total_price) %></td> | |||||
| </tr> | |||||
| <% end %> | <% 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?'} %> | |||||
| @@ -39,10 +39,13 @@ class CartsControllerTest < ActionDispatch::IntegrationTest | |||||
| end | end | ||||
| test "should destroy cart" do | 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 | assert_difference('Cart.count', -1) do | ||||
| delete cart_url(@cart) | delete cart_url(@cart) | ||||
| end | end | ||||
| assert_redirected_to carts_url | |||||
| assert_redirected_to store_index_url | |||||
| end | end | ||||
| end | end | ||||
| @@ -22,8 +22,8 @@ class LineItemsControllerTest < ActionDispatch::IntegrationTest | |||||
| follow_redirect! | 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 | end | ||||
| test "should show line_item" do | test "should show line_item" do | ||||