Permalink
Browse files

adding episode 263

  • Loading branch information...
1 parent 42335b6 commit e7e3a533001d1ac0ba29b29ba70e25280b2d0669 @ryanb committed Apr 25, 2011
Showing with 19,921 additions and 1 deletion.
  1. +1 −1 episode-262/README
  2. +8 −0 episode-263/README
  3. +4 −0 episode-263/validator-after/.gitignore
  4. +9 −0 episode-263/validator-after/Gemfile
  5. +84 −0 episode-263/validator-after/Gemfile.lock
  6. +7 −0 episode-263/validator-after/README.rdoc
  7. +7 −0 episode-263/validator-after/Rakefile
  8. +4 −0 episode-263/validator-after/app/controllers/application_controller.rb
  9. +20 −0 episode-263/validator-after/app/controllers/sessions_controller.rb
  10. +30 −0 episode-263/validator-after/app/controllers/users_controller.rb
  11. +2 −0 episode-263/validator-after/app/helpers/application_helper.rb
  12. +23 −0 episode-263/validator-after/app/helpers/error_messages_helper.rb
  13. +22 −0 episode-263/validator-after/app/helpers/layout_helper.rb
  14. +2 −0 episode-263/validator-after/app/helpers/sessions_helper.rb
  15. +2 −0 episode-263/validator-after/app/helpers/users_helper.rb
  16. +34 −0 episode-263/validator-after/app/models/user.rb
  17. +19 −0 episode-263/validator-after/app/views/layouts/application.html.erb
  18. +15 −0 episode-263/validator-after/app/views/sessions/new.html.erb
  19. +20 −0 episode-263/validator-after/app/views/users/_form.html.erb
  20. +3 −0 episode-263/validator-after/app/views/users/edit.html.erb
  21. +5 −0 episode-263/validator-after/app/views/users/new.html.erb
  22. +4 −0 episode-263/validator-after/config.ru
  23. +44 −0 episode-263/validator-after/config/application.rb
  24. +6 −0 episode-263/validator-after/config/boot.rb
  25. +22 −0 episode-263/validator-after/config/database.yml
  26. +5 −0 episode-263/validator-after/config/environment.rb
  27. +26 −0 episode-263/validator-after/config/environments/development.rb
  28. +49 −0 episode-263/validator-after/config/environments/production.rb
  29. +35 −0 episode-263/validator-after/config/environments/test.rb
  30. +7 −0 episode-263/validator-after/config/initializers/backtrace_silencers.rb
  31. +14 −0 episode-263/validator-after/config/initializers/client_side_validations.rb
  32. +10 −0 episode-263/validator-after/config/initializers/inflections.rb
  33. +5 −0 episode-263/validator-after/config/initializers/mime_types.rb
  34. +7 −0 episode-263/validator-after/config/initializers/secret_token.rb
  35. +8 −0 episode-263/validator-after/config/initializers/session_store.rb
  36. +7 −0 episode-263/validator-after/config/locales/en.yml
  37. +66 −0 episode-263/validator-after/config/routes.rb
  38. +15 −0 episode-263/validator-after/db/migrate/20110424190739_create_users.rb
  39. +24 −0 episode-263/validator-after/db/schema.rb
  40. +7 −0 episode-263/validator-after/db/seeds.rb
  41. +2 −0 episode-263/validator-after/doc/README_FOR_APP
  42. +48 −0 episode-263/validator-after/lib/controller_authentication.rb
  43. +7 −0 episode-263/validator-after/lib/email_format_validator.rb
  44. 0 episode-263/validator-after/lib/tasks/.gitkeep
  45. +26 −0 episode-263/validator-after/public/404.html
  46. +26 −0 episode-263/validator-after/public/422.html
  47. +26 −0 episode-263/validator-after/public/500.html
  48. 0 episode-263/validator-after/public/favicon.ico
  49. BIN episode-263/validator-after/public/images/rails.png
  50. +2 −0 episode-263/validator-after/public/javascripts/application.js
  51. +8,374 −0 episode-263/validator-after/public/javascripts/jquery.js
  52. +16 −0 episode-263/validator-after/public/javascripts/jquery.min.js
  53. +291 −0 episode-263/validator-after/public/javascripts/rails.js
  54. +5 −0 episode-263/validator-after/public/javascripts/rails.validations.custom.js
  55. +390 −0 episode-263/validator-after/public/javascripts/rails.validations.js
  56. +5 −0 episode-263/validator-after/public/robots.txt
  57. 0 episode-263/validator-after/public/stylesheets/.gitkeep
  58. +77 −0 episode-263/validator-after/public/stylesheets/application.css
  59. +6 −0 episode-263/validator-after/script/rails
  60. +12 −0 episode-263/validator-after/test/fixtures/users.yml
  61. +22 −0 episode-263/validator-after/test/functional/sessions_controller_test.rb
  62. +51 −0 episode-263/validator-after/test/functional/users_controller_test.rb
  63. +9 −0 episode-263/validator-after/test/performance/browsing_test.rb
  64. +13 −0 episode-263/validator-after/test/test_helper.rb
  65. +86 −0 episode-263/validator-after/test/unit/user_test.rb
  66. 0 episode-263/validator-after/vendor/plugins/.gitkeep
  67. +4 −0 episode-263/validator-before/.gitignore
  68. +8 −0 episode-263/validator-before/Gemfile
  69. +81 −0 episode-263/validator-before/Gemfile.lock
  70. +7 −0 episode-263/validator-before/README.rdoc
  71. +7 −0 episode-263/validator-before/Rakefile
  72. +4 −0 episode-263/validator-before/app/controllers/application_controller.rb
  73. +20 −0 episode-263/validator-before/app/controllers/sessions_controller.rb
  74. +30 −0 episode-263/validator-before/app/controllers/users_controller.rb
  75. +2 −0 episode-263/validator-before/app/helpers/application_helper.rb
  76. +23 −0 episode-263/validator-before/app/helpers/error_messages_helper.rb
  77. +22 −0 episode-263/validator-before/app/helpers/layout_helper.rb
  78. +2 −0 episode-263/validator-before/app/helpers/sessions_helper.rb
  79. +2 −0 episode-263/validator-before/app/helpers/users_helper.rb
  80. +32 −0 episode-263/validator-before/app/models/user.rb
  81. +19 −0 episode-263/validator-before/app/views/layouts/application.html.erb
  82. +15 −0 episode-263/validator-before/app/views/sessions/new.html.erb
  83. +20 −0 episode-263/validator-before/app/views/users/_form.html.erb
  84. +3 −0 episode-263/validator-before/app/views/users/edit.html.erb
  85. +5 −0 episode-263/validator-before/app/views/users/new.html.erb
  86. +4 −0 episode-263/validator-before/config.ru
  87. +44 −0 episode-263/validator-before/config/application.rb
  88. +6 −0 episode-263/validator-before/config/boot.rb
  89. +22 −0 episode-263/validator-before/config/database.yml
  90. +5 −0 episode-263/validator-before/config/environment.rb
  91. +26 −0 episode-263/validator-before/config/environments/development.rb
  92. +49 −0 episode-263/validator-before/config/environments/production.rb
  93. +35 −0 episode-263/validator-before/config/environments/test.rb
  94. +7 −0 episode-263/validator-before/config/initializers/backtrace_silencers.rb
  95. +10 −0 episode-263/validator-before/config/initializers/inflections.rb
  96. +5 −0 episode-263/validator-before/config/initializers/mime_types.rb
  97. +7 −0 episode-263/validator-before/config/initializers/secret_token.rb
  98. +8 −0 episode-263/validator-before/config/initializers/session_store.rb
  99. +7 −0 episode-263/validator-before/config/locales/en.yml
  100. +66 −0 episode-263/validator-before/config/routes.rb
  101. +15 −0 episode-263/validator-before/db/migrate/20110424190739_create_users.rb
  102. +24 −0 episode-263/validator-before/db/schema.rb
  103. +7 −0 episode-263/validator-before/db/seeds.rb
  104. +2 −0 episode-263/validator-before/doc/README_FOR_APP
  105. +48 −0 episode-263/validator-before/lib/controller_authentication.rb
  106. 0 episode-263/validator-before/lib/tasks/.gitkeep
  107. +26 −0 episode-263/validator-before/public/404.html
  108. +26 −0 episode-263/validator-before/public/422.html
  109. +26 −0 episode-263/validator-before/public/500.html
  110. 0 episode-263/validator-before/public/favicon.ico
  111. BIN episode-263/validator-before/public/images/rails.png
  112. +2 −0 episode-263/validator-before/public/javascripts/application.js
  113. +8,374 −0 episode-263/validator-before/public/javascripts/jquery.js
  114. +16 −0 episode-263/validator-before/public/javascripts/jquery.min.js
  115. +291 −0 episode-263/validator-before/public/javascripts/rails.js
  116. +5 −0 episode-263/validator-before/public/robots.txt
  117. 0 episode-263/validator-before/public/stylesheets/.gitkeep
  118. +77 −0 episode-263/validator-before/public/stylesheets/application.css
  119. +6 −0 episode-263/validator-before/script/rails
  120. +12 −0 episode-263/validator-before/test/fixtures/users.yml
  121. +22 −0 episode-263/validator-before/test/functional/sessions_controller_test.rb
  122. +51 −0 episode-263/validator-before/test/functional/users_controller_test.rb
  123. +9 −0 episode-263/validator-before/test/performance/browsing_test.rb
  124. +13 −0 episode-263/validator-before/test/test_helper.rb
  125. +86 −0 episode-263/validator-before/test/unit/user_test.rb
  126. 0 episode-263/validator-before/vendor/plugins/.gitkeep
