Permalink
Browse files

adding episode 153

  • Loading branch information...
1 parent 1636803 commit b5ade830c0555718b914b10ea12a113b2d90680e @ryanb committed Mar 16, 2009
Showing with 9,732 additions and 0 deletions.
  1. +8 −0 episode-153/README
  2. +3 −0 episode-153/store/.gitignore
  3. +4 −0 episode-153/store/README
  4. +10 −0 episode-153/store/Rakefile
  5. +27 −0 episode-153/store/app/controllers/application_controller.rb
  6. +5 −0 episode-153/store/app/controllers/carts_controller.rb
  7. +44 −0 episode-153/store/app/controllers/categories_controller.rb
  8. +8 −0 episode-153/store/app/controllers/line_items_controller.rb
  9. +25 −0 episode-153/store/app/controllers/orders_controller.rb
  10. +44 −0 episode-153/store/app/controllers/products_controller.rb
  11. +3 −0 episode-153/store/app/helpers/application_helper.rb
  12. +2 −0 episode-153/store/app/helpers/carts_helper.rb
  13. +2 −0 episode-153/store/app/helpers/categories_helper.rb
  14. +23 −0 episode-153/store/app/helpers/layout_helper.rb
  15. +2 −0 episode-153/store/app/helpers/line_items_helper.rb
  16. +2 −0 episode-153/store/app/helpers/orders_helper.rb
  17. +2 −0 episode-153/store/app/helpers/products_helper.rb
  18. +9 −0 episode-153/store/app/models/cart.rb
  19. +3 −0 episode-153/store/app/models/category.rb
  20. +8 −0 episode-153/store/app/models/line_item.rb
  21. +55 −0 episode-153/store/app/models/order.rb
  22. +16 −0 episode-153/store/app/models/order_transaction.rb
  23. +3 −0 episode-153/store/app/models/product.rb
  24. +4 −0 episode-153/store/app/views/carts/purchase.html.erb
  25. +28 −0 episode-153/store/app/views/carts/show.html.erb
  26. +7 −0 episode-153/store/app/views/categories/_form.html.erb
  27. +8 −0 episode-153/store/app/views/categories/edit.html.erb
  28. +13 −0 episode-153/store/app/views/categories/index.html.erb
  29. +5 −0 episode-153/store/app/views/categories/new.html.erb
  30. +13 −0 episode-153/store/app/views/categories/show.html.erb
  31. +22 −0 episode-153/store/app/views/layouts/application.html.erb
  32. +1 −0 episode-153/store/app/views/orders/failure.html.erb
  33. +32 −0 episode-153/store/app/views/orders/new.html.erb
  34. +25 −0 episode-153/store/app/views/orders/show.html.erb
  35. +21 −0 episode-153/store/app/views/orders/show.pdf.prawn
  36. +1 −0 episode-153/store/app/views/orders/success.html.erb
  37. +19 −0 episode-153/store/app/views/products/_form.html.erb
  38. +8 −0 episode-153/store/app/views/products/edit.html.erb
  39. +13 −0 episode-153/store/app/views/products/index.html.erb
  40. +5 −0 episode-153/store/app/views/products/new.html.erb
  41. +21 −0 episode-153/store/app/views/products/show.html.erb
  42. +109 −0 episode-153/store/config/boot.rb
  43. +19 −0 episode-153/store/config/database.yml
  44. +68 −0 episode-153/store/config/environment.rb
  45. +26 −0 episode-153/store/config/environments/development.rb
  46. +31 −0 episode-153/store/config/environments/production.rb
  47. +27 −0 episode-153/store/config/environments/test.rb
  48. +10 −0 episode-153/store/config/initializers/inflections.rb
  49. +5 −0 episode-153/store/config/initializers/mime_types.rb
  50. +15 −0 episode-153/store/config/initializers/new_rails_defaults.rb
  51. +11 −0 episode-153/store/config/routes.rb
  52. +12 −0 episode-153/store/db/migrate/20080621194238_create_categories.rb
  53. +15 −0 episode-153/store/db/migrate/20080621194423_create_products.rb
  54. +12 −0 episode-153/store/db/migrate/20081221210944_create_carts.rb
  55. +15 −0 episode-153/store/db/migrate/20081221211031_create_line_items.rb
  56. +17 −0 episode-153/store/db/migrate/20090119051232_create_orders.rb
  57. +19 −0 episode-153/store/db/migrate/20090119062501_create_order_transactions.rb
  58. +67 −0 episode-153/store/db/schema.rb
  59. +2 −0 episode-153/store/lib/tasks/application.rake
  60. +30 −0 episode-153/store/public/404.html
  61. +30 −0 episode-153/store/public/422.html
  62. +30 −0 episode-153/store/public/500.html
  63. +10 −0 episode-153/store/public/dispatch.cgi
  64. +24 −0 episode-153/store/public/dispatch.fcgi
  65. +10 −0 episode-153/store/public/dispatch.rb
  66. 0 episode-153/store/public/favicon.ico
  67. +2 −0 episode-153/store/public/javascripts/application.js
  68. +963 −0 episode-153/store/public/javascripts/controls.js
  69. +973 −0 episode-153/store/public/javascripts/dragdrop.js
  70. +1,128 −0 episode-153/store/public/javascripts/effects.js
  71. +4,320 −0 episode-153/store/public/javascripts/prototype.js
  72. +5 −0 episode-153/store/public/robots.txt
  73. +128 −0 episode-153/store/public/stylesheets/application.css
  74. +3 −0 episode-153/store/script/about
  75. +3 −0 episode-153/store/script/console
  76. +3 −0 episode-153/store/script/dbconsole
  77. +3 −0 episode-153/store/script/destroy
  78. +3 −0 episode-153/store/script/generate
  79. +3 −0 episode-153/store/script/performance/benchmarker
  80. +3 −0 episode-153/store/script/performance/profiler
  81. +3 −0 episode-153/store/script/performance/request
  82. +3 −0 episode-153/store/script/plugin
  83. +3 −0 episode-153/store/script/process/inspector
  84. +3 −0 episode-153/store/script/process/reaper
  85. +3 −0 episode-153/store/script/process/spawner
  86. +3 −0 episode-153/store/script/runner
  87. +3 −0 episode-153/store/script/server
  88. +2 −0 episode-153/store/test/fixtures/carts.yml
  89. +11 −0 episode-153/store/test/fixtures/categories.yml
  90. +5 −0 episode-153/store/test/fixtures/line_items.yml
  91. +19 −0 episode-153/store/test/fixtures/order_transactions.yml
  92. +15 −0 episode-153/store/test/fixtures/orders.yml
  93. +23 −0 episode-153/store/test/fixtures/products.yml
  94. +20 −0 episode-153/store/test/functional/carts_controller_test.rb
  95. +54 −0 episode-153/store/test/functional/categories_controller_test.rb
  96. +15 −0 episode-153/store/test/functional/line_items_controller_test.rb
  97. +20 −0 episode-153/store/test/functional/orders_controller_test.rb
  98. +54 −0 episode-153/store/test/functional/products_controller_test.rb
  99. +38 −0 episode-153/store/test/test_helper.rb
  100. +7 −0 episode-153/store/test/unit/cart_test.rb
  101. +8 −0 episode-153/store/test/unit/category_test.rb
  102. +7 −0 episode-153/store/test/unit/line_item_test.rb
  103. +7 −0 episode-153/store/test/unit/order_test.rb
  104. +8 −0 episode-153/store/test/unit/order_transaction_test.rb
  105. +8 −0 episode-153/store/test/unit/product_test.rb
  106. 0 episode-153/store/vendor/plugins/.gitignore
  107. +3 −0 episode-153/store/vendor/plugins/prawnto/.gitignore
  108. +20 −0 episode-153/store/vendor/plugins/prawnto/MIT-LICENSE
  109. +12 −0 episode-153/store/vendor/plugins/prawnto/README
  110. +22 −0 episode-153/store/vendor/plugins/prawnto/Rakefile
  111. +7 −0 episode-153/store/vendor/plugins/prawnto/init.rb
  112. +32 −0 episode-153/store/vendor/plugins/prawnto/lib/prawnto.rb
  113. +45 −0 episode-153/store/vendor/plugins/prawnto/lib/prawnto/action_controller.rb
  114. +12 −0 episode-153/store/vendor/plugins/prawnto/lib/prawnto/action_view.rb
  115. +72 −0 episode-153/store/vendor/plugins/prawnto/lib/prawnto/template_handler/compile_support.rb
  116. +16 −0 episode-153/store/vendor/plugins/prawnto/lib/prawnto/template_handlers/base.rb
  117. +16 −0 episode-153/store/vendor/plugins/prawnto/lib/prawnto/template_handlers/dsl.rb
  118. +64 −0 episode-153/store/vendor/plugins/prawnto/lib/prawnto/template_handlers/raw.rb
  119. +4 −0 episode-153/store/vendor/plugins/prawnto/tasks/prawnto_tasks.rake
  120. +38 −0 episode-153/store/vendor/plugins/prawnto/test/action_controller_test.rb
  121. +39 −0 episode-153/store/vendor/plugins/prawnto/test/base_template_handler_test.rb
  122. +40 −0 episode-153/store/vendor/plugins/prawnto/test/dsl_template_handler_test.rb
  123. +163 −0 episode-153/store/vendor/plugins/prawnto/test/raw_template_handler_test.rb
  124. +77 −0 episode-153/store/vendor/plugins/prawnto/test/template_handler_test_mocks.rb
View
@@ -0,0 +1,8 @@
+Railscasts Episode #153: PDFs with Prawn
+
+http://railscasts.com/episodes/153
+
+Commands
+
+ sudo gem install prawn
+ script/plugin install git://github.com/thorny-sun/prawnto.git
@@ -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 :card_number, :card_verification
+
+ 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,25 @@
+class OrdersController < ApplicationController
+ prawnto :prawn => { :top_margin => 75 }
+
+ def show
+ @order = Order.find(params[:id])
+ end
+
+ def new
+ @order = Order.new
+ 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,55 @@
+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 = GATEWAY.purchase(price_in_cents, credit_card, purchase_options)
+ 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
+
+ private
+
+ def 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 validate_card
+ unless 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,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_path %> |
+ <%= link_to "Checkout", new_order_path %>
+</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>
Oops, something went wrong.

0 comments on commit b5ade83

Please sign in to comment.