Permalink
Browse files

automatic import from ryanb/railscasts-episodes

  • Loading branch information...
0 parents commit e7f94442dc9f0fdb352731389daf514d195fd3a2 @gilesbowkett gilesbowkett committed Jul 29, 2011
Showing with 8,620 additions and 0 deletions.
  1. +3 −0 README
  2. +3 −0 store/.gitignore
  3. +4 −0 store/README
  4. +10 −0 store/Rakefile
  5. +20 −0 store/app/controllers/application.rb
  6. +5 −0 store/app/controllers/carts_controller.rb
  7. +44 −0 store/app/controllers/categories_controller.rb
  8. +8 −0 store/app/controllers/line_items_controller.rb
  9. +44 −0 store/app/controllers/products_controller.rb
  10. +3 −0 store/app/helpers/application_helper.rb
  11. +2 −0 store/app/helpers/carts_helper.rb
  12. +2 −0 store/app/helpers/categories_helper.rb
  13. +23 −0 store/app/helpers/layout_helper.rb
  14. +2 −0 store/app/helpers/line_items_helper.rb
  15. +2 −0 store/app/helpers/products_helper.rb
  16. +27 −0 store/app/models/cart.rb
  17. +3 −0 store/app/models/category.rb
  18. +8 −0 store/app/models/line_item.rb
  19. +3 −0 store/app/models/product.rb
  20. +4 −0 store/app/views/carts/purchase.html.erb
  21. +28 −0 store/app/views/carts/show.html.erb
  22. +7 −0 store/app/views/categories/_form.html.erb
  23. +8 −0 store/app/views/categories/edit.html.erb
  24. +13 −0 store/app/views/categories/index.html.erb
  25. +5 −0 store/app/views/categories/new.html.erb
  26. +13 −0 store/app/views/categories/show.html.erb
  27. +22 −0 store/app/views/layouts/application.html.erb
  28. +19 −0 store/app/views/products/_form.html.erb
  29. +8 −0 store/app/views/products/edit.html.erb
  30. +13 −0 store/app/views/products/index.html.erb
  31. +5 −0 store/app/views/products/new.html.erb
  32. +21 −0 store/app/views/products/show.html.erb
  33. +109 −0 store/config/boot.rb
  34. +19 −0 store/config/database.yml
  35. +67 −0 store/config/environment.rb
  36. +17 −0 store/config/environments/development.rb
  37. +22 −0 store/config/environments/production.rb
  38. +22 −0 store/config/environments/test.rb
  39. +10 −0 store/config/initializers/inflections.rb
  40. +5 −0 store/config/initializers/mime_types.rb
  41. +15 −0 store/config/initializers/new_rails_defaults.rb
  42. +9 −0 store/config/routes.rb
  43. +12 −0 store/db/migrate/20080621194238_create_categories.rb
  44. +15 −0 store/db/migrate/20080621194423_create_products.rb
  45. +12 −0 store/db/migrate/20081221210944_create_carts.rb
  46. +15 −0 store/db/migrate/20081221211031_create_line_items.rb
  47. +44 −0 store/db/schema.rb
  48. +2 −0 store/lib/tasks/application.rake
  49. +30 −0 store/public/404.html
  50. +30 −0 store/public/422.html
  51. +30 −0 store/public/500.html
  52. +10 −0 store/public/dispatch.cgi
  53. +24 −0 store/public/dispatch.fcgi
  54. +10 −0 store/public/dispatch.rb
  55. 0 store/public/favicon.ico
  56. +2 −0 store/public/javascripts/application.js
  57. +963 −0 store/public/javascripts/controls.js
  58. +972 −0 store/public/javascripts/dragdrop.js
  59. +1,120 −0 store/public/javascripts/effects.js
  60. +4,225 −0 store/public/javascripts/prototype.js
  61. +5 −0 store/public/robots.txt
  62. +128 −0 store/public/stylesheets/application.css
  63. +3 −0 store/script/about
  64. +3 −0 store/script/console
  65. +3 −0 store/script/dbconsole
  66. +3 −0 store/script/destroy
  67. +3 −0 store/script/generate
  68. +3 −0 store/script/performance/benchmarker
  69. +3 −0 store/script/performance/profiler
  70. +3 −0 store/script/performance/request
  71. +3 −0 store/script/plugin
  72. +3 −0 store/script/process/inspector
  73. +3 −0 store/script/process/reaper
  74. +3 −0 store/script/process/spawner
  75. +3 −0 store/script/runner
  76. +3 −0 store/script/server
  77. +2 −0 store/test/fixtures/carts.yml
  78. +11 −0 store/test/fixtures/categories.yml
  79. +5 −0 store/test/fixtures/line_items.yml
  80. +23 −0 store/test/fixtures/products.yml
  81. +20 −0 store/test/functional/carts_controller_test.rb
  82. +54 −0 store/test/functional/categories_controller_test.rb
  83. +15 −0 store/test/functional/line_items_controller_test.rb
  84. +54 −0 store/test/functional/products_controller_test.rb
  85. +38 −0 store/test/test_helper.rb
  86. +7 −0 store/test/unit/cart_test.rb
  87. +8 −0 store/test/unit/category_test.rb
  88. +7 −0 store/test/unit/line_item_test.rb
  89. +8 −0 store/test/unit/product_test.rb
  90. 0 store/vendor/plugins/.gitignore
