Permalink
Browse files

adding episode 111

  • Loading branch information...
1 parent b7928b4 commit ed7b48148932528d6f4f59cec8e4fbbe02aff4b8 @ryanb committed Nov 17, 2011
Showing with 1,869 additions and 0 deletions.
  1. +11 −0 episode-111/README
  2. +5 −0 episode-111/store-after/.gitignore
  3. +36 −0 episode-111/store-after/Gemfile
  4. +119 −0 episode-111/store-after/Gemfile.lock
  5. +9 −0 episode-111/store-after/README
  6. +7 −0 episode-111/store-after/Rakefile
  7. BIN episode-111/store-after/app/assets/images/rails.png
  8. +9 −0 episode-111/store-after/app/assets/javascripts/application.js
  9. +3 −0 episode-111/store-after/app/assets/javascripts/products.js.coffee
  10. +3 −0 episode-111/store-after/app/assets/javascripts/searches.js.coffee
  11. +92 −0 episode-111/store-after/app/assets/stylesheets/application.css
  12. +3 −0 episode-111/store-after/app/assets/stylesheets/products.css.scss
  13. +3 −0 episode-111/store-after/app/assets/stylesheets/searches.css.scss
  14. +3 −0 episode-111/store-after/app/controllers/application_controller.rb
  15. +5 −0 episode-111/store-after/app/controllers/products_controller.rb
  16. +14 −0 episode-111/store-after/app/controllers/searches_controller.rb
  17. +2 −0 episode-111/store-after/app/helpers/application_helper.rb
  18. +2 −0 episode-111/store-after/app/helpers/products_helper.rb
  19. +2 −0 episode-111/store-after/app/helpers/searches_helper.rb
  20. 0 episode-111/store-after/app/mailers/.gitkeep
  21. 0 episode-111/store-after/app/models/.gitkeep
  22. +3 −0 episode-111/store-after/app/models/category.rb
  23. +9 −0 episode-111/store-after/app/models/product.rb
  24. +16 −0 episode-111/store-after/app/models/search.rb
  25. +17 −0 episode-111/store-after/app/views/layouts/application.html.erb
  26. +9 −0 episode-111/store-after/app/views/products/_product.html.erb
  27. +14 −0 episode-111/store-after/app/views/products/index.html.erb
  28. +18 −0 episode-111/store-after/app/views/searches/new.html.erb
  29. +3 −0 episode-111/store-after/app/views/searches/show.html.erb
  30. +4 −0 episode-111/store-after/config.ru
  31. +48 −0 episode-111/store-after/config/application.rb
  32. +6 −0 episode-111/store-after/config/boot.rb
  33. +25 −0 episode-111/store-after/config/database.yml
  34. +5 −0 episode-111/store-after/config/environment.rb
  35. +30 −0 episode-111/store-after/config/environments/development.rb
  36. +60 −0 episode-111/store-after/config/environments/production.rb
  37. +39 −0 episode-111/store-after/config/environments/test.rb
  38. +7 −0 episode-111/store-after/config/initializers/backtrace_silencers.rb
  39. +10 −0 episode-111/store-after/config/initializers/inflections.rb
  40. +5 −0 episode-111/store-after/config/initializers/mime_types.rb
  41. +7 −0 episode-111/store-after/config/initializers/secret_token.rb
  42. +8 −0 episode-111/store-after/config/initializers/session_store.rb
  43. +14 −0 episode-111/store-after/config/initializers/wrap_parameters.rb
  44. +5 −0 episode-111/store-after/config/locales/en.yml
  45. +5 −0 episode-111/store-after/config/routes.rb
  46. +9 −0 episode-111/store-after/db/migrate/20111117185951_create_categories.rb
  47. +12 −0 episode-111/store-after/db/migrate/20111117190039_create_products.rb
  48. +12 −0 episode-111/store-after/db/migrate/20111117194652_create_searches.rb
  49. +40 −0 episode-111/store-after/db/schema.rb
  50. +35 −0 episode-111/store-after/db/seeds.rb
  51. +2 −0 episode-111/store-after/doc/README_FOR_APP
  52. 0 episode-111/store-after/lib/assets/.gitkeep
  53. 0 episode-111/store-after/lib/tasks/.gitkeep
  54. +4 −0 episode-111/store-after/lib/tasks/searches.rake
  55. 0 episode-111/store-after/log/.gitkeep
  56. +26 −0 episode-111/store-after/public/404.html
  57. +26 −0 episode-111/store-after/public/422.html
  58. +26 −0 episode-111/store-after/public/500.html
  59. 0 episode-111/store-after/public/favicon.ico
  60. +5 −0 episode-111/store-after/public/robots.txt
  61. +6 −0 episode-111/store-after/script/rails
  62. 0 episode-111/store-after/test/fixtures/.gitkeep
  63. +7 −0 episode-111/store-after/test/fixtures/categories.yml
  64. +13 −0 episode-111/store-after/test/fixtures/products.yml
  65. +13 −0 episode-111/store-after/test/fixtures/searches.yml
  66. 0 episode-111/store-after/test/functional/.gitkeep
  67. +7 −0 episode-111/store-after/test/functional/products_controller_test.rb
  68. +7 −0 episode-111/store-after/test/functional/searches_controller_test.rb
  69. 0 episode-111/store-after/test/integration/.gitkeep
  70. +12 −0 episode-111/store-after/test/performance/browsing_test.rb
  71. +13 −0 episode-111/store-after/test/test_helper.rb
  72. 0 episode-111/store-after/test/unit/.gitkeep
  73. +7 −0 episode-111/store-after/test/unit/category_test.rb
  74. +4 −0 episode-111/store-after/test/unit/helpers/products_helper_test.rb
  75. +4 −0 episode-111/store-after/test/unit/helpers/searches_helper_test.rb
  76. +7 −0 episode-111/store-after/test/unit/product_test.rb
  77. +7 −0 episode-111/store-after/test/unit/search_test.rb
  78. 0 episode-111/store-after/vendor/assets/stylesheets/.gitkeep
  79. 0 episode-111/store-after/vendor/plugins/.gitkeep
  80. +5 −0 episode-111/store-before/.gitignore
  81. +36 −0 episode-111/store-before/Gemfile
  82. +119 −0 episode-111/store-before/Gemfile.lock
  83. +9 −0 episode-111/store-before/README
  84. +7 −0 episode-111/store-before/Rakefile
  85. BIN episode-111/store-before/app/assets/images/rails.png
  86. +9 −0 episode-111/store-before/app/assets/javascripts/application.js
  87. +3 −0 episode-111/store-before/app/assets/javascripts/products.js.coffee
  88. +92 −0 episode-111/store-before/app/assets/stylesheets/application.css
  89. +3 −0 episode-111/store-before/app/assets/stylesheets/products.css.scss
  90. +3 −0 episode-111/store-before/app/controllers/application_controller.rb
  91. +5 −0 episode-111/store-before/app/controllers/products_controller.rb
  92. +2 −0 episode-111/store-before/app/helpers/application_helper.rb
  93. +2 −0 episode-111/store-before/app/helpers/products_helper.rb
  94. 0 episode-111/store-before/app/mailers/.gitkeep
  95. 0 episode-111/store-before/app/models/.gitkeep
  96. +3 −0 episode-111/store-before/app/models/category.rb
  97. +9 −0 episode-111/store-before/app/models/product.rb
  98. +17 −0 episode-111/store-before/app/views/layouts/application.html.erb
  99. +9 −0 episode-111/store-before/app/views/products/_product.html.erb
  100. +12 −0 episode-111/store-before/app/views/products/index.html.erb
  101. +4 −0 episode-111/store-before/config.ru
  102. +48 −0 episode-111/store-before/config/application.rb
  103. +6 −0 episode-111/store-before/config/boot.rb
  104. +25 −0 episode-111/store-before/config/database.yml
  105. +5 −0 episode-111/store-before/config/environment.rb
  106. +30 −0 episode-111/store-before/config/environments/development.rb
  107. +60 −0 episode-111/store-before/config/environments/production.rb
  108. +39 −0 episode-111/store-before/config/environments/test.rb
  109. +7 −0 episode-111/store-before/config/initializers/backtrace_silencers.rb
  110. +10 −0 episode-111/store-before/config/initializers/inflections.rb
  111. +5 −0 episode-111/store-before/config/initializers/mime_types.rb
  112. +7 −0 episode-111/store-before/config/initializers/secret_token.rb
  113. +8 −0 episode-111/store-before/config/initializers/session_store.rb
  114. +14 −0 episode-111/store-before/config/initializers/wrap_parameters.rb
  115. +5 −0 episode-111/store-before/config/locales/en.yml
  116. +4 −0 episode-111/store-before/config/routes.rb
  117. +9 −0 episode-111/store-before/db/migrate/20111117185951_create_categories.rb
  118. +12 −0 episode-111/store-before/db/migrate/20111117190039_create_products.rb
  119. +31 −0 episode-111/store-before/db/schema.rb
  120. +35 −0 episode-111/store-before/db/seeds.rb
  121. +2 −0 episode-111/store-before/doc/README_FOR_APP
  122. 0 episode-111/store-before/lib/assets/.gitkeep
  123. 0 episode-111/store-before/lib/tasks/.gitkeep
  124. 0 episode-111/store-before/log/.gitkeep
  125. +26 −0 episode-111/store-before/public/404.html
  126. +26 −0 episode-111/store-before/public/422.html
  127. +26 −0 episode-111/store-before/public/500.html
  128. 0 episode-111/store-before/public/favicon.ico
  129. +5 −0 episode-111/store-before/public/robots.txt
  130. +6 −0 episode-111/store-before/script/rails
  131. 0 episode-111/store-before/test/fixtures/.gitkeep
  132. +7 −0 episode-111/store-before/test/fixtures/categories.yml
  133. +13 −0 episode-111/store-before/test/fixtures/products.yml
  134. 0 episode-111/store-before/test/functional/.gitkeep
  135. +7 −0 episode-111/store-before/test/functional/products_controller_test.rb
  136. 0 episode-111/store-before/test/integration/.gitkeep
  137. +12 −0 episode-111/store-before/test/performance/browsing_test.rb
  138. +13 −0 episode-111/store-before/test/test_helper.rb
  139. 0 episode-111/store-before/test/unit/.gitkeep
  140. +7 −0 episode-111/store-before/test/unit/category_test.rb
  141. +4 −0 episode-111/store-before/test/unit/helpers/products_helper_test.rb
  142. +7 −0 episode-111/store-before/test/unit/product_test.rb
  143. 0 episode-111/store-before/vendor/assets/stylesheets/.gitkeep
  144. 0 episode-111/store-before/vendor/plugins/.gitkeep
