Permalink
Browse files

automatic import from ryanb/railscasts-episodes

  • Loading branch information...
0 parents commit d115446eb412bdb94970cf8d2046834e539e500b @gilesbowkett gilesbowkett committed Jul 29, 2011
Showing with 8,405 additions and 0 deletions.
  1. +3 −0 README
  2. +3 −0 blog/.gitignore
  3. +4 −0 blog/README
  4. +10 −0 blog/Rakefile
  5. +22 −0 blog/app/controllers/application.rb
  6. +45 −0 blog/app/controllers/articles_controller.rb
  7. +41 −0 blog/app/controllers/comments_controller.rb
  8. +3 −0 blog/app/helpers/application_helper.rb
  9. +2 −0 blog/app/helpers/articles_helper.rb
  10. +2 −0 blog/app/helpers/comments_helper.rb
  11. +23 −0 blog/app/helpers/layout_helper.rb
  12. +4 −0 blog/app/models/article.rb
  13. +4 −0 blog/app/models/comment.rb
  14. +14 −0 blog/app/models/user_session.rb
  15. +16 −0 blog/app/views/articles/_form.html.erb
  16. +8 −0 blog/app/views/articles/edit.html.erb
  17. +11 −0 blog/app/views/articles/index.html.erb
  18. +5 −0 blog/app/views/articles/new.html.erb
  19. +15 −0 blog/app/views/articles/show.html.erb
  20. +10 −0 blog/app/views/comments/_comment.html.erb
  21. +17 −0 blog/app/views/comments/_form.html.erb
  22. +4 −0 blog/app/views/comments/edit.html.erb
  23. +4 −0 blog/app/views/comments/new.html.erb
  24. +22 −0 blog/app/views/layouts/application.html.erb
  25. +109 −0 blog/config/boot.rb
  26. +19 −0 blog/config/database.yml
  27. +67 −0 blog/config/environment.rb
  28. +17 −0 blog/config/environments/development.rb
  29. +22 −0 blog/config/environments/production.rb
  30. +22 −0 blog/config/environments/test.rb
  31. +10 −0 blog/config/initializers/inflections.rb
  32. +5 −0 blog/config/initializers/mime_types.rb
  33. +15 −0 blog/config/initializers/new_rails_defaults.rb
  34. +4 −0 blog/config/routes.rb
  35. +14 −0 blog/db/migrate/20080719200447_create_articles.rb
  36. +15 −0 blog/db/migrate/20080719201435_create_comments.rb
  37. +31 −0 blog/db/schema.rb
  38. +2 −0 blog/doc/README_FOR_APP
  39. +2 −0 blog/lib/tasks/application.rake
  40. +30 −0 blog/public/404.html
  41. +30 −0 blog/public/422.html
  42. +30 −0 blog/public/500.html
  43. +10 −0 blog/public/dispatch.cgi
  44. +24 −0 blog/public/dispatch.fcgi
  45. +10 −0 blog/public/dispatch.rb
  46. 0 blog/public/favicon.ico
  47. +2 −0 blog/public/javascripts/application.js
  48. +963 −0 blog/public/javascripts/controls.js
  49. +972 −0 blog/public/javascripts/dragdrop.js
  50. +1,120 −0 blog/public/javascripts/effects.js
  51. +4,225 −0 blog/public/javascripts/prototype.js
  52. +5 −0 blog/public/robots.txt
  53. +113 −0 blog/public/stylesheets/application.css
  54. +3 −0 blog/script/about
  55. +3 −0 blog/script/console
  56. +3 −0 blog/script/dbconsole
  57. +3 −0 blog/script/destroy
  58. +3 −0 blog/script/generate
  59. +3 −0 blog/script/performance/benchmarker
  60. +3 −0 blog/script/performance/profiler
  61. +3 −0 blog/script/performance/request
  62. +3 −0 blog/script/plugin
  63. +3 −0 blog/script/process/inspector
  64. +3 −0 blog/script/process/reaper
  65. +3 −0 blog/script/process/spawner
  66. +3 −0 blog/script/runner
  67. +3 −0 blog/script/server
  68. +17 −0 blog/test/fixtures/articles.yml
  69. +21 −0 blog/test/fixtures/comments.yml
  70. +54 −0 blog/test/functional/articles_controller_test.rb
  71. +37 −0 blog/test/functional/comments_controller_test.rb
  72. +38 −0 blog/test/test_helper.rb
  73. +8 −0 blog/test/unit/article_test.rb
  74. +8 −0 blog/test/unit/comment_test.rb
3 README
@@ -0,0 +1,3 @@
+Railscasts Episode #119: Session Based Model
+
+http://railscasts.com/episodes/119
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
@@ -0,0 +1,4 @@
+Railscasts Example Blog App
+--
+
+To setup the app, just run `rake setup`.
@@ -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,22 @@
+# 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
+ 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
+
+ private
+
+ def user_session
+ @user_session ||= UserSession.new(session)
+ end
+ helper_method :user_session
+end
@@ -0,0 +1,45 @@
+class ArticlesController < ApplicationController
+ 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,41 @@
+class CommentsController < ApplicationController
+ before_filter :authorize, :only => [:edit, :update]
+
+ def new
+ @comment = Comment.new
+ end
+
+ def create
+ @comment = Comment.new(params[:comment])
+ if @comment.save
+ user_session.add_comment(@comment)
+ 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
+
+ private
+
+ def authorize
+ unless user_session.can_edit_comment?(Comment.find(params[:id]))
+ flash[:error] = "You are no longer able to edit this comment."
+ redirect_to root_url
+ 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,4 @@
+class Article < ActiveRecord::Base
+ has_many :comments, :dependent => :destroy
+ validates_presence_of :name, :content
+end
@@ -0,0 +1,4 @@
+class Comment < ActiveRecord::Base
+ belongs_to :article
+ validates_presence_of :author_name, :content
+end
@@ -0,0 +1,14 @@
+class UserSession
+ def initialize(session)
+ @session = session
+ @session[:comment_ids] ||= []
+ end
+
+ def add_comment(comment)
+ @session[:comment_ids] << comment.id
+ end
+
+ def can_edit_comment?(comment)
+ @session[:comment_ids].include?(comment.id) && comment.created_at > 15.minutes.ago
+ 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,11 @@
+<% 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>
+<% end %>
+</div>
@@ -0,0 +1,5 @@
+<% title "New Article" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", articles_path %></p>
@@ -0,0 +1,15 @@
+<% title @article.name %>
+
+<p class="author"><em>from <%=h @article.author_name %></em></p>
+
+<%= simple_format(@article.content) %>
+
+<% unless @article.comments.empty? %>
+ <h2><%= pluralize(@article.comments.size, 'comment') %></h2>
+ <div id="comments">
+ <%= render :partial => @article.comments %>
+ </div>
+<% end %>
+
+<h3>Add your comment:</h3>
+<%= render :partial => 'comments/form' %>
@@ -0,0 +1,10 @@
+<div class="comment">
+ <p>
+ <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>
+ </p>
+ <%= simple_format(h(comment.content)) %>
+ <% if user_session.can_edit_comment? comment %>
+ <p><%= link_to "Edit", edit_comment_path(comment) %></p>
+ <% end %>
+</div>
@@ -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>
Oops, something went wrong.

0 comments on commit d115446

Please sign in to comment.