View
@@ -1,4 +1,4 @@
-Railscasts Episode #261: Trees with Ancestry
+Railscasts Episode #262: Trees with Ancestry
http://railscasts.com/episodes/262
View
@@ -0,0 +1,8 @@
+Railscasts Episode #263: Client Side Validations
+
+http://railscasts.com/episodes/263
+
+Commands used in episode
+
+ bundle
+ rails g client_side_validations:install
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/
@@ -0,0 +1,9 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.7'
+gem 'sqlite3'
+gem 'nifty-generators'
+gem 'bcrypt-ruby', :require => 'bcrypt'
+
+gem 'jquery-rails'
+gem 'client_side_validations'
@@ -0,0 +1,84 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.7)
+ actionpack (= 3.0.7)
+ mail (~> 2.2.15)
+ actionpack (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.5.0)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.14)
+ rack-test (~> 0.5.7)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.7)
+ activesupport (= 3.0.7)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.7)
+ activemodel (= 3.0.7)
+ activesupport (= 3.0.7)
+ activesupport (3.0.7)
+ arel (2.0.9)
+ bcrypt-ruby (2.1.4)
+ builder (2.1.2)
+ client_side_validations (3.0.4)
+ activesupport (~> 3.0.0)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.5.0)
+ jquery-rails (0.2.7)
+ rails (~> 3.0)
+ thor (~> 0.14.4)
+ mail (2.2.17)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ nifty-generators (0.4.6)
+ polyglot (0.3.1)
+ rack (1.2.2)
+ rack-mount (0.6.14)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ rails (3.0.7)
+ actionmailer (= 3.0.7)
+ actionpack (= 3.0.7)
+ activerecord (= 3.0.7)
+ activeresource (= 3.0.7)
+ activesupport (= 3.0.7)
+ bundler (~> 1.0)
+ railties (= 3.0.7)
+ railties (3.0.7)
+ actionpack (= 3.0.7)
+ activesupport (= 3.0.7)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ sqlite3 (1.3.3)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.26)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bcrypt-ruby
+ client_side_validations
+ jquery-rails
+ nifty-generators
+ rails (= 3.0.7)
+ sqlite3
@@ -0,0 +1,7 @@
+= Railscasts Example Application
+
+Run these commands to try it out.
+
+ bundle
+ rake db:migrate
+ rails s
@@ -0,0 +1,7 @@
+# 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.expand_path('../config/application', __FILE__)
+require 'rake'
+
+Validator::Application.load_tasks
@@ -0,0 +1,4 @@
+class ApplicationController < ActionController::Base
+ include ControllerAuthentication
+ protect_from_forgery
+end
@@ -0,0 +1,20 @@
+class SessionsController < ApplicationController
+ def new
+ end
+
+ def create
+ user = User.authenticate(params[:login], params[:password])
+ if user
+ session[:user_id] = user.id
+ redirect_to_target_or_default root_url, :notice => "Logged in successfully."
+ else
+ flash.now[:alert] = "Invalid login or password."
+ render :action => 'new'
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ redirect_to root_url, :notice => "You have been logged out."
+ end
+end
@@ -0,0 +1,30 @@
+class UsersController < ApplicationController
+ before_filter :login_required, :except => [:new, :create]
+
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ session[:user_id] = @user.id
+ redirect_to root_url, :notice => "Thank you for signing up! You are now logged in."
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @user = current_user
+ end
+
+ def update
+ @user = current_user
+ if @user.update_attributes(params[:user])
+ redirect_to root_url, :notice => "Your profile has been updated."
+ else
+ render :action => 'edit'
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module ApplicationHelper
+end
@@ -0,0 +1,23 @@
+module ErrorMessagesHelper
+ # Render error messages for the given objects. The :message and :header_message options are allowed.
+ def error_messages_for(*objects)
+ options = objects.extract_options!
+ options[:header_message] ||= I18n.t(:"activerecord.errors.header", :default => "Invalid Fields")
+ options[:message] ||= I18n.t(:"activerecord.errors.message", :default => "Correct the following errors and try again.")
+ messages = objects.compact.map { |o| o.errors.full_messages }.flatten
+ unless messages.empty?
+ content_tag(:div, :class => "error_messages") do
+ list_items = messages.map { |msg| content_tag(:li, msg) }
+ content_tag(:h2, options[:header_message]) + content_tag(:p, options[:message]) + content_tag(:ul, list_items.join.html_safe)
+ end
+ end
+ end
+
+ module FormBuilderAdditions
+ def error_messages(options = {})
+ @template.error_messages_for(@object, options)
+ end
+ end
+end
+
+ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)
@@ -0,0 +1,22 @@
+# 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) { h(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) }
+ end
+
+ def javascript(*args)
+ 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,34 @@
+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_save :prepare_password
+
+ validates_presence_of :username, :email
+ validates_presence_of :password, :on => :create
+ validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
+ validates_length_of :password, :minimum => 4, :allow_blank => true
+ validates_confirmation_of :password
+ validates_uniqueness_of :username, :email
+ validates :email, :email_format => 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.password_hash == user.encrypt_password(pass)
+ end
+
+ def encrypt_password(pass)
+ BCrypt::Engine.hash_secret(pass, password_salt)
+ end
+
+ private
+
+ def prepare_password
+ unless password.blank?
+ self.password_salt = BCrypt::Engine.generate_salt
+ self.password_hash = encrypt_password(password)
+ end
+ end
+end
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title><%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
+ <%= stylesheet_link_tag "application" %>
+ <%= javascript_include_tag :defaults, "rails.validations", "rails.validations.custom" %>
+ <%= csrf_meta_tag %>
+ <%= yield(:head) %>
+ </head>
+ <body>
+ <div id="container">
+ <% flash.each do |name, msg| %>
+ <%= content_tag :div, msg, :id => "flash_#{name}" %>
+ <% end %>
+ <%= content_tag :h1, yield(:title) if show_title? %>
+ <%= 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,20 @@
+<%= form_for @user, :validate => true 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 (@user.new_record? ? "Sign up" : "Update") %></p>
+<% end %>
@@ -0,0 +1,3 @@
+<% title "Update Profile" %>
+
+<%= render 'form' %>
@@ -0,0 +1,5 @@
+<% title "Sign up" %>
+
+<p>Already have an account? <%= link_to "Log in", login_path %>.</p>
+
+<%= render 'form' %>
@@ -0,0 +1,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run Validator::Application
@@ -0,0 +1,44 @@
+require File.expand_path('../boot', __FILE__)
+
+require 'rails/all'
+
+# If you have a Gemfile, require the gems listed there, including any gems
+# you've limited to :test, :development, or :production.
+Bundler.require(:default, Rails.env) if defined?(Bundler)
+
+module Validator
+ class Application < Rails::Application
+ config.autoload_paths << "#{config.root}/lib"
+
+ # Settings in config/environments/* take precedence over those specified here.
+ # Application configuration should go into files in config/initializers
+ # -- all .rb files in that directory are automatically loaded.
+
+ # Custom directories with classes and modules you want to be autoloadable.
+ # config.autoload_paths += %W(#{config.root}/extras)
+
+ # Only load the plugins named here, in the order given (default is alphabetical).
+ # :all can be used as a placeholder for all plugins not explicitly named.
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
+
+ # Activate observers that should always be running.
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
+ # config.time_zone = 'Central Time (US & Canada)'
+
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ # config.i18n.default_locale = :de
+
+ # JavaScript files you want as :defaults (application.js is always included).
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
+
+ # Configure the default encoding used in templates for Ruby 1.9.
+ config.encoding = "utf-8"
+
+ # Configure sensitive parameters which will be filtered from the log file.
+ config.filter_parameters += [:password]
+ end
+end
Oops, something went wrong.

0 comments on commit e7e3a53

Please sign in to comment.