Permalink
Browse files

adding episode 138

  • Loading branch information...
ryanb committed Dec 1, 2008
1 parent 7bfc64a commit 8fb088996adaa9f9a86446104c0dee4d650b6d07
Showing with 8,693 additions and 0 deletions.
  1. +3 −0 episode-138/README
  2. +3 −0 episode-138/store/.gitignore
  3. +10 −0 episode-138/store/Rakefile
  4. +24 −0 episode-138/store/app/controllers/application.rb
  5. +44 −0 episode-138/store/app/controllers/products_controller.rb
  6. +22 −0 episode-138/store/app/controllers/sessions_controller.rb
  7. +16 −0 episode-138/store/app/controllers/users_controller.rb
  8. +3 −0 episode-138/store/app/helpers/application_helper.rb
  9. +23 −0 episode-138/store/app/helpers/layout_helper.rb
  10. +2 −0 episode-138/store/app/helpers/products_helper.rb
  11. +2 −0 episode-138/store/app/helpers/sessions_helper.rb
  12. +2 −0 episode-138/store/app/helpers/users_helper.rb
  13. +2 −0 episode-138/store/app/models/product.rb
  14. +36 −0 episode-138/store/app/models/user.rb
  15. +22 −0 episode-138/store/app/views/layouts/application.html.erb
  16. +17 −0 episode-138/store/app/views/products/_form.html.erb
  17. +8 −0 episode-138/store/app/views/products/edit.html.erb
  18. +15 −0 episode-138/store/app/views/products/index.html.erb
  19. +5 −0 episode-138/store/app/views/products/new.html.erb
  20. +20 −0 episode-138/store/app/views/products/show.html.erb
  21. +15 −0 episode-138/store/app/views/sessions/new.html.erb
  22. +28 −0 episode-138/store/app/views/users/new.html.erb
  23. +109 −0 episode-138/store/config/boot.rb
  24. +22 −0 episode-138/store/config/database.yml
  25. +75 −0 episode-138/store/config/environment.rb
  26. +17 −0 episode-138/store/config/environments/development.rb
  27. +24 −0 episode-138/store/config/environments/production.rb
  28. +22 −0 episode-138/store/config/environments/test.rb
  29. +10 −0 episode-138/store/config/initializers/inflections.rb
  30. +5 −0 episode-138/store/config/initializers/mime_types.rb
  31. +17 −0 episode-138/store/config/initializers/new_rails_defaults.rb
  32. +12 −0 episode-138/store/config/locales/en.yml
  33. +19 −0 episode-138/store/config/locales/wk.yml
  34. +11 −0 episode-138/store/config/routes.rb
  35. +14 −0 episode-138/store/db/migrate/20081128162545_create_products.rb
  36. +15 −0 episode-138/store/db/migrate/20081128162724_create_users.rb
  37. +9 −0 episode-138/store/db/migrate/20081128162810_add_language_to_users.rb
  38. +32 −0 episode-138/store/db/schema.rb
  39. +5 −0 episode-138/store/doc/README_FOR_APP
  40. +36 −0 episode-138/store/lib/authentication.rb
  41. +30 −0 episode-138/store/public/404.html
  42. +30 −0 episode-138/store/public/422.html
  43. +33 −0 episode-138/store/public/500.html
  44. +10 −0 episode-138/store/public/dispatch.cgi
  45. +24 −0 episode-138/store/public/dispatch.fcgi
  46. +10 −0 episode-138/store/public/dispatch.rb
  47. 0 episode-138/store/public/favicon.ico
  48. +2 −0 episode-138/store/public/javascripts/application.js
  49. +963 −0 episode-138/store/public/javascripts/controls.js
  50. +973 −0 episode-138/store/public/javascripts/dragdrop.js
  51. +1,128 −0 episode-138/store/public/javascripts/effects.js
  52. +4,320 −0 episode-138/store/public/javascripts/prototype.js
  53. +5 −0 episode-138/store/public/robots.txt
  54. +88 −0 episode-138/store/public/stylesheets/application.css
  55. +4 −0 episode-138/store/script/about
  56. +3 −0 episode-138/store/script/console
  57. +3 −0 episode-138/store/script/dbconsole
  58. +3 −0 episode-138/store/script/destroy
  59. +3 −0 episode-138/store/script/generate
  60. +3 −0 episode-138/store/script/performance/benchmarker
  61. +3 −0 episode-138/store/script/performance/profiler
  62. +3 −0 episode-138/store/script/performance/request
  63. +3 −0 episode-138/store/script/plugin
  64. +3 −0 episode-138/store/script/process/inspector
  65. +3 −0 episode-138/store/script/process/reaper
  66. +3 −0 episode-138/store/script/process/spawner
  67. +3 −0 episode-138/store/script/runner
  68. +3 −0 episode-138/store/script/server
  69. +9 −0 episode-138/store/test/fixtures/products.yml
  70. +12 −0 episode-138/store/test/fixtures/users.yml
  71. +54 −0 episode-138/store/test/functional/products_controller_test.rb
  72. +22 −0 episode-138/store/test/functional/sessions_controller_test.rb
  73. +21 −0 episode-138/store/test/functional/users_controller_test.rb
  74. +9 −0 episode-138/store/test/performance/browsing_test.rb
  75. +38 −0 episode-138/store/test/test_helper.rb
  76. +7 −0 episode-138/store/test/unit/product_test.rb
  77. +86 −0 episode-138/store/test/unit/user_test.rb
