Permalink
Browse files

adding episode 196

  • Loading branch information...
1 parent 4201617 commit 87d2559b7002a16297eb168819161e45b7cbd15c @ryanb committed Jan 10, 2010
Showing with 8,365 additions and 0 deletions.
  1. +12 −0 episode-196/README
  2. +3 −0 episode-196/surveysays/.gitignore
  3. +4 −0 episode-196/surveysays/README
  4. +10 −0 episode-196/surveysays/Rakefile
  5. +10 −0 episode-196/surveysays/app/controllers/application_controller.rb
  6. +48 −0 episode-196/surveysays/app/controllers/surveys_controller.rb
  7. +3 −0 episode-196/surveysays/app/helpers/application_helper.rb
  8. +22 −0 episode-196/surveysays/app/helpers/layout_helper.rb
  9. +2 −0 episode-196/surveysays/app/helpers/surveys_helper.rb
  10. +3 −0 episode-196/surveysays/app/models/answer.rb
  11. +5 −0 episode-196/surveysays/app/models/question.rb
  12. +4 −0 episode-196/surveysays/app/models/survey.rb
  13. +22 −0 episode-196/surveysays/app/views/layouts/application.html.erb
  14. +6 −0 episode-196/surveysays/app/views/surveys/_answer_fields.html.erb
  15. +11 −0 episode-196/surveysays/app/views/surveys/_form.html.erb
  16. +9 −0 episode-196/surveysays/app/views/surveys/_question_fields.html.erb
  17. +8 −0 episode-196/surveysays/app/views/surveys/edit.html.erb
  18. +17 −0 episode-196/surveysays/app/views/surveys/index.html.erb
  19. +5 −0 episode-196/surveysays/app/views/surveys/new.html.erb
  20. +25 −0 episode-196/surveysays/app/views/surveys/show.html.erb
  21. +110 −0 episode-196/surveysays/config/boot.rb
  22. +22 −0 episode-196/surveysays/config/database.yml
  23. +41 −0 episode-196/surveysays/config/environment.rb
  24. +17 −0 episode-196/surveysays/config/environments/development.rb
  25. +28 −0 episode-196/surveysays/config/environments/production.rb
  26. +28 −0 episode-196/surveysays/config/environments/test.rb
  27. +7 −0 episode-196/surveysays/config/initializers/backtrace_silencers.rb
  28. +10 −0 episode-196/surveysays/config/initializers/inflections.rb
  29. +5 −0 episode-196/surveysays/config/initializers/mime_types.rb
  30. +21 −0 episode-196/surveysays/config/initializers/new_rails_defaults.rb
  31. +15 −0 episode-196/surveysays/config/initializers/session_store.rb
  32. +5 −0 episode-196/surveysays/config/locales/en.yml
  33. +4 −0 episode-196/surveysays/config/routes.rb
  34. +12 −0 episode-196/surveysays/db/migrate/20100109230308_create_surveys.rb
  35. +14 −0 episode-196/surveysays/db/migrate/20100109231422_create_questions.rb
  36. +14 −0 episode-196/surveysays/db/migrate/20100109235729_create_answers.rb
  37. +34 −0 episode-196/surveysays/db/schema.rb
  38. +7 −0 episode-196/surveysays/db/seeds.rb
  39. +2 −0 episode-196/surveysays/doc/README_FOR_APP
  40. 0 episode-196/surveysays/log/.gitkeep
  41. +30 −0 episode-196/surveysays/public/404.html
  42. +30 −0 episode-196/surveysays/public/422.html
  43. +30 −0 episode-196/surveysays/public/500.html
  44. 0 episode-196/surveysays/public/favicon.ico
  45. BIN episode-196/surveysays/public/images/rails.png
  46. +2 −0 episode-196/surveysays/public/javascripts/application.js
  47. +963 −0 episode-196/surveysays/public/javascripts/controls.js
  48. +973 −0 episode-196/surveysays/public/javascripts/dragdrop.js
  49. +1,128 −0 episode-196/surveysays/public/javascripts/effects.js
  50. +4,320 −0 episode-196/surveysays/public/javascripts/prototype.js
  51. +5 −0 episode-196/surveysays/public/robots.txt
  52. +81 −0 episode-196/surveysays/public/stylesheets/application.css
  53. +4 −0 episode-196/surveysays/script/about
  54. +3 −0 episode-196/surveysays/script/console
  55. +3 −0 episode-196/surveysays/script/dbconsole
  56. +3 −0 episode-196/surveysays/script/destroy
  57. +3 −0 episode-196/surveysays/script/generate
  58. +3 −0 episode-196/surveysays/script/performance/benchmarker
  59. +3 −0 episode-196/surveysays/script/performance/profiler
  60. +3 −0 episode-196/surveysays/script/plugin
  61. +3 −0 episode-196/surveysays/script/runner
  62. +3 −0 episode-196/surveysays/script/server
  63. +9 −0 episode-196/surveysays/test/fixtures/answers.yml
  64. +9 −0 episode-196/surveysays/test/fixtures/questions.yml
  65. +5 −0 episode-196/surveysays/test/fixtures/surveys.yml
  66. +54 −0 episode-196/surveysays/test/functional/surveys_controller_test.rb
  67. +9 −0 episode-196/surveysays/test/performance/browsing_test.rb
  68. +38 −0 episode-196/surveysays/test/test_helper.rb
  69. +8 −0 episode-196/surveysays/test/unit/answer_test.rb
  70. +8 −0 episode-196/surveysays/test/unit/question_test.rb
  71. +7 −0 episode-196/surveysays/test/unit/survey_test.rb
  72. 0 episode-196/surveysays/tmp/.gitkeep
