Permalink
Browse files

adding episode 229

  • Loading branch information...
1 parent 9496377 commit 0d4d7c497ce97d6a80d3b0bf347bd990528c17e5 @ryanb committed Aug 30, 2010
Showing with 7,567 additions and 0 deletions.
  1. +8 −0 episode-229/README
  2. +4 −0 episode-229/blog/.gitignore
  3. +6 −0 episode-229/blog/Gemfile
  4. +78 −0 episode-229/blog/Gemfile.lock
  5. +1 −0 episode-229/blog/README
  6. +7 −0 episode-229/blog/Rakefile
  7. +3 −0 episode-229/blog/app/controllers/application_controller.rb
  8. +45 −0 episode-229/blog/app/controllers/articles_controller.rb
  9. +19 −0 episode-229/blog/app/controllers/comments_controller.rb
  10. +2 −0 episode-229/blog/app/helpers/application_helper.rb
  11. +2 −0 episode-229/blog/app/helpers/articles_helper.rb
  12. +2 −0 episode-229/blog/app/helpers/comments_helper.rb
  13. +23 −0 episode-229/blog/app/helpers/error_messages_helper.rb
  14. +22 −0 episode-229/blog/app/helpers/layout_helper.rb
  15. +4 −0 episode-229/blog/app/models/article.rb
  16. +4 −0 episode-229/blog/app/models/comment.rb
  17. +12 −0 episode-229/blog/app/views/articles/_form.html.erb
  18. +8 −0 episode-229/blog/app/views/articles/edit.html.erb
  19. +14 −0 episode-229/blog/app/views/articles/index.html.erb
  20. +5 −0 episode-229/blog/app/views/articles/new.html.erb
  21. +19 −0 episode-229/blog/app/views/articles/show.html.erb
  22. +5 −0 episode-229/blog/app/views/comments/_comment.html.erb
  23. +13 −0 episode-229/blog/app/views/comments/_form.html.erb
  24. +4 −0 episode-229/blog/app/views/comments/index.js.erb
  25. +3 −0 episode-229/blog/app/views/comments/new.html.erb
  26. +19 −0 episode-229/blog/app/views/layouts/application.html.erb
  27. +4 −0 episode-229/blog/config.ru
  28. +42 −0 episode-229/blog/config/application.rb
  29. +13 −0 episode-229/blog/config/boot.rb
  30. +22 −0 episode-229/blog/config/database.yml
  31. +5 −0 episode-229/blog/config/environment.rb
  32. +26 −0 episode-229/blog/config/environments/development.rb
  33. +49 −0 episode-229/blog/config/environments/production.rb
  34. +35 −0 episode-229/blog/config/environments/test.rb
  35. +7 −0 episode-229/blog/config/initializers/backtrace_silencers.rb
  36. +10 −0 episode-229/blog/config/initializers/inflections.rb
  37. +5 −0 episode-229/blog/config/initializers/mime_types.rb
  38. +7 −0 episode-229/blog/config/initializers/secret_token.rb
  39. +8 −0 episode-229/blog/config/initializers/session_store.rb
  40. +5 −0 episode-229/blog/config/locales/en.yml
  41. +62 −0 episode-229/blog/config/routes.rb
  42. +13 −0 episode-229/blog/db/migrate/20100830033459_create_articles.rb
  43. +14 −0 episode-229/blog/db/migrate/20100830033648_create_comments.rb
  44. +30 −0 episode-229/blog/db/schema.rb
  45. +7 −0 episode-229/blog/db/seeds.rb
  46. +2 −0 episode-229/blog/doc/README_FOR_APP
  47. 0 episode-229/blog/lib/tasks/.gitkeep
  48. +26 −0 episode-229/blog/public/404.html
  49. +26 −0 episode-229/blog/public/422.html
  50. +26 −0 episode-229/blog/public/500.html
  51. 0 episode-229/blog/public/favicon.ico
  52. +16 −0 episode-229/blog/public/javascripts/application.js
  53. +6,240 −0 episode-229/blog/public/javascripts/jquery.js
  54. +154 −0 episode-229/blog/public/javascripts/jquery.min.js
  55. +132 −0 episode-229/blog/public/javascripts/rails.js
  56. +5 −0 episode-229/blog/public/robots.txt
  57. 0 episode-229/blog/public/stylesheets/.gitkeep
  58. +112 −0 episode-229/blog/public/stylesheets/application.css
  59. +6 −0 episode-229/blog/script/rails
  60. +7 −0 episode-229/blog/test/fixtures/articles.yml
  61. +9 −0 episode-229/blog/test/fixtures/comments.yml
  62. +54 −0 episode-229/blog/test/functional/articles_controller_test.rb
  63. +20 −0 episode-229/blog/test/functional/comments_controller_test.rb
  64. +9 −0 episode-229/blog/test/performance/browsing_test.rb
  65. +13 −0 episode-229/blog/test/test_helper.rb
  66. +7 −0 episode-229/blog/test/unit/article_test.rb
  67. +7 −0 episode-229/blog/test/unit/comment_test.rb
  68. 0 episode-229/blog/vendor/plugins/.gitkeep
