Permalink
Browse files

adding episode 237

  • Loading branch information...
1 parent 39ec2fc commit 788152530dff14c2f5f500998fd9056bf9c0591a @ryanb committed Oct 25, 2010
Showing with 10,689 additions and 0 deletions.
  1. +3 −0 episode-237/README
  2. +4 −0 episode-237/blog/.gitignore
  3. +7 −0 episode-237/blog/Gemfile
  4. +80 −0 episode-237/blog/Gemfile.lock
  5. +1 −0 episode-237/blog/README
  6. +7 −0 episode-237/blog/Rakefile
  7. +4 −0 episode-237/blog/app/controllers/application_controller.rb
  8. +48 −0 episode-237/blog/app/controllers/articles_controller.rb
  9. +15 −0 episode-237/blog/app/controllers/comments_controller.rb
  10. +22 −0 episode-237/blog/app/controllers/sessions_controller.rb
  11. +32 −0 episode-237/blog/app/controllers/users_controller.rb
  12. +2 −0 episode-237/blog/app/helpers/application_helper.rb
  13. +2 −0 episode-237/blog/app/helpers/articles_helper.rb
  14. +2 −0 episode-237/blog/app/helpers/comments_helper.rb
  15. +23 −0 episode-237/blog/app/helpers/error_messages_helper.rb
  16. +22 −0 episode-237/blog/app/helpers/layout_helper.rb
  17. +2 −0 episode-237/blog/app/helpers/sessions_helper.rb
  18. +2 −0 episode-237/blog/app/helpers/users_helper.rb
  19. +3 −0 episode-237/blog/app/models/article.rb
  20. +4 −0 episode-237/blog/app/models/comment.rb
  21. +38 −0 episode-237/blog/app/models/user.rb
  22. +16 −0 episode-237/blog/app/views/articles/_form.html.erb
  23. +8 −0 episode-237/blog/app/views/articles/edit.html.erb
  24. +13 −0 episode-237/blog/app/views/articles/index.html.erb
  25. +5 −0 episode-237/blog/app/views/articles/new.html.erb
  26. +10 −0 episode-237/blog/app/views/articles/show.html.erb
  27. +13 −0 episode-237/blog/app/views/comments/_form.html.erb
  28. +3 −0 episode-237/blog/app/views/comments/new.html.erb
  29. +29 −0 episode-237/blog/app/views/layouts/application.html.erb
  30. +15 −0 episode-237/blog/app/views/sessions/new.html.erb
  31. +24 −0 episode-237/blog/app/views/users/_form.html.erb
  32. +3 −0 episode-237/blog/app/views/users/edit.html.erb
  33. +5 −0 episode-237/blog/app/views/users/new.html.erb
  34. +4 −0 episode-237/blog/config.ru
  35. +42 −0 episode-237/blog/config/application.rb
  36. +13 −0 episode-237/blog/config/boot.rb
  37. +22 −0 episode-237/blog/config/database.yml
  38. +5 −0 episode-237/blog/config/environment.rb
  39. +26 −0 episode-237/blog/config/environments/development.rb
  40. +49 −0 episode-237/blog/config/environments/production.rb
  41. +35 −0 episode-237/blog/config/environments/test.rb
  42. +14 −0 episode-237/blog/config/initializers/accessible_attributes.rb
  43. +7 −0 episode-237/blog/config/initializers/backtrace_silencers.rb
  44. +10 −0 episode-237/blog/config/initializers/inflections.rb
  45. +5 −0 episode-237/blog/config/initializers/mime_types.rb
  46. +7 −0 episode-237/blog/config/initializers/secret_token.rb
  47. +8 −0 episode-237/blog/config/initializers/session_store.rb
  48. +5 −0 episode-237/blog/config/locales/en.yml
  49. +68 −0 episode-237/blog/config/routes.rb
  50. +13 −0 episode-237/blog/db/migrate/20100830033459_create_articles.rb
  51. +14 −0 episode-237/blog/db/migrate/20100830033648_create_comments.rb
  52. +16 −0 episode-237/blog/db/migrate/20101024234441_create_users.rb
  53. +9 −0 episode-237/blog/db/migrate/20101024235053_add_important_to_articles.rb
  54. +41 −0 episode-237/blog/db/schema.rb
  55. +7 −0 episode-237/blog/db/seeds.rb
  56. +2 −0 episode-237/blog/doc/README_FOR_APP
  57. +53 −0 episode-237/blog/lib/controller_authentication.rb
  58. 0 episode-237/blog/lib/tasks/.gitkeep
  59. +26 −0 episode-237/blog/public/404.html
  60. +26 −0 episode-237/blog/public/422.html
  61. +26 −0 episode-237/blog/public/500.html
  62. 0 episode-237/blog/public/favicon.ico
  63. +2 −0 episode-237/blog/public/javascripts/application.js
  64. +965 −0 episode-237/blog/public/javascripts/controls.js
  65. +974 −0 episode-237/blog/public/javascripts/dragdrop.js
  66. +1,123 −0 episode-237/blog/public/javascripts/effects.js
  67. +6,001 −0 episode-237/blog/public/javascripts/prototype.js
  68. +175 −0 episode-237/blog/public/javascripts/rails.js
  69. +5 −0 episode-237/blog/public/robots.txt
  70. 0 episode-237/blog/public/stylesheets/.gitkeep
  71. +121 −0 episode-237/blog/public/stylesheets/application.css
  72. +6 −0 episode-237/blog/script/rails
  73. +7 −0 episode-237/blog/test/fixtures/articles.yml
  74. +9 −0 episode-237/blog/test/fixtures/comments.yml
  75. +12 −0 episode-237/blog/test/fixtures/users.yml
  76. +54 −0 episode-237/blog/test/functional/articles_controller_test.rb
  77. +20 −0 episode-237/blog/test/functional/comments_controller_test.rb
  78. +22 −0 episode-237/blog/test/functional/sessions_controller_test.rb
  79. +51 −0 episode-237/blog/test/functional/users_controller_test.rb
  80. +9 −0 episode-237/blog/test/performance/browsing_test.rb
  81. +13 −0 episode-237/blog/test/test_helper.rb
  82. +7 −0 episode-237/blog/test/unit/article_test.rb
  83. +7 −0 episode-237/blog/test/unit/comment_test.rb
  84. +86 −0 episode-237/blog/test/unit/user_test.rb
  85. 0 episode-237/blog/vendor/plugins/.gitkeep
