Permalink
Browse files

adding episode 146

  • Loading branch information...
1 parent dbe194b commit f67696378bcf453fe28d37a7ae40a3844bdac7d6 @ryanb committed Jan 26, 2009
Showing with 8,954 additions and 0 deletions.
  1. +8 −0 episode-146/README
  2. +3 −0 episode-146/store/.gitignore
  3. +4 −0 episode-146/store/README
  4. +10 −0 episode-146/store/Rakefile
  5. +27 −0 episode-146/store/app/controllers/application.rb
  6. +5 −0 episode-146/store/app/controllers/carts_controller.rb
  7. +44 −0 episode-146/store/app/controllers/categories_controller.rb
  8. +8 −0 episode-146/store/app/controllers/line_items_controller.rb
  9. +28 −0 episode-146/store/app/controllers/orders_controller.rb
  10. +44 −0 episode-146/store/app/controllers/products_controller.rb
  11. +3 −0 episode-146/store/app/helpers/application_helper.rb
  12. +2 −0 episode-146/store/app/helpers/carts_helper.rb
  13. +2 −0 episode-146/store/app/helpers/categories_helper.rb
  14. +23 −0 episode-146/store/app/helpers/layout_helper.rb
  15. +2 −0 episode-146/store/app/helpers/line_items_helper.rb
  16. +2 −0 episode-146/store/app/helpers/orders_helper.rb
  17. +2 −0 episode-146/store/app/helpers/products_helper.rb
  18. +9 −0 episode-146/store/app/models/cart.rb
  19. +3 −0 episode-146/store/app/models/category.rb
  20. +8 −0 episode-146/store/app/models/line_item.rb
  21. +81 −0 episode-146/store/app/models/order.rb
  22. +16 −0 episode-146/store/app/models/order_transaction.rb
  23. +3 −0 episode-146/store/app/models/product.rb
  24. +4 −0 episode-146/store/app/views/carts/purchase.html.erb
  25. +31 −0 episode-146/store/app/views/carts/show.html.erb
  26. +7 −0 episode-146/store/app/views/categories/_form.html.erb
  27. +8 −0 episode-146/store/app/views/categories/edit.html.erb
  28. +13 −0 episode-146/store/app/views/categories/index.html.erb
  29. +5 −0 episode-146/store/app/views/categories/new.html.erb
  30. +13 −0 episode-146/store/app/views/categories/show.html.erb
  31. +22 −0 episode-146/store/app/views/layouts/application.html.erb
  32. +1 −0 episode-146/store/app/views/orders/failure.html.erb
  33. +36 −0 episode-146/store/app/views/orders/new.html.erb
  34. +1 −0 episode-146/store/app/views/orders/success.html.erb
  35. +19 −0 episode-146/store/app/views/products/_form.html.erb
  36. +8 −0 episode-146/store/app/views/products/edit.html.erb
  37. +13 −0 episode-146/store/app/views/products/index.html.erb
  38. +5 −0 episode-146/store/app/views/products/new.html.erb
  39. +21 −0 episode-146/store/app/views/products/show.html.erb
  40. +109 −0 episode-146/store/config/boot.rb
  41. +19 −0 episode-146/store/config/database.yml
  42. +68 −0 episode-146/store/config/environment.rb
  43. +28 −0 episode-146/store/config/environments/development.rb
  44. +33 −0 episode-146/store/config/environments/production.rb
  45. +28 −0 episode-146/store/config/environments/test.rb
  46. +10 −0 episode-146/store/config/initializers/inflections.rb
  47. +5 −0 episode-146/store/config/initializers/mime_types.rb
  48. +15 −0 episode-146/store/config/initializers/new_rails_defaults.rb
  49. +11 −0 episode-146/store/config/routes.rb
  50. +12 −0 episode-146/store/db/migrate/20080621194238_create_categories.rb
  51. +15 −0 episode-146/store/db/migrate/20080621194423_create_products.rb
  52. +12 −0 episode-146/store/db/migrate/20081221210944_create_carts.rb
  53. +15 −0 episode-146/store/db/migrate/20081221211031_create_line_items.rb
  54. +17 −0 episode-146/store/db/migrate/20090119051232_create_orders.rb
  55. +19 −0 episode-146/store/db/migrate/20090119062501_create_order_transactions.rb
  56. +11 −0 episode-146/store/db/migrate/20090125063131_add_express_token_to_orders.rb
  57. +69 −0 episode-146/store/db/schema.rb
  58. +2 −0 episode-146/store/lib/tasks/application.rake
  59. +30 −0 episode-146/store/public/404.html
  60. +30 −0 episode-146/store/public/422.html
  61. +30 −0 episode-146/store/public/500.html
  62. +10 −0 episode-146/store/public/dispatch.cgi
  63. +24 −0 episode-146/store/public/dispatch.fcgi
  64. +10 −0 episode-146/store/public/dispatch.rb
  65. 0 episode-146/store/public/favicon.ico
  66. +2 −0 episode-146/store/public/javascripts/application.js
  67. +963 −0 episode-146/store/public/javascripts/controls.js
  68. +972 −0 episode-146/store/public/javascripts/dragdrop.js
  69. +1,120 −0 episode-146/store/public/javascripts/effects.js
  70. +4,225 −0 episode-146/store/public/javascripts/prototype.js
  71. +5 −0 episode-146/store/public/robots.txt
  72. +128 −0 episode-146/store/public/stylesheets/application.css
  73. +3 −0 episode-146/store/script/about
  74. +3 −0 episode-146/store/script/console
  75. +3 −0 episode-146/store/script/dbconsole
  76. +3 −0 episode-146/store/script/destroy
  77. +3 −0 episode-146/store/script/generate
  78. +3 −0 episode-146/store/script/performance/benchmarker
  79. +3 −0 episode-146/store/script/performance/profiler
  80. +3 −0 episode-146/store/script/performance/request
  81. +3 −0 episode-146/store/script/plugin
  82. +3 −0 episode-146/store/script/process/inspector
  83. +3 −0 episode-146/store/script/process/reaper
  84. +3 −0 episode-146/store/script/process/spawner
  85. +3 −0 episode-146/store/script/runner
  86. +3 −0 episode-146/store/script/server
  87. +2 −0 episode-146/store/test/fixtures/carts.yml
  88. +11 −0 episode-146/store/test/fixtures/categories.yml
  89. +5 −0 episode-146/store/test/fixtures/line_items.yml
  90. +19 −0 episode-146/store/test/fixtures/order_transactions.yml
  91. +15 −0 episode-146/store/test/fixtures/orders.yml
  92. +23 −0 episode-146/store/test/fixtures/products.yml
  93. +20 −0 episode-146/store/test/functional/carts_controller_test.rb
  94. +54 −0 episode-146/store/test/functional/categories_controller_test.rb
  95. +15 −0 episode-146/store/test/functional/line_items_controller_test.rb
  96. +20 −0 episode-146/store/test/functional/orders_controller_test.rb
  97. +54 −0 episode-146/store/test/functional/products_controller_test.rb
  98. +38 −0 episode-146/store/test/test_helper.rb
  99. +7 −0 episode-146/store/test/unit/cart_test.rb
  100. +8 −0 episode-146/store/test/unit/category_test.rb
  101. +7 −0 episode-146/store/test/unit/line_item_test.rb
  102. +7 −0 episode-146/store/test/unit/order_test.rb
  103. +8 −0 episode-146/store/test/unit/order_transaction_test.rb
  104. +8 −0 episode-146/store/test/unit/product_test.rb
  105. 0 episode-146/store/vendor/plugins/.gitignore
