Permalink
Browse files

adding episode 230

  • Loading branch information...
1 parent 0d4d7c4 commit f4f51a2fc312969ecb5531373874020a0c342b79 @ryanb committed Sep 6, 2010
Showing with 10,182 additions and 0 deletions.
  1. +3 −0 episode-230/README
  2. +4 −0 episode-230/store/.gitignore
  3. +7 −0 episode-230/store/Gemfile
  4. +82 −0 episode-230/store/Gemfile.lock
  5. +1 −0 episode-230/store/README
  6. +7 −0 episode-230/store/Rakefile
  7. +3 −0 episode-230/store/app/controllers/application_controller.rb
  8. +8 −0 episode-230/store/app/controllers/products_controller.rb
  9. +9 −0 episode-230/store/app/controllers/reviews_controller.rb
  10. +2 −0 episode-230/store/app/helpers/application_helper.rb
  11. +23 −0 episode-230/store/app/helpers/error_messages_helper.rb
  12. +22 −0 episode-230/store/app/helpers/layout_helper.rb
  13. +2 −0 episode-230/store/app/helpers/products_helper.rb
  14. +2 −0 episode-230/store/app/helpers/reviews_helper.rb
  15. +4 −0 episode-230/store/app/models/product.rb
  16. +5 −0 episode-230/store/app/models/review.rb
  17. +19 −0 episode-230/store/app/views/layouts/application.html.erb
  18. +12 −0 episode-230/store/app/views/products/_form.html.erb
  19. +8 −0 episode-230/store/app/views/products/edit.html.erb
  20. +13 −0 episode-230/store/app/views/products/index.html.erb
  21. +5 −0 episode-230/store/app/views/products/new.html.erb
  22. +20 −0 episode-230/store/app/views/products/show.html.erb
  23. +16 −0 episode-230/store/app/views/reviews/index.html.erb
  24. +17 −0 episode-230/store/app/views/reviews/new.html.erb
  25. +4 −0 episode-230/store/config.ru
  26. +42 −0 episode-230/store/config/application.rb
  27. +13 −0 episode-230/store/config/boot.rb
  28. +22 −0 episode-230/store/config/database.yml
  29. +5 −0 episode-230/store/config/environment.rb
  30. +26 −0 episode-230/store/config/environments/development.rb
  31. +49 −0 episode-230/store/config/environments/production.rb
  32. +35 −0 episode-230/store/config/environments/test.rb
  33. +7 −0 episode-230/store/config/initializers/backtrace_silencers.rb
  34. +10 −0 episode-230/store/config/initializers/inflections.rb
  35. +5 −0 episode-230/store/config/initializers/mime_types.rb
  36. +7 −0 episode-230/store/config/initializers/secret_token.rb
  37. +8 −0 episode-230/store/config/initializers/session_store.rb
  38. +8 −0 episode-230/store/config/locales/en.yml
  39. +6 −0 episode-230/store/config/routes.rb
  40. +14 −0 episode-230/store/db/migrate/20100906005045_create_products.rb
  41. +14 −0 episode-230/store/db/migrate/20100906005229_create_reviews.rb
  42. +31 −0 episode-230/store/db/schema.rb
  43. +7 −0 episode-230/store/db/seeds.rb
  44. +2 −0 episode-230/store/doc/README_FOR_APP
  45. 0 episode-230/store/lib/tasks/.gitkeep
  46. +26 −0 episode-230/store/public/404.html
  47. +26 −0 episode-230/store/public/422.html
  48. +26 −0 episode-230/store/public/500.html
  49. 0 episode-230/store/public/favicon.ico
  50. BIN episode-230/store/public/images/rails.png
  51. +2 −0 episode-230/store/public/javascripts/application.js
  52. +965 −0 episode-230/store/public/javascripts/controls.js
  53. +974 −0 episode-230/store/public/javascripts/dragdrop.js
  54. +1,123 −0 episode-230/store/public/javascripts/effects.js
  55. +6,001 −0 episode-230/store/public/javascripts/prototype.js
  56. +175 −0 episode-230/store/public/javascripts/rails.js
  57. +5 −0 episode-230/store/public/robots.txt
  58. 0 episode-230/store/public/stylesheets/.gitkeep
  59. +84 −0 episode-230/store/public/stylesheets/application.css
  60. +6 −0 episode-230/store/script/rails
  61. +45 −0 episode-230/store/test/fixtures/products.yml
  62. 0 episode-230/store/test/fixtures/reviews.yml
  63. +54 −0 episode-230/store/test/functional/products_controller_test.rb
  64. +25 −0 episode-230/store/test/functional/reviews_controller_test.rb
  65. +9 −0 episode-230/store/test/performance/browsing_test.rb
  66. +13 −0 episode-230/store/test/test_helper.rb
  67. +7 −0 episode-230/store/test/unit/product_test.rb
  68. +7 −0 episode-230/store/test/unit/review_test.rb
  69. 0 episode-230/store/vendor/plugins/.gitkeep
