diff --git a/app/assets/stylesheets/carts.scss b/app/assets/stylesheets/carts.scss index 2fe1b9e..fe0ac50 100644 --- a/app/assets/stylesheets/carts.scss +++ b/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 + } +} diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb index e028663..c4227ab 100644 --- a/app/controllers/carts_controller.rb +++ b/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 diff --git a/app/controllers/line_items_controller.rb b/app/controllers/line_items_controller.rb index 3420581..8bea84d 100644 --- a/app/controllers/line_items_controller.rb +++ b/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 } diff --git a/app/models/cart.rb b/app/models/cart.rb index 6d0ffc9..6700a67 100644 --- a/app/models/cart.rb +++ b/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 diff --git a/app/models/line_item.rb b/app/models/line_item.rb index 4642391..2cdd111 100644 --- a/app/models/line_item.rb +++ b/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 diff --git a/app/views/carts/show.html.erb b/app/views/carts/show.html.erb index 935301b..be99d19 100644 --- a/app/views/carts/show.html.erb +++ b/app/views/carts/show.html.erb @@ -1,9 +1,21 @@
<%= notice %>
-| <%= item.quantity %> × | +<%= item.product.title %> | +<%= number_to_currency(item.total_price) %> | +
| Total | +<%= number_to_currency(@cart.total_price) %> | +|