diff --git a/app/assets/stylesheets/carts.css.scss b/app/assets/stylesheets/carts.css.scss index 62647c9..5cae8ec 100644 --- a/app/assets/stylesheets/carts.css.scss +++ b/app/assets/stylesheets/carts.css.scss @@ -1,3 +1,15 @@ // 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_cell { + font-weight: bold; + border-top: 1px solid #595; + } +} + diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb index d39ab43..67bf4d5 100644 --- a/app/controllers/carts_controller.rb +++ b/app/controllers/carts_controller.rb @@ -13,11 +13,16 @@ def index # GET /carts/1 # GET /carts/1.json def show - @cart = Cart.find(params[:id]) - - respond_to do |format| - format.html # show.html.erb - format.json { render json: @cart } + begin + @cart = Cart.find(params[:id]) + rescue ActiveRecord::RecordNotFound + logger.error "Attempt to access invalid cart #{params[:id]}" + redirect_to store_url, notice: 'Invalid cart' + else + respond_to do |format| + format.html # show.html.erb + format.json { render json: @cart } + end end end @@ -72,12 +77,15 @@ def update # DELETE /carts/1 # DELETE /carts/1.json def destroy - @cart = Cart.find(params[:id]) + @cart = current_cart @cart.destroy + session[:cart_id] = nil respond_to do |format| - format.html { redirect_to carts_url } + format.html { redirect_to store_url, + notice: 'Your cart is currently empty' } format.json { head :no_content } end end end + diff --git a/app/controllers/line_items_controller.rb b/app/controllers/line_items_controller.rb index 6418a88..9a9db36 100644 --- a/app/controllers/line_items_controller.rb +++ b/app/controllers/line_items_controller.rb @@ -42,14 +42,11 @@ def edit def create @cart = current_cart product = Product.find(params[:product_id]) - #@line_item = @cart.line_items.build(product_id: product) @line_item = @cart.add_product(product.id) - #@line_item = @cart.add_product(product_id: product.id) 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 json: @line_item, status: :created, location: @line_item } else diff --git a/app/models/cart.rb b/app/models/cart.rb index b0f7fbe..cd0eb8f 100644 --- a/app/models/cart.rb +++ b/app/models/cart.rb @@ -12,5 +12,9 @@ def add_product(product_id) end current_item end + + def total_price + 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 c12db96..6d50006 100644 --- a/app/models/line_item.rb +++ b/app/models/line_item.rb @@ -5,5 +5,9 @@ class LineItem < ActiveRecord::Base 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 37c4876..3242074 100644 --- a/app/views/carts/show.html.erb +++ b/app/views/carts/show.html.erb @@ -2,10 +2,21 @@

<%= notice %>

<% end %> -

Your Pragmatic Cart

-