Permalink
Browse files

adding episode 224

  • Loading branch information...
1 parent fbcf395 commit 0cf4a0b9889defc94380049fa4cb2d4d0e5f0e2a @ryanb committed Jul 26, 2010
Showing with 8,814 additions and 0 deletions.
  1. +3 −0 episode-224/README
  2. +4 −0 episode-224/store/.gitignore
  3. +30 −0 episode-224/store/Gemfile
  4. +1 −0 episode-224/store/README
  5. +7 −0 episode-224/store/Rakefile
  6. +3 −0 episode-224/store/app/controllers/application_controller.rb
  7. +49 −0 episode-224/store/app/controllers/products_controller.rb
  8. +2 −0 episode-224/store/app/helpers/application_helper.rb
  9. +23 −0 episode-224/store/app/helpers/error_messages_helper.rb
  10. +22 −0 episode-224/store/app/helpers/layout_helper.rb
  11. +2 −0 episode-224/store/app/helpers/products_helper.rb
  12. +3 −0 episode-224/store/app/models/product.rb
  13. +19 −0 episode-224/store/app/views/layouts/application.html.erb
  14. +12 −0 episode-224/store/app/views/products/_form.html.erb
  15. +8 −0 episode-224/store/app/views/products/edit.html.erb
  16. +19 −0 episode-224/store/app/views/products/index.html.erb
  17. +5 −0 episode-224/store/app/views/products/new.html.erb
  18. +16 −0 episode-224/store/app/views/products/show.html.erb
  19. +4 −0 episode-224/store/config.ru
  20. +46 −0 episode-224/store/config/application.rb
  21. +13 −0 episode-224/store/config/boot.rb
  22. +22 −0 episode-224/store/config/database.yml
  23. +5 −0 episode-224/store/config/environment.rb
  24. +19 −0 episode-224/store/config/environments/development.rb
  25. +46 −0 episode-224/store/config/environments/production.rb
  26. +32 −0 episode-224/store/config/environments/test.rb
  27. +7 −0 episode-224/store/config/initializers/backtrace_silencers.rb
  28. +10 −0 episode-224/store/config/initializers/inflections.rb
  29. +5 −0 episode-224/store/config/initializers/mime_types.rb
  30. +7 −0 episode-224/store/config/initializers/secret_token.rb
  31. +8 −0 episode-224/store/config/initializers/session_store.rb
  32. +5 −0 episode-224/store/config/locales/en.yml
  33. +4 −0 episode-224/store/config/routes.rb
  34. +13 −0 episode-224/store/db/migrate/20100726045843_create_products.rb
  35. +21 −0 episode-224/store/db/schema.rb
  36. +7 −0 episode-224/store/db/seeds.rb
  37. +2 −0 episode-224/store/doc/README_FOR_APP
  38. 0 episode-224/store/lib/tasks/.gitkeep
  39. +26 −0 episode-224/store/public/404.html
  40. +26 −0 episode-224/store/public/422.html
  41. +26 −0 episode-224/store/public/500.html
  42. 0 episode-224/store/public/favicon.ico
  43. BIN episode-224/store/public/images/rails.png
  44. +2 −0 episode-224/store/public/javascripts/application.js
  45. +965 −0 episode-224/store/public/javascripts/controls.js
  46. +974 −0 episode-224/store/public/javascripts/dragdrop.js
  47. +1,123 −0 episode-224/store/public/javascripts/effects.js
  48. +4,874 −0 episode-224/store/public/javascripts/prototype.js
  49. +118 −0 episode-224/store/public/javascripts/rails.js
  50. +5 −0 episode-224/store/public/robots.txt
  51. 0 episode-224/store/public/stylesheets/.gitkeep
  52. +75 −0 episode-224/store/public/stylesheets/application.css
  53. +6 −0 episode-224/store/script/rails
  54. +7 −0 episode-224/store/test/fixtures/products.yml
  55. +54 −0 episode-224/store/test/functional/products_controller_test.rb
  56. +9 −0 episode-224/store/test/performance/browsing_test.rb
  57. +13 −0 episode-224/store/test/test_helper.rb
  58. +7 −0 episode-224/store/test/unit/product_test.rb
  59. 0 episode-224/store/vendor/plugins/.gitkeep