View
@@ -0,0 +1,3 @@
+Railscasts Episode #237: Dynamic attr_accessible
+
+http://railscasts.com/episodes/237
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
View
@@ -0,0 +1,7 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.0'
+gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
+gem 'nifty-generators'
+gem "bcrypt-ruby", :require => "bcrypt"
+gem "mocha", :group => :test
@@ -0,0 +1,80 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.0)
+ actionpack (= 3.0.0)
+ mail (~> 2.2.5)
+ actionpack (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4.1)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.12)
+ rack-test (~> 0.5.4)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activerecord (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ arel (~> 1.0.0)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ activesupport (3.0.0)
+ arel (1.0.1)
+ activesupport (~> 3.0.0)
+ bcrypt-ruby (2.1.2)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.4.1)
+ mail (2.2.7)
+ activesupport (>= 2.3.6)
+ mime-types
+ treetop (>= 1.4.5)
+ mime-types (1.16)
+ mocha (0.9.8)
+ rake
+ nifty-generators (0.4.2)
+ polyglot (0.3.1)
+ rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
+ rack-test (0.5.6)
+ rack (>= 1.0)
+ rails (3.0.0)
+ actionmailer (= 3.0.0)
+ actionpack (= 3.0.0)
+ activerecord (= 3.0.0)
+ activeresource (= 3.0.0)
+ activesupport (= 3.0.0)
+ bundler (~> 1.0.0)
+ railties (= 3.0.0)
+ railties (3.0.0)
+ actionpack (= 3.0.0)
+ activesupport (= 3.0.0)
+ rake (>= 0.8.4)
+ thor (~> 0.14.0)
+ rake (0.8.7)
+ sqlite3-ruby (1.2.5)
+ thor (0.14.3)
+ treetop (1.4.8)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.23)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bcrypt-ruby
+ mocha
+ nifty-generators
+ rails (= 3.0.0)
+ sqlite3-ruby (= 1.2.5)
View
@@ -0,0 +1 @@
+Example Railscasts Application
@@ -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'
+
+Blog::Application.load_tasks
@@ -0,0 +1,4 @@
+class ApplicationController < ActionController::Base
+ include ControllerAuthentication
+ protect_from_forgery
+end
@@ -0,0 +1,48 @@
+class ArticlesController < ApplicationController
+ def index
+ @articles = Article.order("important desc")
+ end
+
+ def show
+ @article = Article.find(params[:id])
+ @comment = Comment.new(:article_id => @article.id)
+ end
+
+ def new
+ @article = Article.new
+ end
+
+ def create
+ @article = Article.new
+ @article.accessible = :all if admin?
+ @article.attributes = params[:article]
+ if @article.save
+ flash[:notice] = "Successfully created article."
+ redirect_to @article
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @article = Article.find(params[:id])
+ end
+
+ def update
+ @article = Article.find(params[:id])
+ @article.accessible = :all if admin?
+ if @article.update_attributes(params[:article])
+ flash[:notice] = "Successfully updated article."
+ redirect_to @article
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @article = Article.find(params[:id])
+ @article.destroy
+ flash[:notice] = "Successfully destroyed article."
+ redirect_to articles_url
+ end
+end
@@ -0,0 +1,15 @@
+class CommentsController < ApplicationController
+ def new
+ @comment = Comment.new
+ end
+
+ def create
+ @comment = Comment.new(params[:comment])
+ if @comment.save
+ flash[:notice] = "Successfully created comment."
+ redirect_to root_url
+ else
+ render :action => 'new'
+ end
+ 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_target_or_default("/")
+ 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 "/"
+ end
+end
@@ -0,0 +1,32 @@
+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
+ flash[:notice] = "Thank you for signing up! You are now logged in."
+ redirect_to "/"
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @user = current_user
+ end
+
+ def update
+ @user = current_user
+ if @user.update_attributes(params[:user])
+ flash[:notice] = "Your profile has been updated."
+ redirect_to "/"
+ else
+ render :action => 'edit'
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module ApplicationHelper
+end
@@ -0,0 +1,2 @@
+module ArticlesHelper
+end
@@ -0,0 +1,2 @@
+module CommentsHelper
+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] ||= "Invalid Fields"
+ options[:message] ||= "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) { 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,3 @@
+class Article < ActiveRecord::Base
+ attr_accessible :name, :content
+end
@@ -0,0 +1,4 @@
+class Comment < ActiveRecord::Base
+ attr_accessible :article_id, :name, :content
+ belongs_to :article
+end
@@ -0,0 +1,38 @@
+class User < ActiveRecord::Base
+ # new columns need to be added here to be writable through mass assignment
+ attr_accessible :username, :email, :password, :password_confirmation, :admin
+
+ attr_accessor :password
+ before_save :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
+ unless password.blank?
+ self.password_salt = BCrypt::Engine.generate_salt
+ self.password_hash = encrypt_password(password)
+ end
+ end
+
+ def encrypt_password(pass)
+ BCrypt::Engine.hash_secret(pass, password_salt)
+ end
+end
@@ -0,0 +1,16 @@
+<%= form_for @article do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :content %><br />
+ <%= f.text_area :content %>
+ </p>
+ <p>
+ <%= f.check_box :important %>
+ <%= f.label :important %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Article" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", @article %> |
+ <%= link_to "View All", articles_path %>
+</p>
@@ -0,0 +1,13 @@
+<% title "Articles" %>
+
+<div id="articles">
+<% for article in @articles %>
+ <h2>
+ <%= link_to article.name, article %>
+ </h2>
+ <div class="created_at">on <%= article.created_at.strftime('%b %d, %Y') %></div>
+ <div class="content"><%= simple_format(article.content) %></div>
+<% end %>
+</div>
+
+<p><%= link_to "New Article", new_article_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Article" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", articles_path %></p>
Oops, something went wrong.

0 comments on commit 7881525

Please sign in to comment.