View
@@ -0,0 +1,8 @@
+Railscasts Episode #229: Polling for Changes
+
+http://railscasts.com/episodes/229
+
+Commands
+
+ bundle install
+ rails g jquery:install
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
View
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.0'
+gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
+gem 'nifty-generators'
+gem 'jquery-rails'
@@ -0,0 +1,78 @@
+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)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.4.1)
+ jquery-rails (0.1.2)
+ rails (~> 3.0.0.rc)
+ mail (2.2.5)
+ activesupport (>= 2.3.6)
+ mime-types
+ treetop (>= 1.4.5)
+ mime-types (1.16)
+ nifty-generators (0.4.0)
+ polyglot (0.3.1)
+ rack (1.2.1)
+ rack-mount (0.6.12)
+ rack (>= 1.0.0)
+ rack-test (0.5.4)
+ 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.0)
+ treetop (1.4.8)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.23)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ jquery-rails
+ 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,3 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+end
@@ -0,0 +1,45 @@
+class ArticlesController < ApplicationController
+ def index
+ @articles = Article.all
+ 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(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,19 @@
+class CommentsController < ApplicationController
+ def index
+ @comments = Comment.where("article_id = ? and created_at > ?", params[:article_id], Time.at(params[:after].to_i + 1))
+ end
+
+ def new
+ @comment = Comment.new
+ end
+
+ def create
+ @comment = Comment.new(params[:comment])
+ if @comment.save
+ flash[:notice] = "Successfully created comment."
+ redirect_to @comment.article
+ else
+ render :action => 'new'
+ 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,4 @@
+class Article < ActiveRecord::Base
+ attr_accessible :name, :content
+ has_many :comments
+end
@@ -0,0 +1,4 @@
+class Comment < ActiveRecord::Base
+ attr_accessible :article_id, :name, :content
+ belongs_to :article
+end
@@ -0,0 +1,12 @@
+<%= 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.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,14 @@
+<% title "Articles" %>
+
+<div id="articles">
+<% for article in @articles %>
+ <h2>
+ <%= link_to article.name, article %>
+ <span class="comments">(<%= pluralize(article.comments.size, 'comment') %>)</span>
+ </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>
@@ -0,0 +1,19 @@
+<% title @article.name %>
+
+<div id="article" data-id="<%= @article.id %>">
+ <%= 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">
+ <%= render @article.comments %>
+ </div>
+ <% end %>
+
+ <h3>Add your comment:</h3>
+
+ <%= render :partial => 'comments/form' %>
+</div>
@@ -0,0 +1,5 @@
+<div class="comment" data-time="<%= comment.created_at.to_i %>">
+ <strong><%= comment.name %></strong>
+ <em>on <%= comment.created_at.strftime('%b %d, %Y at %I:%M %p') %></em>
+ <%= simple_format comment.content %>
+</div>
@@ -0,0 +1,13 @@
+<%= form_for @comment do |f| %>
+ <%= f.error_messages %>
+ <%= f.hidden_field :article_id %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :content, "Comment" %><br />
+ <%= f.text_area :content, :rows => '12', :cols => 35 %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
@@ -0,0 +1,4 @@
+<% unless @comments.empty? %>
+$("#comments").append("<%=raw escape_javascript(render(@comments)) %>");
+$("#article h2").text($(".comment").length + " comments");
+<% end %>
@@ -0,0 +1,3 @@
+<% title "New Comment" %>
+
+<%= render "form" %>
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title><%= yield(:title) || "Untitled" %></title>
+ <%= stylesheet_link_tag "application" %>
+ <%= javascript_include_tag :defaults %>
+ <%= 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,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run Blog::Application
Oops, something went wrong.

0 comments on commit 0d4d7c4

Please sign in to comment.