Permalink
Browse files

adding Rails 3 app based on Railscasts episodes 196 and 197

  • Loading branch information...
1 parent df979cc commit 14f98e905073bde1f18707278116b94e42cb3c3a @ryanb committed Feb 15, 2011
Showing with 10,200 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +1 −0 .rvmrc
  3. +6 −0 Gemfile
  4. +77 −0 Gemfile.lock
  5. +5 −0 README.md
  6. +7 −0 Rakefile
  7. +3 −0 app/controllers/application_controller.rb
  8. +45 −0 app/controllers/surveys_controller.rb
  9. +13 −0 app/helpers/application_helper.rb
  10. +23 −0 app/helpers/error_messages_helper.rb
  11. +22 −0 app/helpers/layout_helper.rb
  12. +2 −0 app/helpers/surveys_helper.rb
  13. +3 −0 app/models/answer.rb
  14. +5 −0 app/models/question.rb
  15. +4 −0 app/models/survey.rb
  16. +19 −0 app/views/layouts/application.html.erb
  17. +9 −0 app/views/surveys/_answer_fields.html.erb
  18. +12 −0 app/views/surveys/_form.html.erb
  19. +13 −0 app/views/surveys/_question_fields.html.erb
  20. +8 −0 app/views/surveys/edit.html.erb
  21. +17 −0 app/views/surveys/index.html.erb
  22. +5 −0 app/views/surveys/new.html.erb
  23. +25 −0 app/views/surveys/show.html.erb
  24. +4 −0 config.ru
  25. +42 −0 config/application.rb
  26. +6 −0 config/boot.rb
  27. +22 −0 config/database.yml
  28. +5 −0 config/environment.rb
  29. +26 −0 config/environments/development.rb
  30. +49 −0 config/environments/production.rb
  31. +35 −0 config/environments/test.rb
  32. +7 −0 config/initializers/backtrace_silencers.rb
  33. +10 −0 config/initializers/inflections.rb
  34. +5 −0 config/initializers/mime_types.rb
  35. +7 −0 config/initializers/secret_token.rb
  36. +8 −0 config/initializers/session_store.rb
  37. +5 −0 config/locales/en.yml
  38. +4 −0 config/routes.rb
  39. +12 −0 db/migrate/20110215195058_create_surveys.rb
  40. +14 −0 db/migrate/20110215195147_create_questions.rb
  41. +14 −0 db/migrate/20110215195246_create_answers.rb
  42. +35 −0 db/schema.rb
  43. +7 −0 db/seeds.rb
  44. +2 −0 doc/README_FOR_APP
  45. 0 lib/tasks/.gitkeep
  46. +26 −0 public/404.html
  47. +26 −0 public/422.html
  48. +26 −0 public/500.html
  49. 0 public/favicon.ico
  50. BIN public/images/rails.png
  51. +13 −0 public/javascripts/application.js
  52. +965 −0 public/javascripts/controls.js
  53. +974 −0 public/javascripts/dragdrop.js
  54. +1,123 −0 public/javascripts/effects.js
  55. +6,001 −0 public/javascripts/prototype.js
  56. +191 −0 public/javascripts/rails.js
  57. +5 −0 public/robots.txt
  58. 0 public/stylesheets/.gitkeep
  59. +75 −0 public/stylesheets/application.css
  60. +6 −0 script/rails
  61. +9 −0 test/fixtures/answers.yml
  62. +9 −0 test/fixtures/questions.yml
  63. +5 −0 test/fixtures/surveys.yml
  64. +54 −0 test/functional/surveys_controller_test.rb
  65. +9 −0 test/performance/browsing_test.rb
  66. +13 −0 test/test_helper.rb
  67. +8 −0 test/unit/answer_test.rb
  68. +8 −0 test/unit/question_test.rb
  69. +7 −0 test/unit/survey_test.rb
  70. 0 vendor/plugins/.gitkeep
