Permalink
Browse files

adding episode 147

  • Loading branch information...
1 parent f676963 commit 37c4e24c96118646af6e67c5f5b690cd32c30606 @ryanb ryanb committed Jan 29, 2009
Showing with 8,944 additions and 0 deletions.
  1. +11 −0 episode-147/README
  2. +3 −0 episode-147/faqapp/.gitignore
  3. +10 −0 episode-147/faqapp/Rakefile
  4. +15 −0 episode-147/faqapp/app/controllers/application.rb
  5. +51 −0 episode-147/faqapp/app/controllers/faqs_controller.rb
  6. +3 −0 episode-147/faqapp/app/helpers/application_helper.rb
  7. +2 −0 episode-147/faqapp/app/helpers/faqs_helper.rb
  8. +23 −0 episode-147/faqapp/app/helpers/layout_helper.rb
  9. +3 −0 episode-147/faqapp/app/models/faq.rb
  10. +12 −0 episode-147/faqapp/app/views/faqs/_form.html.erb
  11. +8 −0 episode-147/faqapp/app/views/faqs/edit.html.erb
  12. +13 −0 episode-147/faqapp/app/views/faqs/index.html.erb
  13. +5 −0 episode-147/faqapp/app/views/faqs/new.html.erb
  14. +16 −0 episode-147/faqapp/app/views/faqs/show.html.erb
  15. +23 −0 episode-147/faqapp/app/views/layouts/application.html.erb
  16. +109 −0 episode-147/faqapp/config/boot.rb
  17. +22 −0 episode-147/faqapp/config/database.yml
  18. +75 −0 episode-147/faqapp/config/environment.rb
  19. +17 −0 episode-147/faqapp/config/environments/development.rb
  20. +24 −0 episode-147/faqapp/config/environments/production.rb
  21. +22 −0 episode-147/faqapp/config/environments/test.rb
  22. +10 −0 episode-147/faqapp/config/initializers/inflections.rb
  23. +5 −0 episode-147/faqapp/config/initializers/mime_types.rb
  24. +17 −0 episode-147/faqapp/config/initializers/new_rails_defaults.rb
  25. +5 −0 episode-147/faqapp/config/locales/en.yml
  26. +4 −0 episode-147/faqapp/config/routes.rb
  27. +13 −0 episode-147/faqapp/db/migrate/20090128215955_create_faqs.rb
  28. +9 −0 episode-147/faqapp/db/migrate/20090128235020_add_position_to_faqs.rb
  29. +22 −0 episode-147/faqapp/db/schema.rb
  30. +5 −0 episode-147/faqapp/doc/README_FOR_APP
  31. +30 −0 episode-147/faqapp/public/404.html
  32. +30 −0 episode-147/faqapp/public/422.html
  33. +33 −0 episode-147/faqapp/public/500.html
  34. +10 −0 episode-147/faqapp/public/dispatch.cgi
  35. +24 −0 episode-147/faqapp/public/dispatch.fcgi
  36. +10 −0 episode-147/faqapp/public/dispatch.rb
  37. 0 episode-147/faqapp/public/favicon.ico
  38. BIN episode-147/faqapp/public/images/rails.png
  39. +2 −0 episode-147/faqapp/public/javascripts/application.js
  40. +963 −0 episode-147/faqapp/public/javascripts/controls.js
  41. +973 −0 episode-147/faqapp/public/javascripts/dragdrop.js
  42. +1,128 −0 episode-147/faqapp/public/javascripts/effects.js
  43. +4,320 −0 episode-147/faqapp/public/javascripts/prototype.js
  44. +5 −0 episode-147/faqapp/public/robots.txt
  45. +87 −0 episode-147/faqapp/public/stylesheets/application.css
  46. +4 −0 episode-147/faqapp/script/about
  47. +3 −0 episode-147/faqapp/script/console
  48. +3 −0 episode-147/faqapp/script/dbconsole
  49. +3 −0 episode-147/faqapp/script/destroy
  50. +3 −0 episode-147/faqapp/script/generate
  51. +3 −0 episode-147/faqapp/script/performance/benchmarker
  52. +3 −0 episode-147/faqapp/script/performance/profiler
  53. +3 −0 episode-147/faqapp/script/performance/request
  54. +3 −0 episode-147/faqapp/script/plugin
  55. +3 −0 episode-147/faqapp/script/process/inspector
  56. +3 −0 episode-147/faqapp/script/process/reaper
  57. +3 −0 episode-147/faqapp/script/process/spawner
  58. +3 −0 episode-147/faqapp/script/runner
  59. +3 −0 episode-147/faqapp/script/server
  60. +7 −0 episode-147/faqapp/test/fixtures/faqs.yml
  61. +54 −0 episode-147/faqapp/test/functional/faqs_controller_test.rb
  62. +9 −0 episode-147/faqapp/test/performance/browsing_test.rb
  63. +38 −0 episode-147/faqapp/test/test_helper.rb
  64. +7 −0 episode-147/faqapp/test/unit/faq_test.rb
  65. +23 −0 episode-147/faqapp/vendor/plugins/acts_as_list/README
  66. +3 −0 episode-147/faqapp/vendor/plugins/acts_as_list/init.rb
  67. +256 −0 episode-147/faqapp/vendor/plugins/acts_as_list/lib/active_record/acts/list.rb
  68. +332 −0 episode-147/faqapp/vendor/plugins/acts_as_list/test/list_test.rb
