Permalink
Browse files

adding episode 157

  • Loading branch information...
1 parent d3e94bc commit 1b759fbd086bfc8354a3c333695a8ce6b26f7df5 @ryanb committed Apr 12, 2009
Showing with 9,899 additions and 0 deletions.
  1. +3 −0 episode-157/README
  2. +3 −0 episode-157/blog/.gitignore
  3. +4 −0 episode-157/blog/README
  4. +10 −0 episode-157/blog/Rakefile
  5. +16 −0 episode-157/blog/app/controllers/application_controller.rb
  6. +47 −0 episode-157/blog/app/controllers/articles_controller.rb
  7. +36 −0 episode-157/blog/app/controllers/comments_controller.rb
  8. +22 −0 episode-157/blog/app/controllers/sessions_controller.rb
  9. +16 −0 episode-157/blog/app/controllers/users_controller.rb
  10. +3 −0 episode-157/blog/app/helpers/application_helper.rb
  11. +2 −0 episode-157/blog/app/helpers/articles_helper.rb
  12. +2 −0 episode-157/blog/app/helpers/comments_helper.rb
  13. +23 −0 episode-157/blog/app/helpers/layout_helper.rb
  14. +2 −0 episode-157/blog/app/helpers/sessions_helper.rb
  15. +2 −0 episode-157/blog/app/helpers/users_helper.rb
  16. +5 −0 episode-157/blog/app/models/article.rb
  17. +4 −0 episode-157/blog/app/models/comment.rb
  18. +36 −0 episode-157/blog/app/models/user.rb
  19. +16 −0 episode-157/blog/app/views/articles/_form.html.erb
  20. +8 −0 episode-157/blog/app/views/articles/edit.html.erb
  21. +12 −0 episode-157/blog/app/views/articles/index.html.erb
  22. +5 −0 episode-157/blog/app/views/articles/new.html.erb
  23. +24 −0 episode-157/blog/app/views/articles/show.html.erb
  24. +17 −0 episode-157/blog/app/views/comments/_form.html.erb
  25. +4 −0 episode-157/blog/app/views/comments/edit.html.erb
  26. +4 −0 episode-157/blog/app/views/comments/new.html.erb
  27. +22 −0 episode-157/blog/app/views/layouts/application.html.erb
  28. +15 −0 episode-157/blog/app/views/sessions/new.html.erb
  29. +24 −0 episode-157/blog/app/views/users/new.html.erb
  30. +110 −0 episode-157/blog/config/boot.rb
  31. +19 −0 episode-157/blog/config/database.yml
  32. +67 −0 episode-157/blog/config/environment.rb
  33. +17 −0 episode-157/blog/config/environments/development.rb
  34. +22 −0 episode-157/blog/config/environments/production.rb
  35. +25 −0 episode-157/blog/config/environments/test.rb
  36. +10 −0 episode-157/blog/config/initializers/inflections.rb
  37. +5 −0 episode-157/blog/config/initializers/mime_types.rb
  38. +15 −0 episode-157/blog/config/initializers/new_rails_defaults.rb
  39. +13 −0 episode-157/blog/config/routes.rb
  40. +14 −0 episode-157/blog/db/migrate/20080719200447_create_articles.rb
  41. +15 −0 episode-157/blog/db/migrate/20080719201435_create_comments.rb
  42. +15 −0 episode-157/blog/db/migrate/20090408215231_create_users.rb
  43. +9 −0 episode-157/blog/db/migrate/20090408220837_add_admin_to_users.rb
  44. +9 −0 episode-157/blog/db/migrate/20090409005847_add_position_to_articles.rb
  45. +42 −0 episode-157/blog/db/schema.rb
  46. +2 −0 episode-157/blog/doc/README_FOR_APP
  47. +47 −0 episode-157/blog/lib/authentication.rb
  48. +2 −0 episode-157/blog/lib/tasks/application.rake
  49. +165 −0 episode-157/blog/lib/tasks/rspec.rake
  50. +30 −0 episode-157/blog/public/404.html
  51. +30 −0 episode-157/blog/public/422.html
  52. +30 −0 episode-157/blog/public/500.html
  53. +10 −0 episode-157/blog/public/dispatch.cgi
  54. +24 −0 episode-157/blog/public/dispatch.fcgi
  55. +10 −0 episode-157/blog/public/dispatch.rb
  56. 0 episode-157/blog/public/favicon.ico
  57. +2 −0 episode-157/blog/public/javascripts/application.js
  58. +963 −0 episode-157/blog/public/javascripts/controls.js
  59. +973 −0 episode-157/blog/public/javascripts/dragdrop.js
  60. +1,128 −0 episode-157/blog/public/javascripts/effects.js
  61. +4,320 −0 episode-157/blog/public/javascripts/prototype.js
  62. +5 −0 episode-157/blog/public/robots.txt
  63. +113 −0 episode-157/blog/public/stylesheets/application.css
  64. +3 −0 episode-157/blog/script/about
  65. +6 −0 episode-157/blog/script/autospec
  66. +3 −0 episode-157/blog/script/console
  67. +3 −0 episode-157/blog/script/dbconsole
  68. +3 −0 episode-157/blog/script/destroy
  69. +3 −0 episode-157/blog/script/generate
  70. +3 −0 episode-157/blog/script/performance/benchmarker
  71. +3 −0 episode-157/blog/script/performance/profiler
  72. +3 −0 episode-157/blog/script/performance/request
  73. +3 −0 episode-157/blog/script/plugin
  74. +3 −0 episode-157/blog/script/process/inspector
  75. +3 −0 episode-157/blog/script/process/reaper
  76. +3 −0 episode-157/blog/script/process/spawner
  77. +3 −0 episode-157/blog/script/runner
  78. +3 −0 episode-157/blog/script/server
  79. +10 −0 episode-157/blog/script/spec
  80. +9 −0 episode-157/blog/script/spec_server
  81. +24 −0 episode-157/blog/spec/controller_macros.rb
  82. +67 −0 episode-157/blog/spec/controllers/articles_controller_spec.rb
  83. +24 −0 episode-157/blog/spec/custom_matchers.rb
  84. +17 −0 episode-157/blog/spec/fixtures/articles.yml
  85. +21 −0 episode-157/blog/spec/fixtures/comments.yml
  86. +12 −0 episode-157/blog/spec/fixtures/users.yml
  87. +14 −0 episode-157/blog/spec/models/article_spec.rb
  88. +2 −0 episode-157/blog/spec/rcov.opts
  89. +4 −0 episode-157/blog/spec/spec.opts
  90. +54 −0 episode-157/blog/spec/spec_helper.rb
  91. +17 −0 episode-157/blog/test/fixtures/articles.yml
  92. +21 −0 episode-157/blog/test/fixtures/comments.yml
  93. +12 −0 episode-157/blog/test/fixtures/users.yml
  94. +54 −0 episode-157/blog/test/functional/articles_controller_test.rb
  95. +44 −0 episode-157/blog/test/functional/comments_controller_test.rb
  96. +22 −0 episode-157/blog/test/functional/sessions_controller_test.rb
  97. +21 −0 episode-157/blog/test/functional/users_controller_test.rb
  98. +38 −0 episode-157/blog/test/test_helper.rb
  99. +8 −0 episode-157/blog/test/unit/article_test.rb
  100. +8 −0 episode-157/blog/test/unit/comment_test.rb
  101. +86 −0 episode-157/blog/test/unit/user_test.rb
  102. +23 −0 episode-157/blog/vendor/plugins/acts_as_list/README
  103. +3 −0 episode-157/blog/vendor/plugins/acts_as_list/init.rb
  104. +256 −0 episode-157/blog/vendor/plugins/acts_as_list/lib/active_record/acts/list.rb
  105. +332 −0 episode-157/blog/vendor/plugins/acts_as_list/test/list_test.rb
