Permalink
Browse files

adding episode 181

  • Loading branch information...
1 parent f49f46e commit 59c181cb138b03b4fb676737bed8cc34e3569036 @ryanb committed Sep 27, 2009
Showing with 8,701 additions and 0 deletions.
  1. +13 −0 episode-181/README
  2. +3 −0 episode-181/commentator/.gitignore
  3. +1 −0 episode-181/commentator/README
  4. +10 −0 episode-181/commentator/Rakefile
  5. +10 −0 episode-181/commentator/app/controllers/application_controller.rb
  6. +44 −0 episode-181/commentator/app/controllers/comments_controller.rb
  7. +44 −0 episode-181/commentator/app/controllers/groups_controller.rb
  8. +44 −0 episode-181/commentator/app/controllers/users_controller.rb
  9. +3 −0 episode-181/commentator/app/helpers/application_helper.rb
  10. +2 −0 episode-181/commentator/app/helpers/comments_helper.rb
  11. +2 −0 episode-181/commentator/app/helpers/groups_helper.rb
  12. +22 −0 episode-181/commentator/app/helpers/layout_helper.rb
  13. +2 −0 episode-181/commentator/app/helpers/users_helper.rb
  14. +3 −0 episode-181/commentator/app/models/comment.rb
  15. +8 −0 episode-181/commentator/app/models/group.rb
  16. +4 −0 episode-181/commentator/app/models/membership.rb
  17. +5 −0 episode-181/commentator/app/models/user.rb
  18. +7 −0 episode-181/commentator/app/views/comments/_comment.html.erb
  19. +12 −0 episode-181/commentator/app/views/comments/_form.html.erb
  20. +8 −0 episode-181/commentator/app/views/comments/edit.html.erb
  21. +5 −0 episode-181/commentator/app/views/comments/index.html.erb
  22. +5 −0 episode-181/commentator/app/views/comments/new.html.erb
  23. +16 −0 episode-181/commentator/app/views/comments/show.html.erb
  24. +8 −0 episode-181/commentator/app/views/groups/_form.html.erb
  25. +8 −0 episode-181/commentator/app/views/groups/edit.html.erb
  26. +17 −0 episode-181/commentator/app/views/groups/index.html.erb
  27. +5 −0 episode-181/commentator/app/views/groups/new.html.erb
  28. +18 −0 episode-181/commentator/app/views/groups/show.html.erb
  29. +22 −0 episode-181/commentator/app/views/layouts/application.html.erb
  30. +18 −0 episode-181/commentator/app/views/users/_form.html.erb
  31. +8 −0 episode-181/commentator/app/views/users/edit.html.erb
  32. +15 −0 episode-181/commentator/app/views/users/index.html.erb
  33. +5 −0 episode-181/commentator/app/views/users/new.html.erb
  34. +16 −0 episode-181/commentator/app/views/users/show.html.erb
  35. +110 −0 episode-181/commentator/config/boot.rb
  36. +22 −0 episode-181/commentator/config/database.yml
  37. +41 −0 episode-181/commentator/config/environment.rb
  38. +17 −0 episode-181/commentator/config/environments/development.rb
  39. +28 −0 episode-181/commentator/config/environments/production.rb
  40. +28 −0 episode-181/commentator/config/environments/test.rb
  41. +7 −0 episode-181/commentator/config/initializers/backtrace_silencers.rb
  42. +10 −0 episode-181/commentator/config/initializers/inflections.rb
  43. +5 −0 episode-181/commentator/config/initializers/mime_types.rb
  44. +21 −0 episode-181/commentator/config/initializers/new_rails_defaults.rb
  45. +15 −0 episode-181/commentator/config/initializers/session_store.rb
  46. +5 −0 episode-181/commentator/config/locales/en.yml
  47. +6 −0 episode-181/commentator/config/routes.rb
  48. +13 −0 episode-181/commentator/db/migrate/20090927193755_create_users.rb
  49. +13 −0 episode-181/commentator/db/migrate/20090927193835_create_comments.rb
  50. +12 −0 episode-181/commentator/db/migrate/20090927193845_create_groups.rb
  51. +13 −0 episode-181/commentator/db/migrate/20090927194255_create_memberships.rb
  52. +41 −0 episode-181/commentator/db/schema.rb
  53. +7 −0 episode-181/commentator/db/seeds.rb
  54. +2 −0 episode-181/commentator/doc/README_FOR_APP
  55. +30 −0 episode-181/commentator/public/404.html
  56. +30 −0 episode-181/commentator/public/422.html
  57. +30 −0 episode-181/commentator/public/500.html
  58. 0 episode-181/commentator/public/favicon.ico
  59. +2 −0 episode-181/commentator/public/javascripts/application.js
  60. +963 −0 episode-181/commentator/public/javascripts/controls.js
  61. +973 −0 episode-181/commentator/public/javascripts/dragdrop.js
  62. +1,128 −0 episode-181/commentator/public/javascripts/effects.js
  63. +4,320 −0 episode-181/commentator/public/javascripts/prototype.js
  64. +5 −0 episode-181/commentator/public/robots.txt
  65. +97 −0 episode-181/commentator/public/stylesheets/application.css
  66. +4 −0 episode-181/commentator/script/about
  67. +3 −0 episode-181/commentator/script/console
  68. +3 −0 episode-181/commentator/script/dbconsole
  69. +3 −0 episode-181/commentator/script/destroy
  70. +3 −0 episode-181/commentator/script/generate
  71. +3 −0 episode-181/commentator/script/performance/benchmarker
  72. +3 −0 episode-181/commentator/script/performance/profiler
  73. +3 −0 episode-181/commentator/script/plugin
  74. +3 −0 episode-181/commentator/script/runner
  75. +3 −0 episode-181/commentator/script/server
  76. +7 −0 episode-181/commentator/test/fixtures/comments.yml
  77. +5 −0 episode-181/commentator/test/fixtures/groups.yml
  78. +7 −0 episode-181/commentator/test/fixtures/memberships.yml
  79. +7 −0 episode-181/commentator/test/fixtures/users.yml
  80. +54 −0 episode-181/commentator/test/functional/comments_controller_test.rb
  81. +54 −0 episode-181/commentator/test/functional/groups_controller_test.rb
  82. +54 −0 episode-181/commentator/test/functional/users_controller_test.rb
  83. +9 −0 episode-181/commentator/test/performance/browsing_test.rb
  84. +38 −0 episode-181/commentator/test/test_helper.rb
  85. +7 −0 episode-181/commentator/test/unit/comment_test.rb
  86. +7 −0 episode-181/commentator/test/unit/group_test.rb
  87. +7 −0 episode-181/commentator/test/unit/membership_test.rb
  88. +7 −0 episode-181/commentator/test/unit/user_test.rb