View
@@ -0,0 +1,8 @@
+Railscasts Episode #146: PayPal Express Checkout
+
+http://railscasts.com/episodes/146
+
+Commands
+
+ script/generate migration add_express_token_to_orders express_token:string express_payer_id:string
+ rake db:migrate
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
View
@@ -0,0 +1,4 @@
+Railscasts Example Store App
+--
+
+To setup the app, just run `rake setup`.
View
@@ -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,27 @@
+# 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
+ if session[:cart_id]
+ @current_cart ||= Cart.find(session[:cart_id])
+ session[:cart_id] = nil if @current_cart.purchased_at
+ end
+ if session[:cart_id].nil?
+ @current_cart = Cart.create!
+ session[:cart_id] = @current_cart.id
+ end
+ @current_cart
+ 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,28 @@
+class OrdersController < ApplicationController
+ def express
+ response = EXPRESS_GATEWAY.setup_purchase(current_cart.build_order.price_in_cents,
+ :ip => request.remote_ip,
+ :return_url => new_order_url,
+ :cancel_return_url => products_url
+ )
+ redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)
+ end
+
+ def new
+ @order = Order.new(:express_token => params[:token])
+ end
+
+ def create
+ @order = current_cart.build_order(params[:order])
+ @order.ip_address = request.remote_ip
+ if @order.save
+ if @order.purchase
+ render :action => "success"
+ else
+ render :action => "failure"
+ end
+ else
+ render :action => 'new'
+ end
+ 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 OrdersHelper
+end
@@ -0,0 +1,2 @@
+module ProductsHelper
+end
@@ -0,0 +1,9 @@
+class Cart < ActiveRecord::Base
+ has_many :line_items
+ has_one :order
+
+ 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
+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,81 @@
+class Order < ActiveRecord::Base
+ belongs_to :cart
+ has_many :transactions, :class_name => "OrderTransaction"
+
+ attr_accessor :card_number, :card_verification
+
+ validate_on_create :validate_card
+
+ def purchase
+ response = process_purchase
+ transactions.create!(:action => "purchase", :amount => price_in_cents, :response => response)
+ cart.update_attribute(:purchased_at, Time.now) if response.success?
+ response.success?
+ end
+
+ def price_in_cents
+ (cart.total_price*100).round
+ end
+
+ def express_token=(token)
+ self[:express_token] = token
+ if new_record? && !token.blank?
+ details = EXPRESS_GATEWAY.details_for(token)
+ self.express_payer_id = details.payer_id
+ self.first_name = details.params["first_name"]
+ self.last_name = details.params["last_name"]
+ end
+ end
+
+ private
+
+ def process_purchase
+ if express_token.blank?
+ STANDARD_GATEWAY.purchase(price_in_cents, credit_card, standard_purchase_options)
+ else
+ EXPRESS_GATEWAY.purchase(price_in_cents, express_purchase_options)
+ end
+ end
+
+ def standard_purchase_options
+ {
+ :ip => ip_address,
+ :billing_address => {
+ :name => "Ryan Bates",
+ :address1 => "123 Main St.",
+ :city => "New York",
+ :state => "NY",
+ :country => "US",
+ :zip => "10001"
+ }
+ }
+ end
+
+ def express_purchase_options
+ {
+ :ip => ip_address,
+ :token => express_token,
+ :payer_id => express_payer_id
+ }
+ end
+
+ def validate_card
+ if express_token.blank? && !credit_card.valid?
+ credit_card.errors.full_messages.each do |message|
+ errors.add_to_base message
+ end
+ end
+ end
+
+ def credit_card
+ @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
+ :type => card_type,
+ :number => card_number,
+ :verification_value => card_verification,
+ :month => card_expires_on.month,
+ :year => card_expires_on.year,
+ :first_name => first_name,
+ :last_name => last_name
+ )
+ end
+end
@@ -0,0 +1,16 @@
+class OrderTransaction < ActiveRecord::Base
+ belongs_to :order
+ serialize :params
+
+ def response=(response)
+ self.success = response.success?
+ self.authorization = response.authorization
+ self.message = response.message
+ self.params = response.params
+ rescue ActiveMerchant::ActiveMerchantError => e
+ self.success = false
+ self.authorization = nil
+ self.message = e.message
+ self.params = {}
+ 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,31 @@
+<% 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_path %> |
+ <%= link_to "Checkout", new_order_path %>
+</p>
+<p>
+ <%= link_to image_tag("https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"), express_new_order_path %>
+</p>
Oops, something went wrong.

0 comments on commit f676963

Please sign in to comment.