Permalink
Browse files

adding episode 156

  • Loading branch information...
1 parent 87381b9 commit bfef33dc0fd0bb15dec4edd9ff8f82227fec8f9a @ryanb committed Apr 6, 2009
Showing with 8,464 additions and 0 deletions.
  1. +11 −0 episode-156/README
  2. +3 −0 episode-156/authenticator/.gitignore
  3. +1 −0 episode-156/authenticator/README
  4. +10 −0 episode-156/authenticator/Rakefile
  5. +11 −0 episode-156/authenticator/app/controllers/application_controller.rb
  6. +5 −0 episode-156/authenticator/app/controllers/home_controller.rb
  7. +22 −0 episode-156/authenticator/app/controllers/sessions_controller.rb
  8. +16 −0 episode-156/authenticator/app/controllers/users_controller.rb
  9. +3 −0 episode-156/authenticator/app/helpers/application_helper.rb
  10. +2 −0 episode-156/authenticator/app/helpers/home_helper.rb
  11. +23 −0 episode-156/authenticator/app/helpers/layout_helper.rb
  12. +2 −0 episode-156/authenticator/app/helpers/sessions_helper.rb
  13. +2 −0 episode-156/authenticator/app/helpers/users_helper.rb
  14. +36 −0 episode-156/authenticator/app/models/user.rb
  15. +3 −0 episode-156/authenticator/app/views/home/index.html.erb
  16. +22 −0 episode-156/authenticator/app/views/layouts/application.html.erb
  17. +15 −0 episode-156/authenticator/app/views/sessions/new.html.erb
  18. +24 −0 episode-156/authenticator/app/views/users/new.html.erb
  19. +110 −0 episode-156/authenticator/config/boot.rb
  20. +22 −0 episode-156/authenticator/config/database.yml
  21. +41 −0 episode-156/authenticator/config/environment.rb
  22. +17 −0 episode-156/authenticator/config/environments/development.rb
  23. +28 −0 episode-156/authenticator/config/environments/production.rb
  24. +30 −0 episode-156/authenticator/config/environments/test.rb
  25. +7 −0 episode-156/authenticator/config/initializers/backtrace_silencers.rb
  26. +10 −0 episode-156/authenticator/config/initializers/inflections.rb
  27. +5 −0 episode-156/authenticator/config/initializers/mime_types.rb
  28. +19 −0 episode-156/authenticator/config/initializers/new_rails_defaults.rb
  29. +15 −0 episode-156/authenticator/config/initializers/session_store.rb
  30. +5 −0 episode-156/authenticator/config/locales/en.yml
  31. +50 −0 episode-156/authenticator/config/routes.rb
  32. +15 −0 episode-156/authenticator/db/migrate/20090406065216_create_users.rb
  33. +23 −0 episode-156/authenticator/db/schema.rb
  34. +2 −0 episode-156/authenticator/doc/README_FOR_APP
  35. +36 −0 episode-156/authenticator/lib/authentication.rb
  36. +30 −0 episode-156/authenticator/public/404.html
  37. +30 −0 episode-156/authenticator/public/422.html
  38. +30 −0 episode-156/authenticator/public/500.html
  39. 0 episode-156/authenticator/public/favicon.ico
  40. BIN episode-156/authenticator/public/images/rails.png
  41. +2 −0 episode-156/authenticator/public/javascripts/application.js
  42. +963 −0 episode-156/authenticator/public/javascripts/controls.js
  43. +973 −0 episode-156/authenticator/public/javascripts/dragdrop.js
  44. +1,128 −0 episode-156/authenticator/public/javascripts/effects.js
  45. +4,320 −0 episode-156/authenticator/public/javascripts/prototype.js
  46. +5 −0 episode-156/authenticator/public/robots.txt
  47. +81 −0 episode-156/authenticator/public/stylesheets/application.css
  48. +4 −0 episode-156/authenticator/script/about
  49. +3 −0 episode-156/authenticator/script/console
  50. +3 −0 episode-156/authenticator/script/dbconsole
  51. +3 −0 episode-156/authenticator/script/destroy
  52. +3 −0 episode-156/authenticator/script/generate
  53. +3 −0 episode-156/authenticator/script/performance/benchmarker
  54. +3 −0 episode-156/authenticator/script/performance/profiler
  55. +3 −0 episode-156/authenticator/script/plugin
  56. +3 −0 episode-156/authenticator/script/runner
  57. +3 −0 episode-156/authenticator/script/server
  58. +12 −0 episode-156/authenticator/test/fixtures/users.yml
  59. +8 −0 episode-156/authenticator/test/functional/home_controller_test.rb
  60. +22 −0 episode-156/authenticator/test/functional/sessions_controller_test.rb
  61. +21 −0 episode-156/authenticator/test/functional/users_controller_test.rb
  62. +21 −0 episode-156/authenticator/test/integration/authentication_test.rb
  63. +9 −0 episode-156/authenticator/test/performance/browsing_test.rb
  64. +42 −0 episode-156/authenticator/test/test_helper.rb
  65. +4 −0 episode-156/authenticator/test/unit/helpers/home_helper_test.rb
  66. +86 −0 episode-156/authenticator/test/unit/user_test.rb