3 README
@@ -0,0 +1,3 @@
+Railscasts Episode #141: PayPal Basics
+
+http://railscasts.com/episodes/141
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
@@ -0,0 +1,4 @@
+Railscasts Example Store App
+--
+
+To setup the app, just run `rake setup`.
@@ -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'
@@ -0,0 +1,20 @@
+# 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
+
+ def current_cart
+ session[:cart_id] ||= Cart.create!.id
+ @current_cart ||= Cart.find(session[:cart_id])
+ end
+end
@@ -0,0 +1,5 @@
+class CartsController < ApplicationController
+ def show
+ @cart = current_cart
+ end
+end
@@ -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
@@ -0,0 +1,8 @@
+class LineItemsController < ApplicationController
+ def create
+ @product = Product.find(params[:product_id])
+ @line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price)
+ flash[:notice] = "Added #{@product.name} to cart."
+ redirect_to current_cart_url
+ end
+end
@@ -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
@@ -0,0 +1,3 @@
+# Methods added to this helper will be available to all templates in the application.
+module ApplicationHelper
+end
@@ -0,0 +1,2 @@
+module CartsHelper
+end
@@ -0,0 +1,2 @@
+module CategoriesHelper
+end
@@ -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
@@ -0,0 +1,2 @@
+module LineItemsHelper
+end
@@ -0,0 +1,2 @@
+module ProductsHelper
+end
@@ -0,0 +1,27 @@
+class Cart < ActiveRecord::Base
+ has_many :line_items
+
+ def total_price
+ # convert to array so it doesn't try to do sum on database directly
+ line_items.to_a.sum(&:full_price)
+ end
+
+ def paypal_url(return_url)
+ values = {
+ :business => 'seller_1229899173_biz@railscasts.com',
+ :cmd => '_cart',
+ :upload => 1,
+ :return => return_url,
+ :invoice => id
+ }
+ line_items.each_with_index do |item, index|
+ values.merge!({
+ "amount_#{index+1}" => item.unit_price,
+ "item_name_#{index+1}" => item.product.name,
+ "item_number_#{index+1}" => item.id,
+ "quantity_#{index+1}" => item.quantity
+ })
+ end
+ "https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
+ end
+end
@@ -0,0 +1,3 @@
+class Category < ActiveRecord::Base
+ has_many :products
+end
@@ -0,0 +1,8 @@
+class LineItem < ActiveRecord::Base
+ belongs_to :cart
+ belongs_to :product
+
+ def full_price
+ unit_price * quantity
+ end
+end
@@ -0,0 +1,3 @@
+class Product < ActiveRecord::Base
+ belongs_to :category
+end
@@ -0,0 +1,4 @@
+<% title "Purchase Successful" %>
+
+<p><strong>Thank you for your order!</strong> We will process it within one business day.</p>
+<p><%= link_to "Back to Products", products_path %></p>
@@ -0,0 +1,28 @@
+<% title "Shopping Cart" %>
+
+<table id="line_items">
+ <tr>
+ <th>Product</th>
+ <th>Qty</th>
+ <th class="price">Unit Price</th>
+ <th class="price">Full Price</th>
+ </tr>
+ <% for line_item in @cart.line_items %>
+ <tr class="<%= cycle :odd, :even %>">
+ <td><%=h line_item.product.name %></td>
+ <td class="qty"><%= line_item.quantity %></td>
+ <td class="price"><%= number_to_currency(line_item.unit_price) %></td>
+ <td class="price"><%= number_to_currency(line_item.full_price) %></td>
+ </tr>
+ <% end %>
+ <tr>
+ <td class="total price" colspan="4">
+ Total: <%= number_to_currency @cart.total_price %>
+ </td>
+ </tr>
+</table>
+
+<p>
+ <%= link_to "Continue Shopping", products_url %> |
+ <%= link_to "Checkout", @cart.paypal_url(products_url) %>
+</p>
@@ -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 %>
@@ -0,0 +1,8 @@
+<% title "Edit Category" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @category %> |
+ <%= link_to "View All", categories_path %>
+</p>
@@ -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>
@@ -0,0 +1,5 @@
+<% title "New Category" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", categories_path %></p>
@@ -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>
@@ -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>
@@ -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 %>
@@ -0,0 +1,8 @@
+<% title "Edit Product" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @product %> |
+ <%= link_to "View All", products_path %>
+</p>
@@ -0,0 +1,13 @@
+<% title "Products" %>
+
+<% for product in @products %>
+ <div class="product">
+ <h3>
+ <%= link_to h(product.name), product %>
+ <%= number_to_currency(product.price) %>
+ </h3>
+ <div class="actions">
+ <%= link_to "Add to Cart", line_items_path(:product_id => product), :method => :post %>
+ </div>
+ </div>
+<% end %>
@@ -0,0 +1,5 @@
+<% title "New Product" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", products_path %></p>
Oops, something went wrong.

0 comments on commit e7f9444

Please sign in to comment.