Permalink
Browse files

adding episode 244

  • Loading branch information...
1 parent 1ce8665 commit a3f934abfe7c22deea0c2be65fb7c8e274e3b857 @ryanb committed Dec 13, 2010
Showing with 10,121 additions and 0 deletions.
  1. +3 −0 episode-244/README
  2. +4 −0 episode-244/grav/.gitignore
  3. +6 −0 episode-244/grav/Gemfile
  4. +78 −0 episode-244/grav/Gemfile.lock
  5. +7 −0 episode-244/grav/Rakefile
  6. +3 −0 episode-244/grav/app/controllers/application_controller.rb
  7. +44 −0 episode-244/grav/app/controllers/users_controller.rb
  8. +11 −0 episode-244/grav/app/helpers/application_helper.rb
  9. +23 −0 episode-244/grav/app/helpers/error_messages_helper.rb
  10. +22 −0 episode-244/grav/app/helpers/layout_helper.rb
  11. +2 −0 episode-244/grav/app/helpers/users_helper.rb
  12. +3 −0 episode-244/grav/app/models/user.rb
  13. +19 −0 episode-244/grav/app/views/layouts/application.html.erb
  14. +12 −0 episode-244/grav/app/views/users/_form.html.erb
  15. +8 −0 episode-244/grav/app/views/users/edit.html.erb
  16. +19 −0 episode-244/grav/app/views/users/index.html.erb
  17. +5 −0 episode-244/grav/app/views/users/new.html.erb
  18. +16 −0 episode-244/grav/app/views/users/show.html.erb
  19. +4 −0 episode-244/grav/config.ru
  20. +42 −0 episode-244/grav/config/application.rb
  21. +13 −0 episode-244/grav/config/boot.rb
  22. +22 −0 episode-244/grav/config/database.yml
  23. +5 −0 episode-244/grav/config/environment.rb
  24. +26 −0 episode-244/grav/config/environments/development.rb
  25. +49 −0 episode-244/grav/config/environments/production.rb
  26. +35 −0 episode-244/grav/config/environments/test.rb
  27. +7 −0 episode-244/grav/config/initializers/backtrace_silencers.rb
  28. +10 −0 episode-244/grav/config/initializers/inflections.rb
  29. +5 −0 episode-244/grav/config/initializers/mime_types.rb
  30. +7 −0 episode-244/grav/config/initializers/secret_token.rb
  31. +8 −0 episode-244/grav/config/initializers/session_store.rb
  32. +5 −0 episode-244/grav/config/locales/en.yml
  33. +4 −0 episode-244/grav/config/routes.rb
  34. +13 −0 episode-244/grav/db/migrate/20101213065609_create_users.rb
  35. +22 −0 episode-244/grav/db/schema.rb
  36. +7 −0 episode-244/grav/db/seeds.rb
  37. +2 −0 episode-244/grav/doc/README_FOR_APP
  38. 0 episode-244/grav/lib/tasks/.gitkeep
  39. +26 −0 episode-244/grav/public/404.html
  40. +26 −0 episode-244/grav/public/422.html
  41. +26 −0 episode-244/grav/public/500.html
  42. 0 episode-244/grav/public/favicon.ico
  43. BIN episode-244/grav/public/images/guest.png
  44. +2 −0 episode-244/grav/public/javascripts/application.js
  45. +965 −0 episode-244/grav/public/javascripts/controls.js
  46. +974 −0 episode-244/grav/public/javascripts/dragdrop.js
  47. +1,123 −0 episode-244/grav/public/javascripts/effects.js
  48. +6,001 −0 episode-244/grav/public/javascripts/prototype.js
  49. +175 −0 episode-244/grav/public/javascripts/rails.js
  50. +5 −0 episode-244/grav/public/robots.txt
  51. 0 episode-244/grav/public/stylesheets/.gitkeep
  52. +75 −0 episode-244/grav/public/stylesheets/application.css
  53. +56 −0 episode-244/grav/public/stylesheets/scaffold.css
  54. +6 −0 episode-244/grav/script/rails
  55. +7 −0 episode-244/grav/test/fixtures/users.yml
  56. +54 −0 episode-244/grav/test/functional/users_controller_test.rb
  57. +9 −0 episode-244/grav/test/performance/browsing_test.rb
  58. +13 −0 episode-244/grav/test/test_helper.rb
  59. +7 −0 episode-244/grav/test/unit/user_test.rb
  60. 0 episode-244/grav/vendor/plugins/.gitkeep