View
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm 1.9.2@complex-form-examples --create
View
@@ -0,0 +1,6 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.4'
+gem 'sqlite3'
+gem 'nifty-generators'
+gem "mocha", :group => :test
View
@@ -0,0 +1,77 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.4)
+ actionpack (= 3.0.4)
+ mail (~> 2.2.15)
+ actionpack (3.0.4)
+ activemodel (= 3.0.4)
+ activesupport (= 3.0.4)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.13)
+ rack-test (~> 0.5.7)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.4)
+ activesupport (= 3.0.4)
+ builder (~> 2.1.2)
+ i18n (~> 0.4)
+ activerecord (3.0.4)
+ activemodel (= 3.0.4)
+ activesupport (= 3.0.4)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.4)
+ activemodel (= 3.0.4)
+ activesupport (= 3.0.4)
+ activesupport (3.0.4)
+ arel (2.0.8)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.5.0)
+ mail (2.2.15)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ mocha (0.9.12)
+ nifty-generators (0.4.5)
+ polyglot (0.3.1)
+ rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ rails (3.0.4)
+ actionmailer (= 3.0.4)
+ actionpack (= 3.0.4)
+ activerecord (= 3.0.4)
+ activeresource (= 3.0.4)
+ activesupport (= 3.0.4)
+ bundler (~> 1.0)
+ railties (= 3.0.4)
+ railties (3.0.4)
+ actionpack (= 3.0.4)
+ activesupport (= 3.0.4)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ sqlite3 (1.3.3)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.24)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ mocha
+ nifty-generators
+ rails (= 3.0.4)
+ sqlite3
View
@@ -0,0 +1,5 @@
+# Complex Form Examples
+
+This repository currently contains one Rails 3 example of a complex nested model form which is based off of Railscasts episodes 196 & 197. Expect more variations to come.
+
+If you want to see the Rails 2 examples, check out the "rails2" branch.
View
@@ -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'
+
+Surveyor::Application.load_tasks
@@ -0,0 +1,3 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+end
@@ -0,0 +1,45 @@
+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
+ redirect_to @survey, :notice => "Successfully created 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])
+ redirect_to @survey, :notice => "Successfully updated survey."
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @survey = Survey.find(params[:id])
+ @survey.destroy
+ redirect_to surveys_url, :notice => "Successfully destroyed survey."
+ end
+end
@@ -0,0 +1,13 @@
+module ApplicationHelper
+ def link_to_remove_fields(name, f)
+ f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
+ end
+
+ def link_to_add_fields(name, f, association)
+ new_object = f.object.class.reflect_on_association(association).klass.new
+ fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
+ render(association.to_s.singularize + "_fields", :f => builder)
+ end
+ link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")
+ 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] ||= I18n.t(:"activerecord.errors.header", :default => "Invalid Fields")
+ options[:message] ||= I18n.t(:"activerecord.errors.message", :default => "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 SurveysHelper
+end
View
@@ -0,0 +1,3 @@
+class Answer < ActiveRecord::Base
+ belongs_to :question
+end
View
@@ -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
View
@@ -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,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,9 @@
+<div class="fields">
+ <p>
+ <%= f.label :content, "Answer" %>
+ <%= f.text_field :content %>
+ <%# f.check_box :_destroy %>
+ <%# f.label :_destroy, "Remove" %>
+ <%= link_to_remove_fields "remove", f %>
+ </p>
+</div>
@@ -0,0 +1,12 @@
+<%= 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><%= link_to_add_fields "Add Question", f, :questions %></p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,13 @@
+<div class="fields">
+ <p>
+ <%= f.label :content, "Question" %><br />
+ <%= f.text_area :content, :rows => 3 %><br />
+ <%# f.check_box :_destroy %>
+ <%# f.label :_destroy, "Remove Question" %>
+ <%= link_to_remove_fields "remove", f %>
+ </p>
+ <%= f.fields_for :answers do |builder| %>
+ <%= render 'answer_fields', :f => builder %>
+ <% end %>
+ <p><%= link_to_add_fields "Add Answer", f, :answers %></p>
+</div>
@@ -0,0 +1,8 @@
+<% title "Edit Survey" %>
+
+<%= render '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><%= 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 'form' %>
+
+<p><%= link_to "Back to List", surveys_path %></p>
@@ -0,0 +1,25 @@
+<% title "Survey" %>
+
+<p>
+ <strong>Name:</strong>
+ <%= @survey.name %>
+</p>
+
+<ol>
+<% for question in @survey.questions %>
+ <li>
+ <%= question.content %>
+ <ul>
+ <% for answer in question.answers %>
+ <li><%= 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>
View
@@ -0,0 +1,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run Surveyor::Application
Oops, something went wrong.

0 comments on commit 14f98e9

Please sign in to comment.