View
@@ -0,0 +1,11 @@
+Railscasts Episode #147: Sortable Lists
+
+http://railscasts.com/episodes/147
+
+Commands
+
+ script/generate migration add_position_to_faqs position:integer
+
+ rake db:migrate
+
+ script/plugin install git://github.com/rails/acts_as_list.git
@@ -0,0 +1,3 @@
+tmp/*
+log/*
+*.sqlite3
@@ -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,15 @@
+# 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 => '072a094e1de988302438dea2c86d5874'
+
+ # 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
+end
@@ -0,0 +1,51 @@
+class FaqsController < ApplicationController
+ def index
+ @faqs = Faq.all(:order => "position")
+ end
+
+ def show
+ @faq = Faq.find(params[:id])
+ end
+
+ def new
+ @faq = Faq.new
+ end
+
+ def create
+ @faq = Faq.new(params[:faq])
+ if @faq.save
+ flash[:notice] = "Successfully created FAQ."
+ redirect_to @faq
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @faq = Faq.find(params[:id])
+ end
+
+ def update
+ @faq = Faq.find(params[:id])
+ if @faq.update_attributes(params[:faq])
+ flash[:notice] = "Successfully updated FAQ."
+ redirect_to @faq
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @faq = Faq.find(params[:id])
+ @faq.destroy
+ flash[:notice] = "Successfully destroyed FAQ."
+ redirect_to faqs_url
+ end
+
+ def sort
+ params[:faqs].each_with_index do |id, index|
+ Faq.update_all(['position=?', index+1], ['id=?', id])
+ end
+ render :nothing => true
+ 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 FaqsHelper
+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,3 @@
+class Faq < ActiveRecord::Base
+ acts_as_list
+end
@@ -0,0 +1,12 @@
+<% form_for @faq do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :question %><br />
+ <%= f.text_area :question %>
+ </p>
+ <p>
+ <%= f.label :answer %><br />
+ <%= f.text_area :answer %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Faq" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @faq %> |
+ <%= link_to "View All", faqs_path %>
+</p>
@@ -0,0 +1,13 @@
+<% title "FAQs" %>
+
+<ul id="faqs">
+<% for faq in @faqs %>
+ <% content_tag_for :li, faq do %>
+ <span class="handle">[drag]</span>
+ <%= link_to h(faq.question), faq %>
+ <% end %>
+<% end %>
+</ul>
+<%= sortable_element("faqs", :url => sort_faqs_path, :handle => "handle") %>
+
+<p><%= link_to "New FAQ", new_faq_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Faq" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", faqs_path %></p>
@@ -0,0 +1,16 @@
+<% title "FAQ" %>
+
+<p>
+ <strong>Question:</strong>
+ <%=h @faq.question %>
+</p>
+<p>
+ <strong>Answer:</strong>
+ <%=h @faq.answer %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_faq_path(@faq) %> |
+ <%= link_to "Destroy", @faq, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", faqs_path %>
+</p>
@@ -0,0 +1,23 @@
+<!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' %>
+ <%= javascript_include_tag :defaults %>
+ <%= 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>
@@ -0,0 +1,109 @@
+# Don't change this file!
+# Configure your app in config/environment.rb and config/environments/*.rb
+
+RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
+
+module Rails
+ class << self
+ def boot!
+ unless booted?
+ preinitialize
+ pick_boot.run
+ end
+ end
+
+ def booted?
+ defined? Rails::Initializer
+ end
+
+ def pick_boot
+ (vendor_rails? ? VendorBoot : GemBoot).new
+ end
+
+ def vendor_rails?
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
+ end
+
+ def preinitialize
+ load(preinitializer_path) if File.exist?(preinitializer_path)
+ end
+
+ def preinitializer_path
+ "#{RAILS_ROOT}/config/preinitializer.rb"
+ end
+ end
+
+ class Boot
+ def run
+ load_initializer
+ Rails::Initializer.run(:set_load_path)
+ end
+ end
+
+ class VendorBoot < Boot
+ def load_initializer
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
+ Rails::Initializer.run(:install_gem_spec_stubs)
+ end
+ end
+
+ class GemBoot < Boot
+ def load_initializer
+ self.class.load_rubygems
+ load_rails_gem
+ require 'initializer'
+ end
+
+ def load_rails_gem
+ if version = self.class.gem_version
+ gem 'rails', version
+ else
+ gem 'rails'
+ end
+ rescue Gem::LoadError => load_error
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
+ exit 1
+ end
+
+ class << self
+ def rubygems_version
+ Gem::RubyGemsVersion rescue nil
+ end
+
+ def gem_version
+ if defined? RAILS_GEM_VERSION
+ RAILS_GEM_VERSION
+ elsif ENV.include?('RAILS_GEM_VERSION')
+ ENV['RAILS_GEM_VERSION']
+ else
+ parse_gem_version(read_environment_rb)
+ end
+ end
+
+ def load_rubygems
+ require 'rubygems'
+ min_version = '1.3.1'
+ unless rubygems_version >= min_version
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
+ exit 1
+ end
+
+ rescue LoadError
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
+ exit 1
+ end
+
+ def parse_gem_version(text)
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
+ end
+
+ private
+ def read_environment_rb
+ File.read("#{RAILS_ROOT}/config/environment.rb")
+ end
+ end
+ end
+end
+
+# All that for this:
+Rails.boot!
@@ -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
Oops, something went wrong.

0 comments on commit 37c4e24

Please sign in to comment.