View
@@ -0,0 +1,12 @@
+Railscasts Episode #196: Nested Fields Part 1
+
+http://railscasts.com/episodes/196
+
+Commands
+
+ rails surveysays
+ script/generate nifty_layout
+ script/generate nifty_scaffold survey name:string
+ script/generate model question survey_id:integer content:text
+ script/generate model answer question_id:integer content:string
+ rake db:migrate
@@ -0,0 +1,3 @@
+tmp/**/*
+log/*.log
+*.sqlite3
@@ -0,0 +1,4 @@
+Example application for Railscasts. To get started:
+
+ rake db:migrate
+ script/server
@@ -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,48 @@
+class SurveysController < ApplicationController
+ def index
+ @surveys = Survey.all
+ end
+
+ def show
+ @survey = Survey.find(params[:id])
+ end
+
+ def new
+ @survey = Survey.new
+ 3.times do
+ question = @survey.questions.build
+ 4.times { question.answers.build }
+ end
+ end
+
+ def create
+ @survey = Survey.new(params[:survey])
+ if @survey.save
+ flash[:notice] = "Successfully created survey."
+ redirect_to @survey
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @survey = Survey.find(params[:id])
+ end
+
+ def update
+ @survey = Survey.find(params[:id])
+ if @survey.update_attributes(params[:survey])
+ flash[:notice] = "Successfully updated survey."
+ redirect_to @survey
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @survey = Survey.find(params[:id])
+ @survey.destroy
+ flash[:notice] = "Successfully destroyed survey."
+ redirect_to surveys_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,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 SurveysHelper
+end
@@ -0,0 +1,3 @@
+class Answer < ActiveRecord::Base
+ belongs_to :question
+end
@@ -0,0 +1,5 @@
+class Question < ActiveRecord::Base
+ belongs_to :survey
+ has_many :answers, :dependent => :destroy
+ accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
+end
@@ -0,0 +1,4 @@
+class Survey < ActiveRecord::Base
+ has_many :questions, :dependent => :destroy
+ accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
+end
@@ -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>
@@ -0,0 +1,6 @@
+<p>
+ <%= f.label :content, "Answer" %>
+ <%= f.text_field :content %>
+ <%= f.check_box :_destroy %>
+ <%= f.label :_destroy, "Remove" %>
+</p>
@@ -0,0 +1,11 @@
+<% form_for @survey do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <% f.fields_for :questions do |builder| %>
+ <%= render "question_fields", :f => builder %>
+ <% end %>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,9 @@
+<p>
+ <%= f.label :content, "Question" %><br />
+ <%= f.text_area :content, :rows => 3 %><br />
+ <%= f.check_box :_destroy %>
+ <%= f.label :_destroy, "Remove Question" %>
+</p>
+<% f.fields_for :answers do |builder| %>
+ <%= render 'answer_fields', :f => builder %>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Survey" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @survey %> |
+ <%= link_to "View All", surveys_path %>
+</p>
@@ -0,0 +1,17 @@
+<% title "Surveys" %>
+
+<table>
+ <tr>
+ <th>Name</th>
+ </tr>
+ <% for survey in @surveys %>
+ <tr>
+ <td><%=h survey.name %></td>
+ <td><%= link_to "Show", survey %></td>
+ <td><%= link_to "Edit", edit_survey_path(survey) %></td>
+ <td><%= link_to "Destroy", survey, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Survey", new_survey_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Survey" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", surveys_path %></p>
@@ -0,0 +1,25 @@
+<% title "Survey" %>
+
+<p>
+ <strong>Name:</strong>
+ <%=h @survey.name %>
+</p>
+
+<ol>
+<% for question in @survey.questions %>
+ <li>
+ <%=h question.content %>
+ <ul>
+ <% for answer in question.answers %>
+ <li><%=h answer.content %></li>
+ <% end %>
+ </ul>
+ </li>
+<% end %>
+</ol>
+
+<p>
+ <%= link_to "Edit", edit_survey_path(@survey) %> |
+ <%= link_to "Destroy", @survey, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", surveys_path %>
+</p>
@@ -0,0 +1,110 @@
+# 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)
+ Rails::GemDependency.add_frozen_gem_path
+ 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
+ min_version = '1.3.2'
+ require 'rubygems'
+ 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 87d2559

Please sign in to comment.