Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
adding episode 131
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Oct 13, 2008
1 parent 0b1096d commit c5733af
Show file tree
Hide file tree
Showing 89 changed files with 8,617 additions and 0 deletions.
3 changes: 3 additions & 0 deletions episode-131/README
@@ -0,0 +1,3 @@
Railscasts Episode #131: Going Back

http://railscasts.com/episodes/131
3 changes: 3 additions & 0 deletions episode-131/store/.gitignore
@@ -0,0 +1,3 @@
tmp/*
log/*
*.sqlite3
4 changes: 4 additions & 0 deletions episode-131/store/README
@@ -0,0 +1,4 @@
Railscasts Example Store App
--

To setup the app, just run `rake setup`.
10 changes: 10 additions & 0 deletions episode-131/store/Rakefile
@@ -0,0 +1,10 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'
21 changes: 21 additions & 0 deletions episode-131/store/app/controllers/application.rb
@@ -0,0 +1,21 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time

# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => '526292d4095d0fd8135a9aa118f5956e'

# See ActionController::Base for details
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password

private

def current_cart
@current_cart ||= Cart.first || Cart.create!
end
end
8 changes: 8 additions & 0 deletions episode-131/store/app/controllers/cart_items_controller.rb
@@ -0,0 +1,8 @@
class CartItemsController < ApplicationController
def create
current_cart.cart_items.create!(params[:cart_item])
flash[:notice] = "Product added to cart"
session[:last_product_page] = request.env['HTTP_REFERER'] || products_url
redirect_to current_cart_url
end
end
5 changes: 5 additions & 0 deletions episode-131/store/app/controllers/carts_controller.rb
@@ -0,0 +1,5 @@
class CartsController < ApplicationController
def show
@cart = current_cart
end
end
44 changes: 44 additions & 0 deletions episode-131/store/app/controllers/categories_controller.rb
@@ -0,0 +1,44 @@
class CategoriesController < ApplicationController
def index
@categories = Category.find(:all)
end

def show
@category = Category.find(params[:id])
end

def new
@category = Category.new
end

def create
@category = Category.new(params[:category])
if @category.save
flash[:notice] = "Successfully created category."
redirect_to @category
else
render :action => 'new'
end
end

def edit
@category = Category.find(params[:id])
end

def update
@category = Category.find(params[:id])
if @category.update_attributes(params[:category])
flash[:notice] = "Successfully updated category."
redirect_to @category
else
render :action => 'edit'
end
end

def destroy
@category = Category.find(params[:id])
@category.destroy
flash[:notice] = "Successfully destroyed category."
redirect_to categories_url
end
end
44 changes: 44 additions & 0 deletions episode-131/store/app/controllers/products_controller.rb
@@ -0,0 +1,44 @@
class ProductsController < ApplicationController
def index
@products = Product.all(:limit => 10)
end

def show
@product = Product.find(params[:id])
end

def new
@product = Product.new
end

def create
@product = Product.new(params[:product])
if @product.save
flash[:notice] = "Successfully created product."
redirect_to @product
else
render :action => 'new'
end
end

def edit
@product = Product.find(params[:id])
end

def update
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
flash[:notice] = "Successfully updated product."
redirect_to @product
else
render :action => 'edit'
end
end

def destroy
@product = Product.find(params[:id])
@product.destroy
flash[:notice] = "Successfully destroyed product."
redirect_to products_url
end
end
3 changes: 3 additions & 0 deletions episode-131/store/app/helpers/application_helper.rb
@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end
2 changes: 2 additions & 0 deletions episode-131/store/app/helpers/cart_items_helper.rb
@@ -0,0 +1,2 @@
module CartItemsHelper
end
2 changes: 2 additions & 0 deletions episode-131/store/app/helpers/carts_helper.rb
@@ -0,0 +1,2 @@
module CartsHelper
end
2 changes: 2 additions & 0 deletions episode-131/store/app/helpers/categories_helper.rb
@@ -0,0 +1,2 @@
module CategoriesHelper
end
23 changes: 23 additions & 0 deletions episode-131/store/app/helpers/layout_helper.rb
@@ -0,0 +1,23 @@
# These helper methods can be called in your template to set variables to be used in the layout
# This module should be included in all views globally,
# to do so you may need to add this line to your ApplicationController
# helper :layout
module LayoutHelper
def title(page_title, show_title = true)
@content_for_title = page_title.to_s
@show_title = show_title
end

def show_title?
@show_title
end

def stylesheet(*args)
content_for(:head) { stylesheet_link_tag(*args.map(&:to_s)) }
end

def javascript(*args)
args = args.map { |arg| arg == :defaults ? arg : arg.to_s }
content_for(:head) { javascript_include_tag(*args) }
end
end
2 changes: 2 additions & 0 deletions episode-131/store/app/helpers/products_helper.rb
@@ -0,0 +1,2 @@
module ProductsHelper
end
8 changes: 8 additions & 0 deletions episode-131/store/app/models/cart.rb
@@ -0,0 +1,8 @@
class Cart < ActiveRecord::Base
has_many :cart_items
has_many :products, :through => :cart_items

def total_price
cart_items.to_a.sum(&:full_price)
end
end
20 changes: 20 additions & 0 deletions episode-131/store/app/models/cart_item.rb
@@ -0,0 +1,20 @@
class CartItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart

before_create :default_quantity_to_one

def unit_price
product.price
end

def full_price
unit_price*quantity
end

private

def default_quantity_to_one
self.quantity ||= 1
end
end
3 changes: 3 additions & 0 deletions episode-131/store/app/models/category.rb
@@ -0,0 +1,3 @@
class Category < ActiveRecord::Base
has_many :products
end
4 changes: 4 additions & 0 deletions episode-131/store/app/models/product.rb
@@ -0,0 +1,4 @@
class Product < ActiveRecord::Base
belongs_to :category
has_many :cart_items
end
30 changes: 30 additions & 0 deletions episode-131/store/app/views/carts/show.html.erb
@@ -0,0 +1,30 @@
<% title "Cart" %>

<table id="cart_items">
<tr>
<th>Product</th>
<th>Qty</th>
<th class="price">Unit Price</th>
<th class="price">Full Price</th>
</tr>
<% for cart_item in @cart.cart_items %>
<tr class="<%= cycle :odd, :even %>">
<td><%=h cart_item.product.name %></td>
<td class="qty"><%= cart_item.quantity %></td>
<td class="price"><%= number_to_currency(cart_item.unit_price) %></td>
<td class="price"><%= number_to_currency(cart_item.full_price) %></td>
</tr>
<% end %>
<tr>
<td class="total price" colspan="4">
Total: <%= number_to_currency @cart.total_price %>
</td>
</tr>
</table>

<p>
<% if session[:last_product_page] %>
<%= link_to "Continue Shopping", session[:last_product_page] %> |
<% end %>
<%= link_to "Checkout" %>
</p>
7 changes: 7 additions & 0 deletions episode-131/store/app/views/categories/_form.html.erb
@@ -0,0 +1,7 @@
<% form_for @category do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
8 changes: 8 additions & 0 deletions episode-131/store/app/views/categories/edit.html.erb
@@ -0,0 +1,8 @@
<% title "Edit Category" %>
<%= render :partial => 'form' %>

<p>
<%= link_to "Show", @category %> |
<%= link_to "View All", categories_path %>
</p>
13 changes: 13 additions & 0 deletions episode-131/store/app/views/categories/index.html.erb
@@ -0,0 +1,13 @@
<% title "Categories" %>
<% for category in @categories %>
<div class="category">
<h3><%= link_to h(category.name), category %></h3>
<div class="actions">
<%= link_to "Edit", edit_category_path(category) %> |
<%= link_to "Destroy", category, :confirm => 'Are you sure?', :method => :delete %>
</div>
</div>
<% end %>

<p><%= link_to "New Category", new_category_path %></p>
5 changes: 5 additions & 0 deletions episode-131/store/app/views/categories/new.html.erb
@@ -0,0 +1,5 @@
<% title "New Category" %>
<%= render :partial => 'form' %>

<p><%= link_to "Back to List", categories_path %></p>
13 changes: 13 additions & 0 deletions episode-131/store/app/views/categories/show.html.erb
@@ -0,0 +1,13 @@
<% title @category.name %>

<ul>
<% for product in @category.products %>
<li><%= link_to h(product.name), product %></li>
<% end %>
</ul>

<p>
<%= link_to "Edit", edit_category_path(@category) %> |
<%= link_to "Destroy", @category, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", categories_path %>
</p>
22 changes: 22 additions & 0 deletions episode-131/store/app/views/layouts/application.html.erb
@@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><%= h(yield(:title) || "Untitled") %></title>
<%= stylesheet_link_tag 'application' %>
<%= yield(:head) %>
</head>
<body>
<div id="container">
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<%- end -%>
<%- if show_title? -%>
<h1><%=h yield(:title) %></h1>
<%- end -%>
<%= yield %>
</div>
</body>
</html>
19 changes: 19 additions & 0 deletions episode-131/store/app/views/products/_form.html.erb
@@ -0,0 +1,19 @@
<% form_for @product do |f| %>
<p>
<%= f.label :category_id %><br />
<%= f.collection_select :category_id, Category.all, :id, :name %>
</p>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
8 changes: 8 additions & 0 deletions episode-131/store/app/views/products/edit.html.erb
@@ -0,0 +1,8 @@
<% title "Edit Product" %>
<%= render :partial => 'form' %>

<p>
<%= link_to "Show", @product %> |
<%= link_to "View All", products_path %>
</p>
15 changes: 15 additions & 0 deletions episode-131/store/app/views/products/index.html.erb
@@ -0,0 +1,15 @@
<% title "Products" %>
<% for product in @products %>
<div class="product">
<h3>
<%= link_to h(product.name), product %>
<%= number_to_currency(product.price) %>
</h3>
Category: <%=h product.category.name %>
<% form_for product.cart_items.build do |f| %>
<%= f.hidden_field :product_id %>
<%= f.submit "Add to Cart" %>
<% end %>
</div>
<% end %>
5 changes: 5 additions & 0 deletions episode-131/store/app/views/products/new.html.erb
@@ -0,0 +1,5 @@
<% title "New Product" %>
<%= render :partial => 'form' %>

<p><%= link_to "Back to List", products_path %></p>

0 comments on commit c5733af

Please sign in to comment.