View
@@ -0,0 +1,13 @@
+Railscasts Episode #181: Include vs Joins
+
+http://railscasts.com/episodes/181
+
+script/console
+
+ c = Comment.all(:joins => :user, :conditions => { :users => { :admin => true } })
+ c.first.users
+ c = Comment.all(:include => :user, :conditions => { :users => { :admin => true } })
+ c.first.users
+ User.all(:joins => :comments, :select => "users.*, count(comments.id) as comments_count", :group => "users.id")
+ g = Group.first
+ Comment.all(:joins => {:user => :memberships}, :conditions => { :memberships => { :group_id => g.id } })
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
@@ -0,0 +1 @@
+Example Rails application used in Railscasts
@@ -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,10 @@
+# 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
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
+
+ # Scrub sensitive parameters from your log
+ # filter_parameter_logging :password
+end
@@ -0,0 +1,44 @@
+class CommentsController < ApplicationController
+ def index
+ @comments = Comment.all(:joins => :user, :conditions => { :users => { :admin => true } }, :order => "comments.created_at desc")
+ end
+
+ def show
+ @comment = Comment.find(params[:id])
+ 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
+ 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 @comment
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @comment = Comment.find(params[:id])
+ @comment.destroy
+ flash[:notice] = "Successfully destroyed comment."
+ redirect_to comments_url
+ end
+end
@@ -0,0 +1,44 @@
+class GroupsController < ApplicationController
+ def index
+ @groups = Group.all
+ end
+
+ def show
+ @group = Group.find(params[:id])
+ end
+
+ def new
+ @group = Group.new
+ end
+
+ def create
+ @group = Group.new(params[:group])
+ if @group.save
+ flash[:notice] = "Successfully created group."
+ redirect_to @group
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @group = Group.find(params[:id])
+ end
+
+ def update
+ @group = Group.find(params[:id])
+ if @group.update_attributes(params[:group])
+ flash[:notice] = "Successfully updated group."
+ redirect_to @group
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @group = Group.find(params[:id])
+ @group.destroy
+ flash[:notice] = "Successfully destroyed group."
+ redirect_to groups_url
+ end
+end
@@ -0,0 +1,44 @@
+class UsersController < ApplicationController
+ def index
+ @users = User.all(:joins => :comments, :select => "users.*, count(comments.id) as comments_count", :group => "users.id")
+ end
+
+ def show
+ @user = User.find(params[:id])
+ end
+
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ flash[:notice] = "Successfully created user."
+ redirect_to @user
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @user = User.find(params[:id])
+ end
+
+ def update
+ @user = User.find(params[:id])
+ if @user.update_attributes(params[:user])
+ flash[:notice] = "Successfully updated user."
+ redirect_to @user
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @user = User.find(params[:id])
+ @user.destroy
+ flash[:notice] = "Successfully destroyed user."
+ redirect_to users_url
+ 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 CommentsHelper
+end
@@ -0,0 +1,2 @@
+module GroupsHelper
+end
@@ -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 UsersHelper
+end
@@ -0,0 +1,3 @@
+class Comment < ActiveRecord::Base
+ belongs_to :user
+end
@@ -0,0 +1,8 @@
+class Group < ActiveRecord::Base
+ has_many :memberships
+ has_many :users, :through => :memberships
+
+ def comments
+ Comment.scoped(:joins => {:user => :memberships}, :conditions => { :memberships => { :group_id => id } })
+ end
+end
@@ -0,0 +1,4 @@
+class Membership < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :group
+end
@@ -0,0 +1,5 @@
+class User < ActiveRecord::Base
+ has_many :memberships
+ has_many :groups, :through => :memberships
+ has_many :comments
+end
@@ -0,0 +1,7 @@
+<div class="comment">
+ <%= simple_format comment.content %>
+ <p class="actions">
+ <%= link_to "edit", edit_comment_path(comment) %> |
+ <%= link_to "destroy", comment, :method => :delete, :confirm => "Are you sure?" %>
+ </p>
+</div>
@@ -0,0 +1,12 @@
+<% form_for @comment do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :content %><br />
+ <%= f.text_area :content %>
+ </p>
+ <p>
+ <%= f.label :user_id %><br />
+ <%= f.collection_select :user_id, User.all, :id, :name %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Comment" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @comment %> |
+ <%= link_to "View All", comments_path %>
+</p>
@@ -0,0 +1,5 @@
+<% title "Comments" %>
+
+<%= render @comments %>
+
+<p><%= link_to "New Comment", new_comment_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Comment" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", comments_path %></p>
@@ -0,0 +1,16 @@
+<% title "Comment" %>
+
+<p>
+ <strong>Content:</strong>
+ <%=h @comment.content %>
+</p>
+<p>
+ <strong>User:</strong>
+ <%=h @comment.user_id %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_comment_path(@comment) %> |
+ <%= link_to "Destroy", @comment, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", comments_path %>
+</p>
@@ -0,0 +1,8 @@
+<% form_for @group do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Group" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @group %> |
+ <%= link_to "View All", groups_path %>
+</p>
@@ -0,0 +1,17 @@
+<% title "Groups" %>
+
+<table>
+ <tr>
+ <th>Name</th>
+ </tr>
+ <% for group in @groups %>
+ <tr>
+ <td><%=h group.name %></td>
+ <td><%= link_to "Show", group %></td>
+ <td><%= link_to "Edit", edit_group_path(group) %></td>
+ <td><%= link_to "Destroy", group, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Group", new_group_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Group" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", groups_path %></p>
@@ -0,0 +1,18 @@
+<% title @group.name %>
+
+<p>
+ <%= link_to "Edit", edit_group_path(@group) %> |
+ <%= link_to "Destroy", @group, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "See All Groups", groups_path %>
+</p>
+
+<h2>Users</h2>
+<ul>
+<% for user in @group.users %>
+ <li><%=h user.name %></li>
+<% end %>
+</ul>
+
+<h2>Comments</h2>
+
+<%= render @group.comments %>
@@ -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 59c181c

Please sign in to comment.