View
@@ -0,0 +1,11 @@
+RailsCasts Episode #111: Advanced Search Form (revised)
+
+http://railscasts.com/episodes/111-advanced-search-form-revised
+
+Ruby 1.9.2 or later required to run example.
+
+Commands used in episode
+
+ rails g model search keywords:string category_id:integer min_price:decimal max_price:decimal
+ rake db:migrate
+ rails g controller searches
@@ -0,0 +1,5 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/
+.sass-cache/
@@ -0,0 +1,36 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.1.1'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3'
+
+
+# Gems used only for assets and not required
+# in production environments by default.
+group :assets do
+ gem 'sass-rails', '~> 3.1.4'
+ gem 'coffee-rails', '~> 3.1.1'
+ gem 'uglifier', '>= 1.0.3'
+end
+
+gem 'jquery-rails'
+
+# To use ActiveModel has_secure_password
+# gem 'bcrypt-ruby', '~> 3.0.0'
+
+# Use unicorn as the web server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'ruby-debug19', :require => 'ruby-debug'
+
+group :test do
+ # Pretty printed test output
+ gem 'turn', :require => false
+end
@@ -0,0 +1,119 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.1.1)
+ actionpack (= 3.1.1)
+ mail (~> 2.3.0)
+ actionpack (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ i18n (~> 0.6)
+ rack (~> 1.3.2)
+ rack-cache (~> 1.1)
+ rack-mount (~> 0.8.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.0.2)
+ activemodel (3.1.1)
+ activesupport (= 3.1.1)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activerecord (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ arel (~> 2.2.1)
+ tzinfo (~> 0.3.29)
+ activeresource (3.1.1)
+ activemodel (= 3.1.1)
+ activesupport (= 3.1.1)
+ activesupport (3.1.1)
+ multi_json (~> 1.0)
+ ansi (1.4.1)
+ arel (2.2.1)
+ builder (3.0.0)
+ coffee-rails (3.1.1)
+ coffee-script (>= 2.2.0)
+ railties (~> 3.1.0)
+ coffee-script (2.2.0)
+ coffee-script-source
+ execjs
+ coffee-script-source (1.1.3)
+ erubis (2.7.0)
+ execjs (1.2.9)
+ multi_json (~> 1.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ jquery-rails (1.0.17)
+ railties (~> 3.0)
+ thor (~> 0.14)
+ json (1.6.1)
+ mail (2.3.0)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.17.2)
+ multi_json (1.0.3)
+ polyglot (0.3.3)
+ rack (1.3.5)
+ rack-cache (1.1)
+ rack (>= 0.4)
+ rack-mount (0.8.3)
+ rack (>= 1.0.0)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.1.1)
+ actionmailer (= 3.1.1)
+ actionpack (= 3.1.1)
+ activerecord (= 3.1.1)
+ activeresource (= 3.1.1)
+ activesupport (= 3.1.1)
+ bundler (~> 1.0)
+ railties (= 3.1.1)
+ railties (3.1.1)
+ actionpack (= 3.1.1)
+ activesupport (= 3.1.1)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.11)
+ json (~> 1.4)
+ sass (3.1.10)
+ sass-rails (3.1.4)
+ actionpack (~> 3.1.0)
+ railties (~> 3.1.0)
+ sass (>= 3.1.4)
+ sprockets (~> 2.0.0)
+ tilt (~> 1.3.2)
+ sprockets (2.0.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.4)
+ thor (0.14.6)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ turn (0.8.3)
+ ansi
+ tzinfo (0.3.31)
+ uglifier (1.1.0)
+ execjs (>= 0.3.0)
+ multi_json (>= 1.0.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ coffee-rails (~> 3.1.1)
+ jquery-rails
+ rails (= 3.1.1)
+ sass-rails (~> 3.1.4)
+ sqlite3
+ turn
+ uglifier (>= 1.0.3)
@@ -0,0 +1,9 @@
+= RailsCasts Example Application
+
+Run these commands to try it out.
+
+ bundle
+ rake db:setup
+ rails s
+
+Requires Ruby 1.9.2 or later to run.
@@ -0,0 +1,7 @@
+#!/usr/bin/env rake
+# 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__)
+
+Store::Application.load_tasks
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,9 @@
+// This is a manifest file that'll be compiled into including all the files listed below.
+// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
+// be included in the compiled file accessible from http://example.com/assets/application.js
+// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
+// the compiled file.
+//
+//= require jquery
+//= require jquery_ujs
+//= require_tree .
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,92 @@
+/*
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
+ *= require_self
+ *= require_tree .
+*/
+
+body {
+ background-color: #4B7399;
+ font-family: Verdana, Helvetica, Arial;
+ font-size: 14px;
+}
+
+a img {
+ border: none;
+}
+
+a {
+ color: #0000FF;
+}
+
+.clear {
+ clear: both;
+ height: 0;
+ overflow: hidden;
+}
+
+#container {
+ width: 75%;
+ margin: 0 auto;
+ background-color: #FFF;
+ padding: 20px 40px;
+ border: solid 1px black;
+ margin-top: 20px;
+}
+
+#flash_notice, #flash_error, #flash_alert {
+ padding: 5px 8px;
+ margin: 10px 0;
+}
+
+#flash_notice {
+ background-color: #CFC;
+ border: solid 1px #6C6;
+}
+
+#flash_error, #flash_alert {
+ background-color: #FCC;
+ border: solid 1px #C66;
+}
+
+.fieldWithErrors {
+ display: inline;
+}
+
+.error_messages {
+ width: 400px;
+ border: 2px solid #CF0000;
+ padding: 0px;
+ padding-bottom: 12px;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+ font-size: 12px;
+}
+
+.error_messages h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 10px;
+ font-size: 12px;
+ margin: 0;
+ background-color: #c00;
+ color: #fff;
+}
+
+.error_messages p {
+ margin: 8px 10px;
+}
+
+.error_messages ul {
+ margin: 0;
+}
+
+form .field, form .actions {
+ margin: 12px 0;
+}
+
+.product h2 {
+ font-size: 16px;
+ margin-bottom: 2px;
+}
@@ -0,0 +1,3 @@
+// Place all the styles related to the products controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,3 @@
+// Place all the styles related to the searches controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,3 @@
+class ApplicationController < ActionController::Base
+ protect_from_forgery
+end
@@ -0,0 +1,5 @@
+class ProductsController < ApplicationController
+ def index
+ @products = Product.search(params[:search])
+ end
+end
@@ -0,0 +1,14 @@
+class SearchesController < ApplicationController
+ def new
+ @search = Search.new
+ end
+
+ def create
+ @search = Search.create!(params[:search])
+ redirect_to @search
+ end
+
+ def show
+ @search = Search.find(params[:id])
+ end
+end
@@ -0,0 +1,2 @@
+module ApplicationHelper
+end
@@ -0,0 +1,2 @@
+module ProductsHelper
+end
@@ -0,0 +1,2 @@
+module SearchesHelper
+end
@@ -0,0 +1,3 @@
+class Category < ActiveRecord::Base
+ has_many :products
+end
@@ -0,0 +1,9 @@
+class Product < ActiveRecord::Base
+ belongs_to :category
+
+ def self.search(keywords)
+ products = order(:name)
+ products = products.where("name like ?", "%#{keywords}%") if keywords.present?
+ products
+ end
+end
@@ -0,0 +1,16 @@
+class Search < ActiveRecord::Base
+ def products
+ @products ||= find_products
+ end
+
+private
+
+ def find_products
+ products = Product.order(:name)
+ products = products.where("name like ?", "%#{keywords}%") if keywords.present?
+ products = products.where(category_id: category_id) if category_id.present?
+ products = products.where("price >= ?", min_price) if min_price.present?
+ products = products.where("price <= ?", max_price) if max_price.present?
+ products
+ end
+end
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Store</title>
+ <%= stylesheet_link_tag "application" %>
+ <%= javascript_include_tag "application" %>
+ <%= csrf_meta_tag %>
+ </head>
+ <body>
+ <div id="container">
+ <% flash.each do |name, msg| %>
+ <%= content_tag :div, msg, :id => "flash_#{name}" %>
+ <% end %>
+ <%= yield %>
+ </div>
+ </body>
+</html>
Oops, something went wrong.

0 comments on commit ed7b481

Please sign in to comment.