View
@@ -0,0 +1,3 @@
+Railscasts Episode #224: Controllers in Rails 3
+
+http://railscasts.com/episodes/224
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
View
@@ -0,0 +1,30 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.0.beta4'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
+gem 'nifty-generators'
+
+# Use unicorn as the web server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'ruby-debug'
+
+# Bundle the extra gems:
+# gem 'bj'
+# gem 'nokogiri', '1.4.1'
+# gem 'sqlite3-ruby', :require => 'sqlite3'
+# gem 'aws-s3', :require => 'aws/s3'
+
+# Bundle gems for certain environments:
+# gem 'rspec', :group => :test
+# group :test do
+# gem 'webrat'
+# end
View
@@ -0,0 +1 @@
+Example application used in Railscasts.
@@ -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'
+
+Rails::Application.load_tasks
@@ -0,0 +1,3 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+end
@@ -0,0 +1,49 @@
+class ProductsController < ApplicationController
+ respond_to :html, :xml
+
+ def index
+ @products = Product.all
+ respond_with(@products)
+ end
+
+ def show
+ @product = Product.find(params[:id])
+ respond_with(@product)
+ end
+
+ def new
+ @product = Product.new
+ respond_with(@product)
+ end
+
+ def create
+ @product = Product.new(params[:product])
+ if @product.save
+ cookies.permanent[:last_product_id] = @product.id
+ flash[:notice] = "Successfully created product."
+ end
+ respond_with(@product)
+ end
+
+ def edit
+ @product = Product.find(params[:id])
+ respond_with(@product) do |format|
+ format.xml { render :text => "I'm XML!" }
+ end
+ end
+
+ def update
+ @product = Product.find(params[:id])
+ if @product.update_attributes(params[:product])
+ flash[:notice] = "Successfully updated product."
+ end
+ respond_with(@product, :location => products_url)
+ end
+
+ def destroy
+ @product = Product.find(params[:id])
+ @product.destroy
+ flash[:notice] = "Successfully destroyed product."
+ respond_with(@product, :responder => MyResponder)
+ end
+end
@@ -0,0 +1,2 @@
+module ApplicationHelper
+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) { 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 ProductsHelper
+end
@@ -0,0 +1,3 @@
+class Product < ActiveRecord::Base
+ attr_accessible :name, :price
+end
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <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 @product do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :price %><br />
+ <%= f.text_field :price %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Product" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", @product %> |
+ <%= link_to "View All", products_path %>
+</p>
@@ -0,0 +1,19 @@
+<% title "Products" %>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Price</th>
+ </tr>
+ <% for product in @products %>
+ <tr>
+ <td><%= product.name %></td>
+ <td><%= product.price %></td>
+ <td><%= link_to "Show", product %></td>
+ <td><%= link_to "Edit", edit_product_path(product) %></td>
+ <td><%= link_to "Destroy", product, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Product", new_product_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Product" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", products_path %></p>
@@ -0,0 +1,16 @@
+<% title "Product" %>
+
+<p>
+ <strong>Name:</strong>
+ <%= @product.name %>
+</p>
+<p>
+ <strong>Price:</strong>
+ <%= @product.price %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_product_path(@product) %> |
+ <%= link_to "Destroy", @product, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", products_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 Store::Application
@@ -0,0 +1,46 @@
+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 Store
+ 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.
+
+ # Add additional load paths for your own custom dirs
+ # config.load_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
+
+ # Configure generators values. Many other options are available, be sure to check the documentation.
+ # config.generators do |g|
+ # g.orm :active_record
+ # g.template_engine :erb
+ # g.test_framework :test_unit, :fixture => true
+ # end
+
+ # 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
+Store::Application.initialize!
@@ -0,0 +1,19 @@
+Store::Application.configure do
+ # Settings specified here will take precedence over those in config/environment.rb
+
+ # In the development environment your application's code is reloaded on
+ # every request. This slows down response time but is perfect for development
+ # since you don't have to restart the webserver when you make code changes.
+ config.cache_classes = false
+
+ # Log error messages when you accidentally call methods on nil.
+ config.whiny_nils = true
+
+ # Show full error reports and disable caching
+ config.consider_all_requests_local = true
+ config.action_view.debug_rjs = true
+ config.action_controller.perform_caching = false
+
+ # Don't care if the mailer can't send
+ config.action_mailer.raise_delivery_errors = false
+end
Oops, something went wrong.

0 comments on commit 0cf4a0b

Please sign in to comment.