View
@@ -0,0 +1,11 @@
+Railscasts Episode #156: Webrat
+
+http://railscasts.com/episodes/156
+
+Commands
+
+ sudo rake gems:install RAILS_ENV=test
+ script/generate integration_test authentication
+ rake test:integration
+ script/generate nifty_authentication
+ rake db:migrate
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
@@ -0,0 +1 @@
+Example application for Railscasts.
@@ -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,11 @@
+# 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
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
+
+ # Scrub sensitive parameters from your log
+ # filter_parameter_logging :password
+end
@@ -0,0 +1,5 @@
+class HomeController < ApplicationController
+ def index
+ 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] = "Logged in successfully."
+ 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,2 @@
+module HomeHelper
+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 SessionsHelper
+end
@@ -0,0 +1,2 @@
+module UsersHelper
+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
+
+ 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,3 @@
+<% title "Home" %>
+
+<p>This is the home page.</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,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,24 @@
+<% 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.submit "Sign up" %></p>
+<% end %>
@@ -0,0 +1,110 @@
+# Don't change this file!
+# Configure your app in config/environment.rb and config/environments/*.rb
+
+RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
+
+module Rails
+ class << self
+ def boot!
+ unless booted?
+ preinitialize
+ pick_boot.run
+ end
+ end
+
+ def booted?
+ defined? Rails::Initializer
+ end
+
+ def pick_boot
+ (vendor_rails? ? VendorBoot : GemBoot).new
+ end
+
+ def vendor_rails?
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
+ end
+
+ def preinitialize
+ load(preinitializer_path) if File.exist?(preinitializer_path)
+ end
+
+ def preinitializer_path
+ "#{RAILS_ROOT}/config/preinitializer.rb"
+ end
+ end
+
+ class Boot
+ def run
+ load_initializer
+ Rails::Initializer.run(:set_load_path)
+ end
+ end
+
+ class VendorBoot < Boot
+ def load_initializer
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
+ Rails::Initializer.run(:install_gem_spec_stubs)
+ Rails::GemDependency.add_frozen_gem_path
+ end
+ end
+
+ class GemBoot < Boot
+ def load_initializer
+ self.class.load_rubygems
+ load_rails_gem
+ require 'initializer'
+ end
+
+ def load_rails_gem
+ if version = self.class.gem_version
+ gem 'rails', version
+ else
+ gem 'rails'
+ end
+ rescue Gem::LoadError => load_error
+ $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.)
+ exit 1
+ end
+
+ class << self
+ def rubygems_version
+ Gem::RubyGemsVersion rescue nil
+ end
+
+ def gem_version
+ if defined? RAILS_GEM_VERSION
+ RAILS_GEM_VERSION
+ elsif ENV.include?('RAILS_GEM_VERSION')
+ ENV['RAILS_GEM_VERSION']
+ else
+ parse_gem_version(read_environment_rb)
+ end
+ end
+
+ def load_rubygems
+ require 'rubygems'
+ min_version = '1.3.1'
+ unless rubygems_version >= min_version
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ exit 1
+ end
+
+ rescue LoadError
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
+ exit 1
+ end
+
+ def parse_gem_version(text)
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
+ end
+
+ private
+ def read_environment_rb
+ File.read("#{RAILS_ROOT}/config/environment.rb")
+ end
+ end
+ end
+end
+
+# All that for this:
+Rails.boot!
@@ -0,0 +1,22 @@
+# SQLite version 3.x
+# gem install sqlite3-ruby (not necessary on OS X Leopard)
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+
+# Warning: The database defined as "test" will be erased and
+# re-generated from your development database when you run "rake".
+# Do not set this db to the same as development or production.
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+ pool: 5
+ timeout: 5000
+
+production:
+ adapter: sqlite3
+ database: db/production.sqlite3
+ pool: 5
+ timeout: 5000
Oops, something went wrong.

0 comments on commit bfef33d

Please sign in to comment.