Skip to content
This repository
Browse code

adding episode 137

  • Loading branch information...
commit 7bfc64ab3bd71fee2d3967914ff54b29fc979806 1 parent 3022f5b
Ryan Bates authored November 22, 2008

Showing 74 changed files with 8,412 additions and 0 deletions. Show diff stats Hide diff stats

  1. 11  episode-137/README
  2. 3  episode-137/store/.gitignore
  3. 4  episode-137/store/README
  4. 10  episode-137/store/Rakefile
  5. 15  episode-137/store/app/controllers/application.rb
  6. 44  episode-137/store/app/controllers/categories_controller.rb
  7. 44  episode-137/store/app/controllers/products_controller.rb
  8. 3  episode-137/store/app/helpers/application_helper.rb
  9. 2  episode-137/store/app/helpers/categories_helper.rb
  10. 23  episode-137/store/app/helpers/layout_helper.rb
  11. 2  episode-137/store/app/helpers/products_helper.rb
  12. 3  episode-137/store/app/models/category.rb
  13. 12  episode-137/store/app/models/product.rb
  14. 7  episode-137/store/app/views/categories/_form.html.erb
  15. 8  episode-137/store/app/views/categories/edit.html.erb
  16. 13  episode-137/store/app/views/categories/index.html.erb
  17. 5  episode-137/store/app/views/categories/new.html.erb
  18. 13  episode-137/store/app/views/categories/show.html.erb
  19. 22  episode-137/store/app/views/layouts/application.html.erb
  20. 19  episode-137/store/app/views/products/_form.html.erb
  21. 8  episode-137/store/app/views/products/edit.html.erb
  22. 16  episode-137/store/app/views/products/index.html.erb
  23. 5  episode-137/store/app/views/products/new.html.erb
  24. 21  episode-137/store/app/views/products/show.html.erb
  25. 109  episode-137/store/config/boot.rb
  26. 19  episode-137/store/config/database.yml
  27. 67  episode-137/store/config/environment.rb
  28. 17  episode-137/store/config/environments/development.rb
  29. 22  episode-137/store/config/environments/production.rb
  30. 22  episode-137/store/config/environments/test.rb
  31. 10  episode-137/store/config/initializers/inflections.rb
  32. 5  episode-137/store/config/initializers/mime_types.rb
  33. 15  episode-137/store/config/initializers/new_rails_defaults.rb
  34. 5  episode-137/store/config/routes.rb
  35. 12  episode-137/store/db/migrate/20080621194238_create_categories.rb
  36. 15  episode-137/store/db/migrate/20080621194423_create_products.rb
  37. 29  episode-137/store/db/schema.rb
  38. 23  episode-137/store/lib/tasks/application.rake
  39. 30  episode-137/store/public/404.html
  40. 30  episode-137/store/public/422.html
  41. 30  episode-137/store/public/500.html
  42. 10  episode-137/store/public/dispatch.cgi
  43. 24  episode-137/store/public/dispatch.fcgi
  44. 10  episode-137/store/public/dispatch.rb
  45. 0  episode-137/store/public/favicon.ico
  46. 2  episode-137/store/public/javascripts/application.js
  47. 963  episode-137/store/public/javascripts/controls.js
  48. 972  episode-137/store/public/javascripts/dragdrop.js
  49. 1,120  episode-137/store/public/javascripts/effects.js
  50. 4,225  episode-137/store/public/javascripts/prototype.js
  51. 5  episode-137/store/public/robots.txt
  52. 88  episode-137/store/public/stylesheets/application.css
  53. 3  episode-137/store/script/about
  54. 3  episode-137/store/script/console
  55. 3  episode-137/store/script/dbconsole
  56. 3  episode-137/store/script/destroy
  57. 3  episode-137/store/script/generate
  58. 3  episode-137/store/script/performance/benchmarker
  59. 3  episode-137/store/script/performance/profiler
  60. 3  episode-137/store/script/performance/request
  61. 3  episode-137/store/script/plugin
  62. 3  episode-137/store/script/process/inspector
  63. 3  episode-137/store/script/process/reaper
  64. 3  episode-137/store/script/process/spawner
  65. 3  episode-137/store/script/runner
  66. 3  episode-137/store/script/server
  67. 5  episode-137/store/test/fixtures/categories.yml
  68. 11  episode-137/store/test/fixtures/products.yml
  69. 54  episode-137/store/test/functional/categories_controller_test.rb
  70. 54  episode-137/store/test/functional/products_controller_test.rb
  71. 38  episode-137/store/test/test_helper.rb
  72. 8  episode-137/store/test/unit/category_test.rb
  73. 8  episode-137/store/test/unit/product_test.rb
  74. 0  episode-137/store/vendor/plugins/.gitignore
11  episode-137/README
... ...
@@ -0,0 +1,11 @@
  1
+Railscasts Episode #137: Memoization
  2
+
  3
+http://railscasts.com/episodes/137
  4
+
  5
+Commands
  6
+
  7
+  sudo gem install rails
  8
+  sudo gem update --system
  9
+  sudo gem install rubygems-update
  10
+  sudo update_rubygems
  11
+  gem -v
3  episode-137/store/.gitignore
... ...
@@ -0,0 +1,3 @@
  1
