Skip to content

Commit

Permalink
Task E
Browse files Browse the repository at this point in the history
  • Loading branch information
sashapro committed Mar 28, 2012
1 parent 4152e3e commit 5ac3e71
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 128 deletions.
209 changes: 100 additions & 109 deletions .idea/workspace.xml

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions app/assets/stylesheets/carts.css.scss
@@ -1,3 +1,19 @@
// 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/
/* START_HIGHLIGHT */
.carts {
.cart_title {
font: 120% bold;
}

.item_price, .total_line {
text-align: right;
}

.total_line .total_cell {
font-weight: bold;
border-top: 1px solid #595;
}
}
/* END_HIGHLIGHT */
21 changes: 14 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,11 +77,13 @@ 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
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/line_items_controller.rb
Expand Up @@ -42,12 +42,12 @@ def edit
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build(product: product)
@line_item = @cart.add_product(product.id)

respond_to do |format|
if @line_item.save
session[:counter] = nill
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
session[:counter] = nil
format.html { redirect_to @line_item.cart }
format.json { render json: @line_item, status: :created, location: @line_item }
else
format.html { render action: "new" }
Expand Down
16 changes: 15 additions & 1 deletion app/models/cart.rb
@@ -1,3 +1,17 @@
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
end

def add_product(product_id)
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end

end
5 changes: 5 additions & 0 deletions app/models/line_item.rb
@@ -1,4 +1,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
@@ -1,9 +1,20 @@
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h2>Your Pragmatic Cart</h2>
<ul>
<div class="cart_title">Your Cart</div>
<table>
<% @cart.line_items.each do |item| %>
<li><%= 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,
confirm: 'Are you sure?' %>

5 changes: 5 additions & 0 deletions db/migrate/20120328130343_add_quantity_to_line_items.rb
@@ -0,0 +1,5 @@
class AddQuantityToLineItems < ActiveRecord::Migration
def change
add_column :line_items, :quantity, :integer, default: 1
end
end
30 changes: 30 additions & 0 deletions db/migrate/20120328135252_combine_items_in_cart.rb
@@ -0,0 +1,30 @@
class CombineItemsInCart < ActiveRecord::Migration
def up
# replace multiple items for a single product in a cart with a single item
Cart.all.each do |cart|
# count the number of each product in the cart
sums = cart.line_items.group(:product_id).sum(:quantity)
sums.each do |product_id, quantity|
if quantity > 1
# remove individual items
cart.line_items.where(product_id: product_id).delete_all
# replace with a single item
cart.line_items.create(product_id: product_id, quantity: quantity)
end
end
end
end

def down
# split items with quantity>1 into multiple items
LineItem.where("quantity>1").each do |line_item|
# add individual items
line_item.quantity.times do
LineItem.create cart_id: line_item.cart_id,
product_id: line_item.product_id, quantity: 1
end
# remove original item
line_item.destroy
end
end
end
7 changes: 4 additions & 3 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120326193046) do
ActiveRecord::Schema.define(:version => 20120328135252) do

create_table "carts", :force => true do |t|
t.datetime "created_at", :null => false
Expand All @@ -21,8 +21,9 @@
create_table "line_items", :force => true do |t|
t.integer "product_id"
t.integer "cart_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "quantity", :default => 1
end

create_table "products", :force => true do |t|
Expand Down
3 changes: 2 additions & 1 deletion test/functional/carts_controller_test.rb
Expand Up @@ -41,9 +41,10 @@ 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 5ac3e71

Please sign in to comment.