View
@@ -0,0 +1,3 @@
+Railscasts Episode #230: Inherited Resources
+
+http://railscasts.com/episodes/230
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
@@ -0,0 +1,7 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.0'
+gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
+gem 'nifty-generators'
+gem 'inherited_resources'
+gem 'has_scope'
@@ -0,0 +1,82 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.0)
+ actionpack (= 3.0.0)
+ mail (~> 2.2.5)
+ actionpack (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4.1)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.12)
+ rack-test (~> 0.5.4)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activerecord (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ arel (~> 1.0.0)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ activesupport (3.0.0)
+ arel (1.0.1)
+ activesupport (~> 3.0.0)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ has_scope (0.5.0)
+ i18n (0.4.1)
+ inherited_resources (1.1.2)
+ has_scope (~> 0.5.0)
+ responders (~> 0.6.0)
+ mail (2.2.5)
+ activesupport (>= 2.3.6)
+ mime-types
+ treetop (>= 1.4.5)
+ mime-types (1.16)
+ nifty-generators (0.4.0)
+ polyglot (0.3.1)
+ rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
+ rack-test (0.5.4)
+ rack (>= 1.0)
+ rails (3.0.0)
+ actionmailer (= 3.0.0)
+ actionpack (= 3.0.0)
+ activerecord (= 3.0.0)
+ activeresource (= 3.0.0)
+ activesupport (= 3.0.0)
+ bundler (~> 1.0.0)
+ railties (= 3.0.0)
+ railties (3.0.0)
+ actionpack (= 3.0.0)
+ activesupport (= 3.0.0)
+ rake (>= 0.8.4)
+ thor (~> 0.14.0)
+ rake (0.8.7)
+ responders (0.6.2)
+ sqlite3-ruby (1.2.5)
+ thor (0.14.0)
+ treetop (1.4.8)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.23)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ has_scope
+ inherited_resources
+ nifty-generators
+ rails (= 3.0.0)
+ sqlite3-ruby (= 1.2.5)
View
@@ -0,0 +1 @@
+Example Railscasts application.
@@ -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'
+
+Store::Application.load_tasks
@@ -0,0 +1,3 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+end
@@ -0,0 +1,8 @@
+class ProductsController < InheritedResources::Base
+ respond_to :html, :xml
+ has_scope :limit, :default => 8
+
+ def create
+ create! { products_path }
+ end
+end
@@ -0,0 +1,9 @@
+class ReviewsController < InheritedResources::Base
+ belongs_to :product
+ actions :index, :new, :create
+ has_scope :rating
+
+ def create
+ create! { collection_url }
+ 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,2 @@
+module ReviewsHelper
+end
@@ -0,0 +1,4 @@
+class Product < ActiveRecord::Base
+ attr_accessible :name, :price, :discontinued_at
+ has_many :reviews
+end
@@ -0,0 +1,5 @@
+class Review < ActiveRecord::Base
+ attr_accessible :content, :rating
+ belongs_to :product
+ scope :rating, proc { |rating| where(:rating => rating) }
+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,13 @@
+<% title "Products" %>
+
+<% for product in @products %>
+ <div class="product">
+ <h2><%= product.name %></h2>
+ <div class="details">
+ Price: <%= number_to_currency(product.price) %> |
+ <%= link_to "Read Reviews", product_reviews_path(product) %>
+ </div>
+ </div>
+<% end %>
+
+<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,20 @@
+<% title "Product" %>
+
+<p>
+ <strong>Name:</strong>
+ <%= @product.name %>
+</p>
+<p>
+ <strong>Price:</strong>
+ <%= @product.price %>
+</p>
+<p>
+ <strong>Discontinued At:</strong>
+ <%= @product.discontinued_at %>
+</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,16 @@
+<% title "Reviews for #{@product.name}" %>
+
+<p><%= link_to "Back to Products", products_path %></p>
+
+<% if @reviews.empty? %>
+ <h2>No reviews yet.</h2>
+<% else %>
+ <% for review in @reviews %>
+ <div class="review">
+ <p>Rating: <%= review.rating %></p>
+ <p><%= review.content %></p>
+ </div>
+ <% end %>
+<% end %>
+
+<p><%= link_to "Add a Review", new_product_review_path(@product) %></p>
@@ -0,0 +1,17 @@
+<% title "New Review for #{@product.name}" %>
+
+<%= form_for [@product, @review] do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :content %><br />
+ <%= f.text_area :content, :rows => 10, :cols => 40 %>
+ </p>
+ <p>
+ <%= f.label :rating %><br />
+ <%= f.select :rating, 1..5 %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
+
+
+<p><%= link_to "Back to Reviews", product_reviews_path(@product) %></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,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 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.
+
+ # 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)
Oops, something went wrong.

2 comments on commit f4f51a2

zzak commented on f4f51a2 Sep 6, 2010

Congrats on episode #230 Ryan!

Owner
ryanb replied Sep 6, 2010

Thanks

Please sign in to comment.