View
@@ -0,0 +1,3 @@
+Railscasts Episode #157: RSpec Matchers & Macros
+
+http://railscasts.com/episodes/157
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
View
@@ -0,0 +1,4 @@
+Railscasts Example Blog App
+--
+
+To setup the app, just run `rake setup`.
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,16 @@
+# 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 => '9346baedc099926e71cc3a3f39613aa6'
+
+ # 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
+end
@@ -0,0 +1,47 @@
+class ArticlesController < ApplicationController
+ before_filter :admin_required, :except => [:index, :show]
+
+ def index
+ @articles = Article.find(:all)
+ end
+
+ def show
+ @article = Article.find(params[:id])
+ @comment = Comment.new(:article => @article)
+ end
+
+ def new
+ @article = Article.new
+ end
+
+ def create
+ @article = Article.new(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])
+ 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,36 @@
+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 article_url(@comment.article_id)
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @comment = Comment.find(params[:id])
+ end
+
+ def update
+ @comment = Comment.find(params[:id])
+ if @comment.update_attributes(params[:comment])
+ flash[:notice] = "Successfully updated comment."
+ redirect_to article_url(@comment.article_id)
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @comment = Comment.find(params[:id])
+ @comment.destroy
+ flash[:notice] = "Successfully destroyed comment."
+ redirect_to article_url(@comment.article_id)
+ 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 ArticlesHelper
+end
@@ -0,0 +1,2 @@
+module CommentsHelper
+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,5 @@
+class Article < ActiveRecord::Base
+ has_many :comments, :dependent => :destroy
+ validates_presence_of :name
+ acts_as_list
+end
@@ -0,0 +1,4 @@
+class Comment < ActiveRecord::Base
+ belongs_to :article
+ validates_presence_of :author_name, :content
+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,16 @@
+<%= error_messages_for :article %>
+<% form_for @article do |f| %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :content %><br />
+ <%= f.text_area :content %>
+ </p>
+ <p>
+ <%= f.label :author_name %><br />
+ <%= f.text_field :author_name %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Article" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @article %> |
+ <%= link_to "View All", articles_path %>
+</p>
@@ -0,0 +1,12 @@
+<% title "Articles" %>
+
+<div id="articles">
+<% for article in @articles %>
+ <h2>
+ <%= link_to h(article.name), article %>
+ <span class="comments">(<%= pluralize(article.comments.size, 'comment') %>)</span>
+ </h2>
+ <div class="author">from <%=h article.author_name %> on <%= article.created_at.strftime('%b %d, %Y') %></div>
+ <div class="content"><%= simple_format(article.content) %></div>
+<% end %>
+</div>
@@ -0,0 +1,5 @@
+<% title "New Article" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", articles_path %></p>
@@ -0,0 +1,24 @@
+<% title @article.name %>
+
+<p class="author"><em>from <%=h @article.author_name %></em></p>
+
+<%=simple_format @article.content %>
+
+<p><%= link_to "Back to Articles", articles_path %></p>
+
+<% unless @article.comments.empty? %>
+ <h2><%= pluralize(@article.comments.size, 'comment') %></h2>
+
+ <div id="comments">
+ <% for comment in @article.comments %>
+ <div class="comment">
+ <strong><%= link_to_unless comment.site_url.blank?, h(comment.author_name), h(comment.site_url) %></strong>
+ <em>on <%= comment.created_at.strftime('%b %d, %Y at %H:%M') %></em>
+ <%=simple_format comment.content %>
+ </div>
+ <% end %>
+ </div>
+<% end %>
+
+<h3>Add your comment:</h3>
+<%= render :partial => 'comments/form' %>
@@ -0,0 +1,17 @@
+<%= error_messages_for :comment %>
+<% form_for @comment do |f| %>
+ <%= f.hidden_field :article_id %>
+ <p>
+ <%= f.label :author_name, 'Name' %><br />
+ <%= f.text_field :author_name %>
+ </p>
+ <p>
+ <%= f.label :site_url, 'Website URL' %><br />
+ <%= f.text_field :site_url %>
+ </p>
+ <p>
+ <%= f.label :content, 'Comment' %><br />
+ <%= f.text_area :content, :rows => '12', :cols => 35 %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,4 @@
+<% title "Edit Comment" %>
+
+<%= render :partial => 'form' %>
+
@@ -0,0 +1,4 @@
+<% title "New Comment" %>
+
+<%= render :partial => 'form' %>
+
@@ -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 %>
Oops, something went wrong.

0 comments on commit 1b759fb

Please sign in to comment.