Permalink
Browse files

adding episode 189

  • Loading branch information...
1 parent d9caacb commit b9e8c3d75d553de1d0df1a1221be824fc9fe1869 @ryanb committed Nov 23, 2009
Showing with 8,933 additions and 0 deletions.
  1. +9 −0 episode-189/README
  2. +3 −0 episode-189/blog/.gitignore
  3. +4 −0 episode-189/blog/README
  4. +10 −0 episode-189/blog/Rakefile
  5. +16 −0 episode-189/blog/app/controllers/application_controller.rb
  6. +42 −0 episode-189/blog/app/controllers/articles_controller.rb
  7. +34 −0 episode-189/blog/app/controllers/comments_controller.rb
  8. +22 −0 episode-189/blog/app/controllers/user_sessions_controller.rb
  9. +15 −0 episode-189/blog/app/controllers/users_controller.rb
  10. +3 −0 episode-189/blog/app/helpers/application_helper.rb
  11. +2 −0 episode-189/blog/app/helpers/articles_helper.rb
  12. +2 −0 episode-189/blog/app/helpers/comments_helper.rb
  13. +23 −0 episode-189/blog/app/helpers/layout_helper.rb
  14. +2 −0 episode-189/blog/app/helpers/user_sessions_helper.rb
  15. +2 −0 episode-189/blog/app/helpers/users_helper.rb
  16. +5 −0 episode-189/blog/app/models/article.rb
  17. +4 −0 episode-189/blog/app/models/assignment.rb
  18. +5 −0 episode-189/blog/app/models/comment.rb
  19. +4 −0 episode-189/blog/app/models/role.rb
  20. +21 −0 episode-189/blog/app/models/user.rb
  21. +2 −0 episode-189/blog/app/models/user_session.rb
  22. +16 −0 episode-189/blog/app/views/articles/_form.html.erb
  23. +8 −0 episode-189/blog/app/views/articles/edit.html.erb
  24. +16 −0 episode-189/blog/app/views/articles/index.html.erb
  25. +5 −0 episode-189/blog/app/views/articles/new.html.erb
  26. +42 −0 episode-189/blog/app/views/articles/show.html.erb
  27. +17 −0 episode-189/blog/app/views/comments/_form.html.erb
  28. +4 −0 episode-189/blog/app/views/comments/edit.html.erb
  29. +4 −0 episode-189/blog/app/views/comments/new.html.erb
  30. +31 −0 episode-189/blog/app/views/layouts/application.html.erb
  31. +16 −0 episode-189/blog/app/views/user_sessions/new.html.erb
  32. +32 −0 episode-189/blog/app/views/users/new.html.erb
  33. +26 −0 episode-189/blog/config/authorization_rules.rb
  34. +110 −0 episode-189/blog/config/boot.rb
  35. +19 −0 episode-189/blog/config/database.yml
  36. +69 −0 episode-189/blog/config/environment.rb
  37. +17 −0 episode-189/blog/config/environments/development.rb
  38. +22 −0 episode-189/blog/config/environments/production.rb
  39. +22 −0 episode-189/blog/config/environments/test.rb
  40. +10 −0 episode-189/blog/config/initializers/inflections.rb
  41. +5 −0 episode-189/blog/config/initializers/mime_types.rb
  42. +15 −0 episode-189/blog/config/initializers/new_rails_defaults.rb
  43. +11 −0 episode-189/blog/config/routes.rb
  44. +14 −0 episode-189/blog/db/migrate/20080719200447_create_articles.rb
  45. +15 −0 episode-189/blog/db/migrate/20080719201435_create_comments.rb
  46. +16 −0 episode-189/blog/db/migrate/20091116005037_create_users.rb
  47. +12 −0 episode-189/blog/db/migrate/20091116011510_create_roles.rb
  48. +14 −0 episode-189/blog/db/migrate/20091116072645_create_assignments.rb
  49. +11 −0 episode-189/blog/db/migrate/20091116073210_add_user_to_articles.rb
  50. +9 −0 episode-189/blog/db/migrate/20091123010658_add_role_to_users.rb
  51. +9 −0 episode-189/blog/db/migrate/20091123013022_add_roles_mask_to_users.rb
  52. +58 −0 episode-189/blog/db/schema.rb
  53. +2 −0 episode-189/blog/doc/README_FOR_APP
  54. +55 −0 episode-189/blog/lib/authentication.rb
  55. +2 −0 episode-189/blog/lib/tasks/application.rake
  56. +30 −0 episode-189/blog/public/404.html
  57. +30 −0 episode-189/blog/public/422.html
  58. +30 −0 episode-189/blog/public/500.html
  59. +10 −0 episode-189/blog/public/dispatch.cgi
  60. +24 −0 episode-189/blog/public/dispatch.fcgi
  61. +10 −0 episode-189/blog/public/dispatch.rb
  62. 0 episode-189/blog/public/favicon.ico
  63. +2 −0 episode-189/blog/public/javascripts/application.js
  64. +963 −0 episode-189/blog/public/javascripts/controls.js
  65. +973 −0 episode-189/blog/public/javascripts/dragdrop.js
  66. +1,128 −0 episode-189/blog/public/javascripts/effects.js
  67. +4,320 −0 episode-189/blog/public/javascripts/prototype.js
  68. +5 −0 episode-189/blog/public/robots.txt
  69. +118 −0 episode-189/blog/public/stylesheets/application.css
  70. +3 −0 episode-189/blog/script/about
  71. +3 −0 episode-189/blog/script/console
  72. +3 −0 episode-189/blog/script/dbconsole
  73. +3 −0 episode-189/blog/script/destroy
  74. +3 −0 episode-189/blog/script/generate
  75. +3 −0 episode-189/blog/script/performance/benchmarker
  76. +3 −0 episode-189/blog/script/performance/profiler
  77. +3 −0 episode-189/blog/script/performance/request
  78. +3 −0 episode-189/blog/script/plugin
  79. +3 −0 episode-189/blog/script/process/inspector
  80. +3 −0 episode-189/blog/script/process/reaper
  81. +3 −0 episode-189/blog/script/process/spawner
  82. +3 −0 episode-189/blog/script/runner
  83. +3 −0 episode-189/blog/script/server
  84. +17 −0 episode-189/blog/test/fixtures/articles.yml
  85. +9 −0 episode-189/blog/test/fixtures/assignments.yml
  86. +21 −0 episode-189/blog/test/fixtures/comments.yml
  87. +11 −0 episode-189/blog/test/fixtures/roles.yml
  88. +14 −0 episode-189/blog/test/fixtures/users.yml
  89. +54 −0 episode-189/blog/test/functional/articles_controller_test.rb
  90. +44 −0 episode-189/blog/test/functional/comments_controller_test.rb
  91. +20 −0 episode-189/blog/test/functional/user_sessions_controller_test.rb
  92. +20 −0 episode-189/blog/test/functional/users_controller_test.rb
  93. +38 −0 episode-189/blog/test/test_helper.rb
  94. +8 −0 episode-189/blog/test/unit/article_test.rb
  95. +8 −0 episode-189/blog/test/unit/assignment_test.rb
  96. +8 −0 episode-189/blog/test/unit/comment_test.rb
  97. +8 −0 episode-189/blog/test/unit/role_test.rb
  98. +4 −0 episode-189/blog/test/unit/user_test.rb
