Skip to content

Commit

Permalink
Smarter Cart--with total amount displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
jezreljane committed Aug 22, 2012
1 parent 0772dab commit 18af98f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 16 deletions.
12 changes: 12 additions & 0 deletions 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;
}
}

22 changes: 15 additions & 7 deletions app/controllers/carts_controller.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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

5 changes: 1 addition & 4 deletions app/controllers/line_items_controller.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/cart.rb
Expand Up @@ -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

4 changes: 4 additions & 0 deletions app/models/line_item.rb
Expand Up @@ -5,5 +5,9 @@ class LineItem < ActiveRecord::Base

belongs_to :product
belongs_to :cart

def total_price
product.price * quantity
end
end

19 changes: 15 additions & 4 deletions app/views/carts/show.html.erb
Expand Up @@ -2,10 +2,21 @@
<p id="notice"><%= notice %></p>
<% end %>

<h2>Your Pragmatic Cart</h2>
<ul>
<h2>Your Cart</h2>
<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 changes: 3 additions & 1 deletion test/functional/carts_controller_test.rb
Expand Up @@ -41,9 +41,11 @@ class CartsControllerTest < ActionController::TestCase

test "should destroy cart" do
assert_difference('Cart.count', -1) do
session[:cart_id] = @cart.id
delete :destroy, id: @cart
end

assert_redirected_to carts_path
assert_redirected_to store_path
end
end

0 comments on commit 18af98f

Please sign in to comment.