Permalink
Browse files

Finished ch 9.

  • Loading branch information...
1 parent 706ec4e commit 5c659db85163139b494753381ce6deb5efb1b725 Codemis committed May 24, 2012
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -1,3 +1,13 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ private
+
+ def current_cart
+ Cart.find(session[:cart_id])
+ rescue ActiveRecord::RecordNotFound
+ cart = Cart.create
+ session[:cart_id] = cart.id
+ cart
+ end
end
@@ -0,0 +1,83 @@
+class CartsController < ApplicationController
+ # GET /carts
+ # GET /carts.json
+ def index
+ @carts = Cart.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @carts }
+ end
+ end
+
+ # 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 }
+ end
+ end
+
+ # GET /carts/new
+ # GET /carts/new.json
+ def new
+ @cart = Cart.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @cart }
+ end
+ end
+
+ # GET /carts/1/edit
+ def edit
+ @cart = Cart.find(params[:id])
+ end
+
+ # POST /carts
+ # POST /carts.json
+ def create
+ @cart = Cart.new(params[:cart])
+
+ respond_to do |format|
+ if @cart.save
+ format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
+ format.json { render json: @cart, status: :created, location: @cart }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @cart.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /carts/1
+ # PUT /carts/1.json
+ def update
+ @cart = Cart.find(params[:id])
+
+ respond_to do |format|
+ if @cart.update_attributes(params[:cart])
+ format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @cart.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /carts/1
+ # DELETE /carts/1.json
+ def destroy
+ @cart = Cart.find(params[:id])
+ @cart.destroy
+
+ respond_to do |format|
+ format.html { redirect_to carts_url }
+ format.json { head :no_content }
+ end
+ end
+end
@@ -0,0 +1,85 @@
+class LineItemsController < ApplicationController
+ # GET /line_items
+ # GET /line_items.json
+ def index
+ @line_items = LineItem.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @line_items }
+ end
+ end
+
+ # GET /line_items/1
+ # GET /line_items/1.json
+ def show
+ @line_item = LineItem.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @line_item }
+ end
+ end
+
+ # GET /line_items/new
+ # GET /line_items/new.json
+ def new
+ @line_item = LineItem.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @line_item }
+ end
+ end
+
+ # GET /line_items/1/edit
+ def edit
+ @line_item = LineItem.find(params[:id])
+ end
+
+ # POST /line_items
+ # POST /line_items.json
+ def create
+ @cart = current_cart
+ product = Product.find(params[:product_id])
+ @line_item = @cart.line_items.build(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.json { render json: @line_item, status: :created, location: @line_item }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @line_item.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /line_items/1
+ # PUT /line_items/1.json
+ def update
+ @line_item = LineItem.find(params[:id])
+
+ respond_to do |format|
+ if @line_item.update_attributes(params[:line_item])
+ format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @line_item.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /line_items/1
+ # DELETE /line_items/1.json
+ def destroy
+ @line_item = LineItem.find(params[:id])
+ @line_item.destroy
+
+ respond_to do |format|
+ format.html { redirect_to line_items_url }
+ format.json { head :no_content }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module CartsHelper
+end
@@ -0,0 +1,2 @@
+module LineItemsHelper
+end
View
@@ -0,0 +1,4 @@
+class Cart < ActiveRecord::Base
+ # attr_accessible :title, :body
+ has_many :line_items, dependent: :destroy
+end
View
@@ -0,0 +1,5 @@
+class LineItem < ActiveRecord::Base
+ attr_accessible :cart_id, :product_id
+ belongs_to :product
+ belongs_to :cart
+end
View
@@ -11,4 +11,17 @@ class Product < ActiveRecord::Base
# with: %r{\d*\.\d\d\z},
# message: 'must be a valid dollar amount.'
# }
+ has_many :line_items
+ before_destroy :ensure_not_referenced_by_any_line_item
+
+ private
+ # ensure that there are no line items referencing this product
+ def ensure_not_referenced_by_any_line_item
+ if line_items.empty?
+ return true
+ else
+ errors.add(:base, 'Line Items present')
+ return false
+ end
+ end
end
@@ -0,0 +1,17 @@
+<%= form_for(@cart) do |f| %>
+ <% if @cart.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@cart.errors.count, "error") %> prohibited this cart from being saved:</h2>
+
+ <ul>
+ <% @cart.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing cart</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @cart %> |
+<%= link_to 'Back', carts_path %>
@@ -0,0 +1,21 @@
+<h1>Listing carts</h1>
+
+<table>
+ <tr>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @carts.each do |cart| %>
+ <tr>
+ <td><%= link_to 'Show', cart %></td>
+ <td><%= link_to 'Edit', edit_cart_path(cart) %></td>
+ <td><%= link_to 'Destroy', cart, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Cart', new_cart_path %>
@@ -0,0 +1,5 @@
+<h1>New cart</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', carts_path %>
@@ -0,0 +1,9 @@
+<% if notice %>
+ <p id="notice"><%= notice %></p>
+<% end %>
+<h2>Your Pragmatic Cart</h2>
+<ul>
+ <% @cart.line_items.each do |item| %>
+ <li><%= item.product.title %></li>
+ <% end %>
+</ul>
@@ -0,0 +1,25 @@
+<%= form_for(@line_item) do |f| %>
+ <% if @line_item.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@line_item.errors.count, "error") %> prohibited this line_item from being saved:</h2>
+
+ <ul>
+ <% @line_item.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :product_id %><br />
+ <%= f.number_field :product_id %>
+ </div>
+ <div class="field">
+ <%= f.label :cart_id %><br />
+ <%= f.number_field :cart_id %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing line_item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @line_item %> |
+<%= link_to 'Back', line_items_path %>
@@ -0,0 +1,25 @@
+<h1>Listing line_items</h1>
+
+<table>
+ <tr>
+ <th>Product</th>
+ <th>Cart</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @line_items.each do |line_item| %>
+ <tr>
+ <td><%= line_item.product_id %></td>
+ <td><%= line_item.cart_id %></td>
+ <td><%= link_to 'Show', line_item %></td>
+ <td><%= link_to 'Edit', edit_line_item_path(line_item) %></td>
+ <td><%= link_to 'Destroy', line_item, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Line item', new_line_item_path %>
@@ -0,0 +1,5 @@
+<h1>New line_item</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', line_items_path %>
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Product:</b>
+ <%= @line_item.product_id %>
+</p>
+
+<p>
+ <b>Cart:</b>
+ <%= @line_item.cart_id %>
+</p>
+
+
+<%= link_to 'Edit', edit_line_item_path(@line_item) %> |
+<%= link_to 'Back', line_items_path %>
@@ -9,6 +9,7 @@
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
+ <%= button_to 'Add to Cart', line_items_path(product_id: product) %>
</div>
</div>
<% end %>
View
@@ -1,4 +1,8 @@
Depot::Application.routes.draw do
+ resources :line_items
+
+ resources :carts
+
get "store/index"
resources :products
@@ -0,0 +1,8 @@
+class CreateCarts < ActiveRecord::Migration
+ def change
+ create_table :carts do |t|
+
+ t.timestamps
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 5c659db

Please sign in to comment.