+tmp/*
  2
+log/*
  3
+*.sqlite3
4  episode-137/store/README
... ...
@@ -0,0 +1,4 @@
  1
+Railscasts Example Store App
  2
+--
  3
+
  4
+To setup the app, just run `rake setup`.
10  episode-137/store/Rakefile
... ...
@@ -0,0 +1,10 @@
  1
+# Add your own tasks in files placed in lib/tasks ending in .rake,
  2
+# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
  3
+
  4
+require(File.join(File.dirname(__FILE__), 'config', 'boot'))
  5
+
  6
+require 'rake'
  7
+require 'rake/testtask'
  8
+require 'rake/rdoctask'
  9
+
  10
+require 'tasks/rails'
15  episode-137/store/app/controllers/application.rb
... ...
@@ -0,0 +1,15 @@
  1
+# Filters added to this controller apply to all controllers in the application.
  2
+# Likewise, all the methods added will be available for all controllers.
  3
+
  4
+class ApplicationController < ActionController::Base
  5
+  helper :all # include all helpers, all the time
  6
+
  7
+  # See ActionController::RequestForgeryProtection for details
  8
+  # Uncomment the :secret if you're not using the cookie session store
  9
+  protect_from_forgery # :secret => '526292d4095d0fd8135a9aa118f5956e'
  10
+  
  11
+  # See ActionController::Base for details 
  12
+  # Uncomment this to filter the contents of submitted sensitive data parameters
  13
+  # from your application log (in this case, all fields with names like "password"). 
  14
+  # filter_parameter_logging :password
  15
+end
44  episode-137/store/app/controllers/categories_controller.rb
... ...
@@ -0,0 +1,44 @@
  1
+class CategoriesController < ApplicationController
  2
+  def index
  3
+    @categories = Category.find(:all)
  4
+  end
  5
+  
  6
+  def show
  7
+    @category = Category.find(params[:id])
  8
+  end
  9
+  
  10
+  def new
  11
+    @category = Category.new
  12
+  end
  13
+  
  14
+  def create
  15
+    @category = Category.new(params[:category])
  16
+    if @category.save
  17
+      flash[:notice] = "Successfully created category."
  18
+      redirect_to @category
  19
+    else
  20
+      render :action => 'new'
  21
+    end
  22
+  end
  23
+  
  24
+  def edit
  25
+    @category = Category.find(params[:id])
  26
+  end
  27
+  
  28
+  def update
  29
+    @category = Category.find(params[:id])
  30
+    if @category.update_attributes(params[:category])
  31
+      flash[:notice] = "Successfully updated category."
  32
+      redirect_to @category
  33
+    else
  34
+      render :action => 'edit'
  35
+    end
  36
+  end
  37
+  
  38
+  def destroy
  39
+    @category = Category.find(params[:id])
  40
+    @category.destroy
  41
+    flash[:notice] = "Successfully destroyed category."
  42
+    redirect_to categories_url
  43
+  end
  44
+end
44  episode-137/store/app/controllers/products_controller.rb
... ...
@@ -0,0 +1,44 @@
  1
+class ProductsController < ApplicationController
  2
+  def index
  3
+    @products = Product.all(:limit => 10)
  4
+  end
  5
+  
  6
+  def show
  7
+    @product = Product.find(params[:id])
  8
+  end
  9
+  
  10
+  def new
  11
+    @product = Product.new
  12
+  end
  13
+  
  14
+  def create
  15
+    @product = Product.new(params[:product])
  16
+    if @product.save
  17
+      flash[:notice] = "Successfully created product."
  18
+      redirect_to @product
  19
+    else
  20
+      render :action => 'new'
  21
+    end
  22
+  end
  23
+  
  24
+  def edit
  25
+    @product = Product.find(params[:id])
  26
+  end
  27
+  
  28
+  def update
  29
+    @product = Product.find(params[:id])
  30
+    if @product.update_attributes(params[:product])
  31
+      flash[:notice] = "Successfully updated product."
  32
+      redirect_to @product
  33
+    else
  34
+      render :action => 'edit'
  35
+    end
  36
+  end
  37
+  
  38
+  def destroy
  39
+    @product = Product.find(params[:id])
  40
+    @product.destroy
  41
+    flash[:notice] = "Successfully destroyed product."
  42
+    redirect_to products_url
  43
+  end
  44
+end
3  episode-137/store/app/helpers/application_helper.rb
... ...
@@ -0,0 +1,3 @@
  1
+# Methods added to this helper will be available to all templates in the application.
  2
+module ApplicationHelper
  3
+end
2  episode-137/store/app/helpers/categories_helper.rb
... ...
@@ -0,0 +1,2 @@
  1
+module CategoriesHelper
  2
+end
23  episode-137/store/app/helpers/layout_helper.rb
... ...
@@ -0,0 +1,23 @@
  1
+# These helper methods can be called in your template to set variables to be used in the layout
  2
+# This module should be included in all views globally,
  3
+# to do so you may need to add this line to your ApplicationController
  4
+#   helper :layout
  5
+module LayoutHelper
  6
+  def title(page_title, show_title = true)
  7
+    @content_for_title = page_title.to_s
  8
+    @show_title = show_title
  9
+  end
  10
+  
  11
+  def show_title?
  12
+    @show_title
  13
+  end
  14
+  
  15
+  def stylesheet(*args)
  16
+    content_for(:head) { stylesheet_link_tag(*args.map(&:to_s)) }
  17
+  end
  18
+  
  19
+  def javascript(*args)
  20
+    args = args.map { |arg| arg == :defaults ? arg : arg.to_s }
  21
+    content_for(:head) { javascript_include_tag(*args) }
  22
+  end
  23
+end
2  episode-137/store/app/helpers/products_helper.rb
... ...
@@ -0,0 +1,2 @@
  1
+module ProductsHelper
  2
+end
3  episode-137/store/app/models/category.rb
... ...
@@ -0,0 +1,3 @@
  1
+class Category < ActiveRecord::Base
  2
+  has_many :products
  3
+end
12  episode-137/store/app/models/product.rb
... ...
@@ -0,0 +1,12 @@
  1
+class Product < ActiveRecord::Base
  2
+  extend ActiveSupport::Memoizable
  3
+  
  4
+  belongs_to :category
  5
+  
  6
+  def filesize(num = 1)
  7
+    # some expensive operation
  8
+    sleep 2
  9
+    12345789 * num
  10
+  end
  11
+  memoize :filesize
  12
+end
7  episode-137/store/app/views/categories/_form.html.erb
... ...
@@ -0,0 +1,7 @@
  1
+<% form_for @category do |f| %>
  2
+  <p>
  3
+    <%= f.label :name %><br />
  4
+    <%= f.text_field :name %>
  5
+  </p>
  6
+  <p><%= f.submit "Submit" %></p>
  7
+<% end %>
8  episode-137/store/app/views/categories/edit.html.erb
... ...
@@ -0,0 +1,8 @@
  1
+<% title "Edit Category" %>
  2
+
  3
+<%= render :partial => 'form' %>
  4
+
  5
+<p>
  6
+  <%= link_to "Show", @category %> |
  7
+  <%= link_to "View All", categories_path %>
  8
+</p>
13  episode-137/store/app/views/categories/index.html.erb
... ...
@@ -0,0 +1,13 @@
  1
+<% title "Categories" %>
  2
+
  3
+<% for category in @categories %>
  4
+  <div class="category">
  5
+    <h3><%= link_to h(category.name), category %></h3>
  6
+    <div class="actions">
  7
+      <%= link_to "Edit", edit_category_path(category) %> |
  8
+      <%= link_to "Destroy", category, :confirm => 'Are you sure?', :method => :delete %>
  9
+    </div>
  10
+  </div>
  11
+<% end %>
  12
+
  13
+<p><%= link_to "New Category", new_category_path %></p>
5  episode-137/store/app/views/categories/new.html.erb
... ...
@@ -0,0 +1,5 @@
  1
+<% title "New Category" %>
  2
+
  3
+<%= render :partial => 'form' %>
  4
+
  5
+<p><%= link_to "Back to List", categories_path %></p>
13  episode-137/store/app/views/categories/show.html.erb
... ...
@@ -0,0 +1,13 @@
  1
+<% title @category.name %>
  2
+
  3
+<ul>
  4
+<% for product in @category.products %>
  5
+  <li><%= link_to h(product.name), product %></li>
  6
+<% end %>
  7
+</ul>
  8
+
  9
+<p>
  10
+  <%= link_to "Edit", edit_category_path(@category) %> |
  11
+  <%= link_to "Destroy", @category, :confirm => 'Are you sure?', :method => :delete %> |
  12
+  <%= link_to "View All", categories_path %>
  13
+</p>
22  episode-137/store/app/views/layouts/application.html.erb
... ...
@@ -0,0 +1,22 @@
  1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2
+  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3
+<html>
  4
+  <head>
  5
+    <title><%= h(yield(:title) || "Untitled") %></title>
  6
+    <%= stylesheet_link_tag 'application' %>
  7
+    <%= yield(:head) %>
  8
+  </head>
  9
+  <body>
  10
+    <div id="container">
  11
+      <%- flash.each do |name, msg| -%>
  12
+        <%= content_tag :div, msg, :id => "flash_#{name}" %>
  13
+      <%- end -%>
  14
+      
  15
+      <%- if show_title? -%>
  16
+        <h1><%=h yield(:title) %></h1>
  17
+      <%- end -%>
  18
+      
  19
+      <%= yield %>
  20
+    </div>
  21
+  </body>
  22
+</html>
19  episode-137/store/app/views/products/_form.html.erb
... ...
@@ -0,0 +1,19 @@
  1
+<% form_for @product do |f| %>
  2
+  <p>
  3
+    <%= f.label :category_id %><br />
  4
+    <%= f.collection_select :category_id, Category.all, :id, :name %>
  5
+  </p>
  6
+  <p>
  7
+    <%= f.label :name %><br />
  8
+    <%= f.text_field :name %>
  9
+  </p>
  10
+  <p>
  11
+    <%= f.label :price %><br />
  12
+    <%= f.text_field :price %>
  13
+  </p>
  14
+  <p>
  15
+    <%= f.label :description %><br />
  16
+    <%= f.text_area :description %>
  17
+  </p>
  18
+  <p><%= f.submit "Submit" %></p>
  19
+<% end %>
8  episode-137/store/app/views/products/edit.html.erb
... ...
@@ -0,0 +1,8 @@
  1
+<% title "Edit Product" %>
  2
+
  3
+<%= render :partial => 'form' %>
  4
+
  5
+<p>
  6
+  <%= link_to "Show", @product %> |
  7
+  <%= link_to "View All", products_path %>
  8
+</p>
16  episode-137/store/app/views/products/index.html.erb
... ...
@@ -0,0 +1,16 @@
  1
+<% title "Products" %>
  2
+
  3
+<% for product in @products %>
  4
+  <div class="product">
  5
+    <h3>
  6
+      <%= link_to h(product.name), product %>
  7
+      <%= number_to_currency(product.price) %>
  8
+    </h3>
  9
+    <div class="actions">
  10
+      <%= link_to "Edit", edit_product_path(product) %> |
  11
+      <%= link_to "Destroy", product, :confirm => 'Are you sure?', :method => :delete %>
  12
+    </div>
  13
+  </div>
  14
+<% end %>
  15
+
  16
+<p><%= link_to "New Product", new_product_path %></p>
5  episode-137/store/app/views/products/new.html.erb
... ...
@@ -0,0 +1,5 @@
  1
+<% title "New Product" %>
  2
+
  3
+<%= render :partial => 'form' %>
  4
+
  5
+<p><%= link_to "Back to List", products_path %></p>
21  episode-137/store/app/views/products/show.html.erb
... ...
@@ -0,0 +1,21 @@
  1
+<% title @product.name %>
  2
+
  3
+<%= simple_format h(@product.description) %>
  4
+
  5
+<p>
  6
+  <strong>Price:</strong>
  7
+  <%= number_to_currency @product.price %>
  8
+</p>
  9
+
  10
+<% unless @product.category.nil? %>
  11
+<p>
  12
+  <strong>Category:</strong>
  13
+  <%= link_to h(@product.category.name), @product.category %>
  14
+</p>
  15
+<% end %>
  16
+
  17
+<p>
  18
+  <%= link_to "Edit", edit_product_path(@product) %> |
  19
+  <%= link_to "Destroy", @product, :confirm => 'Are you sure?', :method => :delete %> |
  20
+  <%= link_to "View All", products_path %>
  21
+</p>
109  episode-137/store/config/boot.rb
... ...
@@ -0,0 +1,109 @@
  1
+# Don't change this file!
  2
+# Configure your app in config/environment.rb and config/environments/*.rb
  3
+
  4
+RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
  5
+
  6
+module Rails
  7
+  class << self
  8
+    def boot!
  9
+      unless booted?
  10
+        preinitialize
  11
+        pick_boot.run
  12
+      end
  13
+    end
  14
+
  15
+    def booted?
  16
+      defined? Rails::Initializer
  17
+    end
  18
+
  19
+    def pick_boot
  20
+      (vendor_rails? ? VendorBoot : GemBoot).new
  21
+    end
  22
+
  23
+    def vendor_rails?
  24
+      File.exist?("#{RAILS_ROOT}/vendor/rails")
  25
+    end
  26
+
  27
+    def preinitialize
  28
+      load(preinitializer_path) if File.exist?(preinitializer_path)
  29
+    end
  30
+
  31
+    def preinitializer_path
  32
+      "#{RAILS_ROOT}/config/preinitializer.rb"
  33
+    end
  34
+  end
  35
+
  36
+  class Boot
  37
+    def run
  38
+      load_initializer
  39
+      Rails::Initializer.run(:set_load_path)
  40
+    end
  41
+  end
  42
+
  43
+  class VendorBoot < Boot
  44
+    def load_initializer
  45
+      require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
  46
+      Rails::Initializer.run(:install_gem_spec_stubs)
  47
+    end
  48
+  end
  49
+
  50
+  class GemBoot < Boot
  51
+    def load_initializer
  52
+      self.class.load_rubygems
  53
+      load_rails_gem
  54
+      require 'initializer'
  55
+    end
  56
+
  57
+    def load_rails_gem
  58
+      if version = self.class.gem_version
  59
+        gem 'rails', version
  60
+      else
  61
+        gem 'rails'
  62
+      end
  63
+    rescue Gem::LoadError => load_error
  64
+      $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
  65
+      exit 1
  66
+    end
  67
+
  68
+    class << self
  69
+      def rubygems_version
  70
+        Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
  71
+      end
  72
+
  73
+      def gem_version
  74
+        if defined? RAILS_GEM_VERSION
  75
+          RAILS_GEM_VERSION
  76
+        elsif ENV.include?('RAILS_GEM_VERSION')
  77
+          ENV['RAILS_GEM_VERSION']
  78
+        else
  79
+          parse_gem_version(read_environment_rb)
  80
+        end
  81
+      end
  82
+
  83
+      def load_rubygems
  84
+        require 'rubygems'
  85
+
  86
+        unless rubygems_version >= '0.9.4'
  87
+          $stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
  88
+          exit 1
  89
+        end
  90
+
  91
+      rescue LoadError
  92
+        $stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
  93
+        exit 1
  94
+      end
  95
+
  96
+      def parse_gem_version(text)
  97
+        $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
  98
+      end
  99
+
  100
+      private
  101
+        def read_environment_rb
  102
+          File.read("#{RAILS_ROOT}/config/environment.rb")
  103
+        end
  104
+    end
  105
+  end
  106
+end
  107
+
  108
+# All that for this:
  109
+Rails.boot!
19  episode-137/store/config/database.yml
... ...
@@ -0,0 +1,19 @@
  1
+# SQLite version 3.x
  2
+#   gem install sqlite3-ruby (not necessary on OS X Leopard)
  3
+development:
  4
+  adapter: sqlite3
  5
+  database: db/development.sqlite3
  6
+  timeout: 5000
  7
+
  8
+# Warning: The database defined as "test" will be erased and
  9
+# re-generated from your development database when you run "rake".
  10
+# Do not set this db to the same as development or production.
  11
+test:
  12
+  adapter: sqlite3
  13
+  database: db/test.sqlite3
  14
+  timeout: 5000
  15
+
  16
+production:
  17
+  adapter: sqlite3
  18
+  database: db/production.sqlite3
  19
+  timeout: 5000
67  episode-137/store/config/environment.rb
... ...
@@ -0,0 +1,67 @@
  1
+# Be sure to restart your server when you modify this file
  2
+
  3
+# Uncomment below to force Rails into production mode when
  4
+# you don't control web/app server and can't set it the proper way
  5
+# ENV['RAILS_ENV'] ||= 'production'
  6
+
  7
+# Specifies gem version of Rails to use when vendor/rails is not present
  8
+RAILS_GEM_VERSION = '>= 2.2.2' unless defined? RAILS_GEM_VERSION
  9
+
  10
+# Bootstrap the Rails environment, frameworks, and default configuration
  11
+require File.join(File.dirname(__FILE__), 'boot')
  12
+
  13
+Rails::Initializer.run do |config|
  14
+  # Settings in config/environments/* take precedence over those specified here.
  15
+  # Application configuration should go into files in config/initializers
  16
+  # -- all .rb files in that directory are automatically loaded.
  17
+  # See Rails::Configuration for more options.
  18
+
  19
+  # Skip frameworks you're not going to use. To use Rails without a database
  20
+  # you must remove the Active Record framework.
  21
+  # config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  22
+
  23
+  # Specify gems that this application depends on. 
  24
+  # They can then be installed with "rake gems:install" on new installations.
  25
+  # config.gem "bj"
  26
+  # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
  27
+  # config.gem "aws-s3", :lib => "aws/s3"
  28
+
  29
+  # Only load the plugins named here, in the order given. By default, all plugins 
  30
+  # in vendor/plugins are loaded in alphabetical order.
  31
+  # :all can be used as a placeholder for all plugins not explicitly named
  32
+  # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
  33
+
  34
+  # Add additional load paths for your own custom dirs
  35
+  # config.load_paths += %W( #{RAILS_ROOT}/extras )
  36
+
  37
+  # Force all environments to use the same logger level
  38
+  # (by default production uses :info, the others :debug)
  39
+  # config.log_level = :debug
  40
+
  41
+  # Make Time.zone default to the specified zone, and make Active Record store time values
  42
+  # in the database in UTC, and return them converted to the specified local zone.
  43
+  # Run "rake -D time" for a list of tasks for finding time zone names. Uncomment to use default local time.
  44
+  config.time_zone = 'UTC'
  45
+
  46
+  # Your secret key for verifying cookie session data integrity.
  47
+  # If you change this key, all old sessions will become invalid!
  48
+  # Make sure the secret is at least 30 characters and all random, 
  49
+  # no regular words or you'll be exposed to dictionary attacks.
  50
+  config.action_controller.session = {
  51
+    :session_key => '_store_session',
  52
+    :secret      => '114555005035015951bcae597e86871cac497fa51daad4955210c9784cdf95ab270f46a6401edbff051ed71c60180e98b8ae095d41cd2dcf1f29684462620381'
  53
+  }
  54
+
  55
+  # Use the database for sessions instead of the cookie-based default,
  56
+  # which shouldn't be used to store highly confidential information
  57
+  # (create the session table with "rake db:sessions:create")
  58
+  # config.action_controller.session_store = :active_record_store
  59
+
  60
+  # Use SQL instead of Active Record's schema dumper when creating the test database.
  61
+  # This is necessary if your schema can't be completely dumped by the schema dumper,
  62
+  # like if you have constraints or database-specific column types
  63
+  # config.active_record.schema_format = :sql
  64
+
  65
+  # Activate observers that should always be running
  66
+  # config.active_record.observers = :cacher, :garbage_collector
  67
+end
17  episode-137/store/config/environments/development.rb
... ...
@@ -0,0 +1,17 @@
  1
+# Settings specified here will take precedence over those in config/environment.rb
  2
+
  3
+# In the development environment your application's code is reloaded on
  4
+# every request.  This slows down response time but is perfect for development
  5
+# since you don't have to restart the webserver when you make code changes.
  6
+config.cache_classes = false
  7
+
  8
+# Log error messages when you accidentally call methods on nil.
  9
+config.whiny_nils = true
  10
+
  11
+# Show full error reports and disable caching
  12
+config.action_controller.consider_all_requests_local = true
  13
+config.action_view.debug_rjs                         = true
  14
+config.action_controller.perform_caching             = false
  15
+
  16
+# Don't care if the mailer can't send
  17
+config.action_mailer.raise_delivery_errors = false
22  episode-137/store/config/environments/production.rb
... ...
@@ -0,0 +1,22 @@
  1
+# Settings specified here will take precedence over those in config/environment.rb
  2
+
  3
+# The production environment is meant for finished, "live" apps.
  4
+# Code is not reloaded between requests
  5
+config.cache_classes = true
  6
+
  7
+# Use a different logger for distributed setups
  8
+# config.logger = SyslogLogger.new
  9
+
  10
+# Full error reports are disabled and caching is turned on
  11
+config.action_controller.consider_all_requests_local = false
  12
+config.action_controller.perform_caching             = true
  13
+config.action_view.cache_template_loading            = true
  14
+
  15
+# Use a different cache store in production
  16
+# config.cache_store = :mem_cache_store
  17
+
  18
+# Enable serving of images, stylesheets, and javascripts from an asset server
  19
+# config.action_controller.asset_host                  = "http://assets.example.com"
  20
+
  21
+# Disable delivery errors, bad email addresses will be ignored
  22
+# config.action_mailer.raise_delivery_errors = false
22  episode-137/store/config/environments/test.rb
... ...
@@ -0,0 +1,22 @@
  1
+# Settings specified here will take precedence over those in config/environment.rb
  2
+
  3
+# The test environment is used exclusively to run your application's
  4
+# test suite.  You never need to work with it otherwise.  Remember that
  5
+# your test database is "scratch space" for the test suite and is wiped
  6
+# and recreated between test runs.  Don't rely on the data there!
  7
+config.cache_classes = true
  8
+
  9
+# Log error messages when you accidentally call methods on nil.
  10
+config.whiny_nils = true
  11
+
  12
+# Show full error reports and disable caching
  13
+config.action_controller.consider_all_requests_local = true
  14
+config.action_controller.perform_caching             = false
  15
+
  16
+# Disable request forgery protection in test environment
  17
+config.action_controller.allow_forgery_protection    = false
  18
+
  19
+# Tell Action Mailer not to deliver emails to the real world.
  20
+# The :test delivery method accumulates sent emails in the
  21
+# ActionMailer::Base.deliveries array.
  22
+config.action_mailer.delivery_method = :test
10  episode-137/store/config/initializers/inflections.rb
... ...
@@ -0,0 +1,10 @@
  1
+# Be sure to restart your server when you modify this file.
  2
+
  3
+# Add new inflection rules using the following format 
  4
+# (all these examples are active by default):
  5
+# Inflector.inflections do |inflect|
  6
+#   inflect.plural /^(ox)$/i, '\1en'
  7
+#   inflect.singular /^(ox)en/i, '\1'
  8
+#   inflect.irregular 'person', 'people'
  9
+#   inflect.uncountable %w( fish sheep )
  10
+# end
5  episode-137/store/config/initializers/mime_types.rb
... ...
@@ -0,0 +1,5 @@
  1
+# Be sure to restart your server when you modify this file.
  2
+
  3
+# Add new mime types for use in respond_to blocks:
  4
+# Mime::Type.register "text/richtext", :rtf
  5
+# Mime::Type.register_alias "text/html", :iphone
15  episode-137/store/config/initializers/new_rails_defaults.rb
... ...
@@ -0,0 +1,15 @@
  1
+# These settings change the behavior of Rails 2 apps and will be defaults
  2
+# for Rails 3. You can remove this initializer when Rails 3 is released.
  3
+
  4
+# Include Active Record class name as root for JSON serialized output.
  5
+ActiveRecord::Base.include_root_in_json = true
  6
+
  7
+# Store the full class name (including module namespace) in STI type column.
  8
+ActiveRecord::Base.store_full_sti_class = true
  9
+
  10
+# Use ISO 8601 format for JSON serialized times and dates.
  11
+ActiveSupport.use_standard_json_time_format = true
  12
+
  13
+# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
  14
+# if you're including raw json in an HTML page.
  15
+ActiveSupport.escape_html_entities_in_json = false
5  episode-137/store/config/routes.rb
... ...
@@ -0,0 +1,5 @@
  1
+ActionController::Routing::Routes.draw do |map|
  2
+  map.resources :products
  3
+  map.resources :categories
  4
+  map.root :products
  5
+end
12  episode-137/store/db/migrate/20080621194238_create_categories.rb
... ...
@@ -0,0 +1,12 @@
  1
+class CreateCategories < ActiveRecord::Migration
  2
+  def self.up
  3
+    create_table :categories do |t|
  4
+      t.string :name
  5
+      t.timestamps
  6
+    end
  7
+  end
  8
+  
  9
+  def self.down
  10
+    drop_table :categories
  11
+  end
  12
+end
15  episode-137/store/db/migrate/20080621194423_create_products.rb
... ...
@@ -0,0 +1,15 @@
  1
+class CreateProducts < ActiveRecord::Migration
  2
+  def self.up
  3
+    create_table :products do |t|
  4
+      t.belongs_to :category
  5
+      t.string :name
  6
+      t.decimal :price
  7
+      t.text :description
  8
+      t.timestamps
  9
+    end
  10
+  end
  11
+  
  12
+  def self.down
  13
+    drop_table :products
  14
+  end
  15
+end
29  episode-137/store/db/schema.rb
... ...
@@ -0,0 +1,29 @@
  1
+# This file is auto-generated from the current state of the database. Instead of editing this file, 
  2
+# please use the migrations feature of Active Record to incrementally modify your database, and
  3
+# then regenerate this schema definition.
  4
+#
  5
+# Note that this schema.rb definition is the authoritative source for your database schema. If you need
  6
+# to create the application database on another system, you should be using db:schema:load, not running
  7
+# all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
  8
+# you'll amass, the slower it'll run and the greater likelihood for issues).
  9
+#
  10
+# It's strongly recommended to check this file into your version control system.
  11
+
  12
+ActiveRecord::Schema.define(:version => 20080621194423) do
  13
+
  14
+  create_table "categories", :force => true do |t|
  15
+    t.string   "name"
  16
+    t.datetime "created_at"
  17
+    t.datetime "updated_at"
  18
+  end
  19
+
  20
+  create_table "products", :force => true do |t|
  21
+    t.integer  "category_id"
  22
+    t.string   "name"
  23
+    t.decimal  "price"
  24
+    t.text     "description"
  25
+    t.datetime "created_at"
  26
+    t.datetime "updated_at"
  27
+  end
  28
+
  29
+end
23  episode-137/store/lib/tasks/application.rake
... ...
@@ -0,0 +1,23 @@
  1
+desc "Setup all for app"
  2
+task :setup => ['db:migrate', 'load:categories', 'load:products']
  3
+
  4
+namespace :load do
  5
+  desc "Load categories into database"
  6
+  task :categories do
  7
+    Category.delete_all
  8
+    ['Electronics', 'Office Supplies', 'Toys', 'Clothing', 'Groceries'].each do |name|
  9
+      Category.create!(:name => name)
  10
+    end
  11
+  end
  12
+  
  13
+  desc "Load products into database"
  14
+  task :products do
  15
+    Product.delete_all
  16
+    categories = Category.all
  17
+    words = File.readlines("/usr/share/dict/words").sort_by { rand }
  18
+    lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  19
+    25.times do
  20
+      Product.create!(:name => words.pop.titleize, :category => categories.rand, :description => lorem, :price => [4.99, 9.99, 14.99, 19.99, 29.99].rand)
  21
+    end
  22
+  end
  23
+end
30  episode-137/store/public/404.html
... ...
@@ -0,0 +1,30 @@
  1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2
+       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3
+
  4
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5
+
  6
+<head>
  7
+  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  8
+  <title>The page you were looking for doesn't exist (404)</title>
  9
+	<style type="text/css">
  10
+		body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
  11
+		div.dialog {
  12
+			width: 25em;
  13
+			padding: 0 4em;
  14
+			margin: 4em auto 0 auto;
  15
+			border: 1px solid #ccc;
  16
+			border-right-color: #999;
  17
+			border-bottom-color: #999;
  18
+		}
  19
+		h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
  20
+	</style>
  21
+</head>
  22
+
  23
+<body>
  24
+  <!-- This file lives in public/404.html -->
  25
+  <div class="dialog">
  26
+    <h1>The page you were looking for doesn't exist.</h1>
  27
+    <p>You may have mistyped the address or the page may have moved.</p>
  28
+  </div>
  29
+</body>
  30
+</html>
30  episode-137/store/public/422.html
... ...
@@ -0,0 +1,30 @@
  1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2
+       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3
+
  4
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5
+
  6
+<head>
  7
+  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  8
+  <title>The change you wanted was rejected (422)</title>
  9
+	<style type="text/css">
  10
+		body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
  11
+		div.dialog {
  12
+			width: 25em;
  13
+			padding: 0 4em;
  14
+			margin: 4em auto 0 auto;
  15
+			border: 1px solid #ccc;
  16
+			border-right-color: #999;
  17
+			border-bottom-color: #999;
  18
+		}
  19
+		h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
  20
+	</style>
  21
+</head>
  22
+
  23
+<body>
  24
+  <!-- This file lives in public/422.html -->
  25
+  <div class="dialog">
  26
+    <h1>The change you wanted was rejected.</h1>
  27
+    <p>Maybe you tried to change something you didn't have access to.</p>
  28
+  </div>
  29
+</body>
  30
+</html>
30  episode-137/store/public/500.html
... ...
@@ -0,0 +1,30 @@
  1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2
+       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3
+
  4
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5
+
  6
+<head>
  7
+  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  8
+  <title>We're sorry, but something went wrong (500)</title>
  9
+	<style type="text/css">
  10
+		body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
  11
+		div.dialog {
  12
+			width: 25em;
  13
+			padding: 0 4em;
  14
+			margin: 4em auto 0 auto;
  15
+			border: 1px solid #ccc;
  16
+			border-right-color: #999;
  17
+			border-bottom-color: #999;
  18
+		}
  19
+		h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
  20
+	</style>
  21
+</head>
  22
+
  23
+<body>
  24
+  <!-- This file lives in public/500.html -->
  25
+  <div class="dialog">
  26
+    <h1>We're sorry, but something went wrong.</h1>
  27
+    <p>We've been notified about this issue and we'll take a look at it shortly.</p>
  28
+  </div>
  29
+</body>
  30
+</html>
10  episode-137/store/public/dispatch.cgi
... ...
@@ -0,0 +1,10 @@
  1
+#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  2
+
  3
+require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
  4
+
  5
+# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
  6
+# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
  7
+require "dispatcher"
  8
+
  9
+ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
  10
+Dispatcher.dispatch
24  episode-137/store/public/dispatch.fcgi
... ...
@@ -0,0 +1,24 @@
  1
+#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  2
+#
  3
+# You may specify the path to the FastCGI crash log (a log of unhandled
  4
+# exceptions which forced the FastCGI instance to exit, great for debugging)
  5
+# and the number of requests to process before running garbage collection.
  6
+#
  7
+# By default, the FastCGI crash log is RAILS_ROOT/log/fastcgi.crash.log
  8
+# and the GC period is nil (turned off).  A reasonable number of requests
  9
+# could range from 10-100 depending on the memory footprint of your app.
  10
+#
  11
+# Example:
  12
+#   # Default log path, normal GC behavior.
  13
+#   RailsFCGIHandler.process!
  14
+#
  15
+#   # Default log path, 50 requests between GC.
  16
+#   RailsFCGIHandler.process! nil, 50
  17
+#
  18
+#   # Custom log path, normal GC behavior.
  19
+#   RailsFCGIHandler.process! '/var/log/myapp_fcgi_crash.log'
  20
+#
  21
+require File.dirname(__FILE__) + "/../config/environment"
  22
+require 'fcgi_handler'
  23
+
  24
+RailsFCGIHandler.process!
10  episode-137/store/public/dispatch.rb
... ...
@@ -0,0 +1,10 @@
  1
+#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  2
+
  3
+require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
  4
+
  5
+# If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
  6
+# "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
  7
+require "dispatcher"
  8
+
  9
+ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)
  10
+Dispatcher.dispatch
0  episode-137/store/public/favicon.ico
No changes.
2  episode-137/store/public/javascripts/application.js
... ...
@@ -0,0 +1,2 @@
  1
+// Place your application-specific JavaScript functions and classes here
  2
+// This file is automatically included by javascript_include_tag :defaults
963  episode-137/store/public/javascripts/controls.js
... ...
@@ -0,0 +1,963 @@
  1
+// Copyright (c) 2005-2008 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
  2
+//           (c) 2005-2007 Ivan Krstic (http://blogs.law.harvard.edu/ivan)
  3
+//           (c) 2005-2007 Jon Tirsen (http://www.tirsen.com)
  4
+// Contributors:
  5
+//  Richard Livsey
  6
+//  Rahul Bhargava
  7
+//  Rob Wills
  8
+// 
  9
+// script.aculo.us is freely distributable under the terms of an MIT-style license.
  10
+// For details, see the script.aculo.us web site: http://script.aculo.us/
  11
+
  12
+// Autocompleter.Base handles all the autocompletion functionality 
  13
+// that's independent of the data source for autocompletion. This
  14
+// includes drawing the autocompletion menu, observing keyboard
  15
+// and mouse events, and similar.
  16
+//
  17
+// Specific autocompleters need to provide, at the very least, 
  18
+// a getUpdatedChoices function that will be invoked every time
  19
+// the text inside the monitored textbox changes. This method 
  20
+// should get the text for which to provide autocompletion by
  21
+// invoking this.getToken(), NOT by directly accessing
  22
+// this.element.value. This is to allow incremental tokenized
  23
+// autocompletion. Specific auto-completion logic (AJAX, etc)
  24
+// belongs in getUpdatedChoices.
  25
+//
  26
+// Tokenized incremental autocompletion is enabled automatically
  27
+// when an autocompleter is instantiated with the 'tokens' option
  28
+// in the options parameter, e.g.:
  29
+// new Ajax.Autocompleter('id','upd', '/url/', { tokens: ',' });
  30
+// will incrementally autocomplete with a comma as the token.
  31
+// Additionally, ',' in the above example can be replaced with
  32
+// a token array, e.g. { tokens: [',', '\n'] } which
  33
+// enables autocompletion on multiple tokens. This is most 
  34
+// useful when one of the tokens is \n (a newline), as it 
  35
+// allows smart autocompletion after linebreaks.
  36
+
  37
+if(typeof Effect == 'undefined')
  38
+  throw("controls.js requires including script.aculo.us' effects.js library");
  39
+
  40
+var Autocompleter = { }
  41
+Autocompleter.Base = Class.create({
  42
+  baseInitialize: function(element, update, options) {
  43
+    element          = $(element)
  44
+    this.element     = element; 
  45
+    this.update      = $(update);  
  46
+    this.hasFocus    = false; 
  47
+    this.changed     = false; 
  48
+    this.active      = false; 
  49
+    this.index       = 0;     
  50
+    this.entryCount  = 0;
  51
+    this.oldElementValue = this.element.value;
  52
+
  53
+    if(this.setOptions)
  54
+      this.setOptions(options);
  55
+    else
  56
+      this.options = options || { };
  57
+
  58
+    this.options.paramName    = this.options.paramName || this.element.name;
  59
+    this.options.tokens       = this.options.tokens || [];
  60
+    this.options.frequency    = this.options.frequency || 0.4;
  61
+    this.options.minChars     = this.options.minChars || 1;
  62
+    this.options.onShow       = this.options.onShow || 
  63
+      function(element, update){ 
  64
+        if(!update.style.position || update.style.position=='absolute') {
  65
+          update.style.position = 'absolute';
  66
+          Position.clone(element, update, {
  67
+            setHeight: false, 
  68
+            offsetTop: element.offsetHeight
  69
+          });
  70
+        }
  71
+        Effect.Appear(update,{duration:0.15});
  72
+      };
  73
+    this.options.onHide = this.options.onHide || 
  74
+      function(element, update){ new Effect.Fade(update,{duration:0.15}) };
  75
+
  76
+    if(typeof(this.options.tokens) == 'string') 
  77
+      this.options.tokens = new Array(this.options.tokens);
  78
+    // Force carriage returns as token delimiters anyway
  79
+    if (!this.options.tokens.include('\n'))
  80
+      this.options.tokens.push('\n');
  81
+
  82
+    this.observer = null;
  83
+    
  84
+    this.element.setAttribute('autocomplete','off');
  85
+
  86
+    Element.hide(this.update);
  87
+
  88
+    Event.observe(this.element, 'blur', this.onBlur.bindAsEventListener(this));
  89
+    Event.observe(this.element, 'keydown', this.onKeyPress.bindAsEventListener(this));
  90
+  },
  91
+
  92
+  show: function() {
  93
+    if(Element.getStyle(this.update, 'display')=='none') this.options.onShow(this.element, this.update);
  94
+    if(!this.iefix && 
  95
+      (Prototype.Browser.IE) &&
  96
+      (Element.getStyle(this.update, 'position')=='absolute')) {
  97
+      new Insertion.After(this.update, 
  98
+       '<iframe id="' + this.update.id + '_iefix" '+
  99
+       'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
  100
+       'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
  101
+      this.iefix = $(this.update.id+'_iefix');
  102
+    }
  103
+    if(this.iefix) setTimeout(this.fixIEOverlapping.bind(this), 50);
  104
+  },
  105
+  
  106
+  fixIEOverlapping: function() {
  107
+    Position.clone(this.update, this.iefix, {setTop:(!this.update.style.height)});
  108
+    this.iefix.style.zIndex = 1;
  109
+    this.update.style.zIndex = 2;
  110
+    Element.show(this.iefix);
  111
+  },
  112
+
  113
+  hide: function() {
  114
+    this.stopIndicator();
  115
+    if(Element.getStyle(this.update, 'display')!='none') this.options.onHide(this.element, this.update);
  116
+    if(this.iefix) Element.hide(this.iefix);
  117
+  },
  118
+
  119
+  startIndicator: function() {
  120
+    if(this.options.indicator) Element.show(this.options.indicator);
  121
+  },
  122
+
  123
+  stopIndicator: function() {
  124
+    if(this.options.indicator) Element.hide(this.options.indicator);
  125
+  },
  126
+
  127
+  onKeyPress: function(event) {
  128
+    if(this.active)
  129
+      switch(event.keyCode) {
  130
+       case Event.KEY_TAB:
  131
+       case Event.KEY_RETURN:
  132
+         this.selectEntry();
  133
+         Event.stop(event);
  134
+       case Event.KEY_ESC:
  135
+         this.hide();
  136
+         this.active = false;
  137
+         Event.stop(event);
  138
+         return;
  139
+       case Event.KEY_LEFT:
  140
+       case Event.KEY_RIGHT:
  141
+         return;
  142