View
@@ -0,0 +1,9 @@
+Railscasts Episode #189: Embedded Association
+
+http://railscasts.com/episodes/189
+
+Commands
+
+ script/generate migration add_role_to_users role:string
+ script/generate migration add_roles_mask_to_users roles_mask:integer
+ rake db:migrate
@@ -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,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
+ protect_from_forgery
+ before_filter { |c| Authorization.current_user = c.current_user }
+
+ protected
+
+ def permission_denied
+ flash[:error] = "Sorry, you are not allowed to access that page."
+ redirect_to root_url
+ end
+end
@@ -0,0 +1,42 @@
+class ArticlesController < ApplicationController
+ filter_resource_access
+
+ def index
+ @articles = Article.find(:all)
+ end
+
+ def show
+ @comment = Comment.new(:article => @article)
+ end
+
+ def new
+ end
+
+ def create
+ @article.user = current_user
+ if @article.save
+ flash[:notice] = "Successfully created article."
+ redirect_to @article
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ end
+
+ def update
+ if @article.update_attributes(params[:article])
+ flash[:notice] = "Successfully updated article."
+ redirect_to @article
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @article.destroy
+ flash[:notice] = "Successfully destroyed article."
+ redirect_to articles_url
+ end
+end
@@ -0,0 +1,34 @@
+class CommentsController < ApplicationController
+ filter_resource_access
+
+ def new
+ end
+
+ def create
+ @comment.user = current_user
+ if @comment.save
+ flash[:notice] = "Successfully created comment."
+ redirect_to article_url(@comment.article_id)
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ end
+
+ def update
+ 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.destroy
+ flash[:notice] = "Successfully destroyed comment."
+ redirect_to article_url(@comment.article_id)
+ end
+end
@@ -0,0 +1,22 @@
+class UserSessionsController < ApplicationController
+ def new
+ @user_session = UserSession.new
+ end
+
+ def create
+ @user_session = UserSession.new(params[:user_session])
+ if @user_session.save
+ flash[:notice] = "Logged in successfully."
+ redirect_to_target_or_default(root_url)
+ else
+ render :action => 'new'
+ end
+ end
+
+ def destroy
+ @user_session = UserSession.find
+ @user_session.destroy
+ flash[:notice] = "You have been logged out."
+ redirect_to root_url
+ end
+end
@@ -0,0 +1,15 @@
+class UsersController < ApplicationController
+ def new
+ @user = User.new
+ end
+
+ def create
+ @user = User.new(params[:user])
+ if @user.save
+ 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 UserSessionsHelper
+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, :content
+ belongs_to :user
+end
@@ -0,0 +1,4 @@
+class Assignment < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :role
+end
@@ -0,0 +1,5 @@
+class Comment < ActiveRecord::Base
+ belongs_to :article
+ belongs_to :user
+ validates_presence_of :author_name, :content
+end
@@ -0,0 +1,4 @@
+class Role < ActiveRecord::Base
+ has_many :assignments
+ has_many :users, :through => :assignments
+end
@@ -0,0 +1,21 @@
+class User < ActiveRecord::Base
+ acts_as_authentic
+ has_many :articles
+ has_many :comments
+
+ named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0"} }
+
+ ROLES = %w[admin moderator author]
+
+ def roles=(roles)
+ self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
+ end
+
+ def roles
+ ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
+ end
+
+ def role_symbols
+ roles.map(&:to_sym)
+ end
+end
@@ -0,0 +1,2 @@
+class UserSession < Authlogic::Session::Base
+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,16 @@
+<% 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>
+
+<% if permitted_to? :create, Article.new %>
+ <p><%= link_to "New Article", new_article_path %></p>
+<% end %>
@@ -0,0 +1,5 @@
+<% title "New Article" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", articles_path %></p>
@@ -0,0 +1,42 @@
+<% title @article.name %>
+
+<p class="author"><em>from <%=h @article.author_name %></em></p>
+
+<%= simple_format @article.content %>
+
+<p>
+ <% if permitted_to? :edit, @article %>
+ <%= link_to "Edit", edit_article_path(@article) %> |
+ <% end %>
+ <% if permitted_to? :destroy, @article %>
+ <%= link_to "Destroy", @article, :method => :delete, :confirm => "Are you sure?" %> |
+ <% end %>
+ <%= 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 %>
+ <p>
+ <% if permitted_to? :edit, comment %>
+ <%= link_to "Edit", edit_comment_path(comment) %>
+ <% end %>
+ <% if permitted_to? :destroy, comment %>
+ | <%= link_to "Destroy", comment, :method => :delete, :confirm => "Are you sure?" %>
+ <% end %>
+ </p>
+ </div>
+ <% end %>
+ </div>
+<% end %>
+
+<% if permitted_to? :create, @comment %>
+ <h3>Add your comment:</h3>
+ <%= render :partial => 'comments/form' %>
+<% end %>
@@ -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' %>
+
Oops, something went wrong. Retry.

0 comments on commit b9e8c3d

Please sign in to comment.