View
@@ -0,0 +1,3 @@
+Railscasts Episode #244: Gravatar
+
+http://railscasts.com/episodes/244
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.3'
+gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
+gem 'nifty-generators'
+gem "mocha", :group => :test
@@ -0,0 +1,78 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.3)
+ actionpack (= 3.0.3)
+ mail (~> 2.2.9)
+ actionpack (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.13)
+ rack-test (~> 0.5.6)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.3)
+ activesupport (= 3.0.3)
+ builder (~> 2.1.2)
+ i18n (~> 0.4)
+ activerecord (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ activesupport (3.0.3)
+ arel (2.0.6)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.5.0)
+ mail (2.2.12)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ mocha (0.9.8)
+ rake
+ nifty-generators (0.4.2)
+ polyglot (0.3.1)
+ rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
+ rack-test (0.5.6)
+ rack (>= 1.0)
+ rails (3.0.3)
+ actionmailer (= 3.0.3)
+ actionpack (= 3.0.3)
+ activerecord (= 3.0.3)
+ activeresource (= 3.0.3)
+ activesupport (= 3.0.3)
+ bundler (~> 1.0)
+ railties (= 3.0.3)
+ railties (3.0.3)
+ actionpack (= 3.0.3)
+ activesupport (= 3.0.3)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ sqlite3-ruby (1.2.5)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.23)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ mocha
+ nifty-generators
+ rails (= 3.0.3)
+ sqlite3-ruby (= 1.2.5)
@@ -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'
+
+Grav::Application.load_tasks
@@ -0,0 +1,3 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+end
@@ -0,0 +1,44 @@
+class UsersController < ApplicationController
+ def index
+ @users = User.all
+ 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,11 @@
+module ApplicationHelper
+ def avatar_url(user)
+ if user.avatar_url.present?
+ user.avatar_url
+ else
+ default_url = "#{root_url}images/guest.png"
+ gravatar_id = Digest::MD5.hexdigest(user.email.downcase)
+ "http://gravatar.com/avatar/#{gravatar_id}.png?s=48&d=#{CGI.escape(default_url)}"
+ end
+ end
+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) { h(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 User < ActiveRecord::Base
+ attr_accessible :email, :avatar_url
+end
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title><%= content_for?(: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,12 @@
+<%= form_for @user do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :email %><br />
+ <%= f.text_field :email %>
+ </p>
+ <p>
+ <%= f.label :avatar_url %><br />
+ <%= f.text_field :avatar_url %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit User" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", @user %> |
+ <%= link_to "View All", users_path %>
+</p>
@@ -0,0 +1,19 @@
+<% title "Users" %>
+
+<table>
+ <tr>
+ <th>Avatar</th>
+ <th>Email</th>
+ </tr>
+ <% for user in @users %>
+ <tr>
+ <td><%= image_tag avatar_url(user) %></td>
+ <td><%= user.email %></td>
+ <td><%= link_to "Show", user %></td>
+ <td><%= link_to "Edit", edit_user_path(user) %></td>
+ <td><%= link_to "Destroy", user, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New User", new_user_path %></p>
@@ -0,0 +1,5 @@
+<% title "New User" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", users_path %></p>
@@ -0,0 +1,16 @@
+<% title "User" %>
+
+<p>
+ <strong>Email:</strong>
+ <%= @user.email %>
+</p>
+<p>
+ <strong>Avatar Url:</strong>
+ <%= @user.avatar_url %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_user_path(@user) %> |
+ <%= link_to "Destroy", @user, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", users_path %>
+</p>
@@ -0,0 +1,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run Grav::Application
@@ -0,0 +1,42 @@
+require File.expand_path('../boot', __FILE__)
+
+require 'rails/all'
+
+# If you have a Gemfile, require the gems listed there, including any gems
+# you've limited to :test, :development, or :production.
+Bundler.require(:default, Rails.env) if defined?(Bundler)
+
+module Grav
+ class Application < Rails::Application
+ # Settings in config/environments/* take precedence over those specified here.
+ # Application configuration should go into files in config/initializers
+ # -- all .rb files in that directory are automatically loaded.
+
+ # Custom directories with classes and modules you want to be autoloadable.
+ # config.autoload_paths += %W(#{config.root}/extras)
+
+ # Only load the plugins named here, in the order given (default is alphabetical).
+ # :all can be used as a placeholder for all plugins not explicitly named.
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
+
+ # Activate observers that should always be running.
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
+ # config.time_zone = 'Central Time (US & Canada)'
+
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
+ # config.i18n.default_locale = :de
+
+ # JavaScript files you want as :defaults (application.js is always included).
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
+
+ # Configure the default encoding used in templates for Ruby 1.9.
+ config.encoding = "utf-8"
+
+ # Configure sensitive parameters which will be filtered from the log file.
+ config.filter_parameters += [:password]
+ end
+end
@@ -0,0 +1,13 @@
+require 'rubygems'
+
+# Set up gems listed in the Gemfile.
+gemfile = File.expand_path('../../Gemfile', __FILE__)
+begin
+ ENV['BUNDLE_GEMFILE'] = gemfile
+ require 'bundler'
+ Bundler.setup
+rescue Bundler::GemNotFound => e
+ STDERR.puts e.message
+ STDERR.puts "Try running `bundle install`."
+ exit!
+end if File.exist?(gemfile)
@@ -0,0 +1,22 @@
+# SQLite version 3.x
+# gem install sqlite3-ruby (not necessary on OS X Leopard)
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+
+# Warning: The database defined as "test" will be erased and
+# re-generated from your development database when you run "rake".
+# Do not set this db to the same as development or production.
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+ pool: 5
+ timeout: 5000
+
+production:
+ adapter: sqlite3
+ database: db/production.sqlite3
+ pool: 5
+ timeout: 5000
@@ -0,0 +1,5 @@
+# Load the rails application
+require File.expand_path('../application', __FILE__)
+
+# Initialize the rails application
+Grav::Application.initialize!
Oops, something went wrong. Retry.

0 comments on commit a3f934a

Please sign in to comment.