View
@@ -0,0 +1,3 @@
+Railscasts Episode #138: I18n
+
+http://railscasts.com/episodes/138
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
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,24 @@
+# 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
+ include Authentication
+ 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 => '7ba3655f1b862ec027b1d6fa7360520f'
+
+ # 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
+
+ before_filter :set_user_language
+
+ private
+
+ def set_user_language
+ I18n.locale = current_user.language if logged_in?
+ end
+end
@@ -0,0 +1,44 @@
+class ProductsController < ApplicationController
+ def index
+ @products = Product.find(:all)
+ 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,22 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.authenticate(params[:login], params[:password])
+ if user
+ session[:user_id] = user.id
+ flash[:notice] = t('flash.login')
+ redirect_to root_url
+ else
+ flash.now[:error] = "Invalid login or password."
+ render :action => 'new'
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ flash[:notice] = "You have been logged out."
+ redirect_to root_url
+ end
+end
@@ -0,0 +1,16 @@
+class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ session[:user_id] = @user.id
+ flash[:notice] = "Thank you for signing up! You are now logged in."
+ redirect_to root_url
+ else
+ render :action => 'new'
+ end
+ 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,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 ProductsHelper
+end
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+end
@@ -0,0 +1,2 @@
+class Product < ActiveRecord::Base
+end
@@ -0,0 +1,36 @@
+class User < ActiveRecord::Base
+ # new columns need to be added here to be writable through mass assignment
+ attr_accessible :username, :email, :password, :password_confirmation, :language
+
+ attr_accessor :password
+ before_create :prepare_password
+
+ validates_presence_of :username
+ validates_uniqueness_of :username, :email, :allow_blank => true
+ validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
+ validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
+ validates_presence_of :password, :on => :create
+ validates_confirmation_of :password
+ validates_length_of :password, :minimum => 4, :allow_blank => true
+
+ # login can be either username or email address
+ def self.authenticate(login, pass)
+ user = find_by_username(login) || find_by_email(login)
+ return user if user && user.matching_password?(pass)
+ end
+
+ def matching_password?(pass)
+ self.password_hash == encrypt_password(pass)
+ end
+
+ private
+
+ def prepare_password
+ self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
+ self.password_hash = encrypt_password(password)
+ end
+
+ def encrypt_password(pass)
+ Digest::SHA1.hexdigest([pass, password_salt].join)
+ end
+end
@@ -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,17 @@
+<% form_for @product do |f| %>
+ <%= f.error_messages %>
+
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_area :description %>
+ </p>
+ <p>
+ <%= f.label :price %><br />
+ <%= f.text_field :price %>
+ </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,15 @@
+<% title t('welcome.title') %>
+
+<p><%= t 'welcome.paragraph' %></p>
+
+<h2><%= t 'products.title' %></h2>
+
+<% for product in @products %>
+ <h3>
+ <%= link_to h(product.name), product %>
+ <%= number_to_currency(product.price) %>
+ </h3>
+ <div class="date_released">
+ <em><%= t 'products.released' %>: <%= product.created_at.strftime("%m/%d/%Y") %></em>
+ </div>
+<% end %>
@@ -0,0 +1,5 @@
+<% title "New Product" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", products_path %></p>
@@ -0,0 +1,20 @@
+<% title "Product" %>
+
+<p>
+ <strong>Name:</strong>
+ <%=h @product.name %>
+</p>
+<p>
+ <strong>Description:</strong>
+ <%=h @product.description %>
+</p>
+<p>
+ <strong>Price:</strong>
+ <%=h @product.price %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_product_path(@product) %> |
+ <%= link_to "Destroy", @product, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", products_path %>
+</p>
@@ -0,0 +1,15 @@
+<% title "Log in" %>
+
+<p>Don't have an account? <%= link_to "Sign up!", signup_path %></p>
+
+<% form_tag sessions_path do %>
+ <p>
+ <%= label_tag :login, "Username or Email Address" %><br />
+ <%= text_field_tag :login, params[:login] %>
+ </p>
+ <p>
+ <%= label_tag :password %><br />
+ <%= password_field_tag :password %>
+ </p>
+ <p><%= submit_tag "Log in" %></p>
+<% end %>
@@ -0,0 +1,28 @@
+<% title "Sign up" %>
+
+<p>Already have an account? <%= link_to "Log in", login_path %>.</p>
+
+<% form_for @user do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :username %><br />
+ <%= f.text_field :username %>
+ </p>
+ <p>
+ <%= f.label :email, "Email Address" %><br />
+ <%= f.text_field :email %>
+ </p>
+ <p>
+ <%= f.label :password %><br />
+ <%= f.password_field :password %>
+ </p>
+ <p>
+ <%= f.label :password_confirmation, "Confirm Password" %><br />
+ <%= f.password_field :password_confirmation %>
+ </p>
+ <p>
+ <%= f.label :language, "Language" %><br />
+ <%= f.select :language, [['English', 'en'], ['Wookieespeak', 'wk']] %>
+ </p>
+ <p><%= f.submit "Sign up" %></p>
+<% end %>
Oops, something went wrong.

0 comments on commit 8fb0889

Please sign in to comment.