Permalink
Browse files

adding episode 251

  • Loading branch information...
1 parent eeb4330 commit b482bb8d4da0734196d767f9729caf2035464bac @ryanb committed Jan 31, 2011
Showing with 8,240 additions and 0 deletions.
  1. +11 −0 episode-251/README
  2. +4 −0 episode-251/store/.gitignore
  3. +38 −0 episode-251/store/Gemfile
  4. +94 −0 episode-251/store/Gemfile.lock
  5. +7 −0 episode-251/store/Rakefile
  6. +3 −0 episode-251/store/app/controllers/application_controller.rb
  7. +42 −0 episode-251/store/app/controllers/products_controller.rb
  8. +8 −0 episode-251/store/app/helpers/application_helper.rb
  9. +23 −0 episode-251/store/app/helpers/error_messages_helper.rb
  10. +22 −0 episode-251/store/app/helpers/layout_helper.rb
  11. +2 −0 episode-251/store/app/helpers/products_helper.rb
  12. +3 −0 episode-251/store/app/models/product.rb
  13. +19 −0 episode-251/store/app/views/layouts/application.html.erb
  14. +16 −0 episode-251/store/app/views/products/_form.html.erb
  15. +8 −0 episode-251/store/app/views/products/edit.html.erb
  16. +32 −0 episode-251/store/app/views/products/index.html.erb
  17. +5 −0 episode-251/store/app/views/products/new.html.erb
  18. +20 −0 episode-251/store/app/views/products/show.html.erb
  19. +4 −0 episode-251/store/config.ru
  20. +42 −0 episode-251/store/config/application.rb
  21. +13 −0 episode-251/store/config/boot.rb
  22. +22 −0 episode-251/store/config/database.yml
  23. +5 −0 episode-251/store/config/environment.rb
  24. +22 −0 episode-251/store/config/environments/development.rb
  25. +49 −0 episode-251/store/config/environments/production.rb
  26. +35 −0 episode-251/store/config/environments/test.rb
  27. +7 −0 episode-251/store/config/initializers/backtrace_silencers.rb
  28. +10 −0 episode-251/store/config/initializers/inflections.rb
  29. +5 −0 episode-251/store/config/initializers/mime_types.rb
  30. +4 −0 episode-251/store/config/initializers/renderers.rb
  31. +7 −0 episode-251/store/config/initializers/secret_token.rb
  32. +8 −0 episode-251/store/config/initializers/session_store.rb
  33. +5 −0 episode-251/store/config/locales/en.yml
  34. +61 −0 episode-251/store/config/routes.rb
  35. +14 −0 episode-251/store/db/migrate/20100823060835_create_products.rb
  36. +23 −0 episode-251/store/db/schema.rb
  37. +7 −0 episode-251/store/db/seeds.rb
  38. +2 −0 episode-251/store/doc/README_FOR_APP
  39. 0 episode-251/store/lib/tasks/.gitkeep
  40. +26 −0 episode-251/store/public/404.html
  41. +26 −0 episode-251/store/public/422.html
  42. +26 −0 episode-251/store/public/500.html
  43. 0 episode-251/store/public/favicon.ico
  44. BIN episode-251/store/public/images/down_arrow.gif
  45. BIN episode-251/store/public/images/rails.png
  46. BIN episode-251/store/public/images/up_arrow.gif
  47. 0 episode-251/store/public/javascripts/application.js
  48. +6,883 −0 episode-251/store/public/javascripts/jquery.js
  49. +166 −0 episode-251/store/public/javascripts/jquery.min.js
  50. +156 −0 episode-251/store/public/javascripts/rails.js
  51. +5 −0 episode-251/store/public/robots.txt
  52. 0 episode-251/store/public/stylesheets/.gitkeep
  53. +108 −0 episode-251/store/public/stylesheets/application.css
  54. +6 −0 episode-251/store/script/rails
  55. +53 −0 episode-251/store/test/fixtures/products.yml
  56. +54 −0 episode-251/store/test/functional/products_controller_test.rb
  57. +9 −0 episode-251/store/test/performance/browsing_test.rb
  58. +13 −0 episode-251/store/test/test_helper.rb
  59. +7 −0 episode-251/store/test/unit/product_test.rb
  60. 0 episode-251/store/vendor/plugins/.gitkeep
View
@@ -0,0 +1,11 @@
+Railscasts Episode #251: MetaWhere & MetaSearch
+
+http://railscasts.com/episodes/251
+
+rails console
+
+ Product.where(:price.lt => 5)
+ Product.where({:price.lt => 5} | {:name.matches => "%video%"})
+ Product.order(:released_at.desc)
+ MetaWhere.operator_overload!
+ Product.where(:price < 5)
@@ -0,0 +1,4 @@
+.bundle
+db/*.sqlite3
+log/*.log
+tmp/**/*
View
@@ -0,0 +1,38 @@
+source 'http://rubygems.org'
+
+gem 'rails', '3.0.3'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
+gem 'nifty-generators'
+gem 'will_paginate', '3.0.pre2'
+gem 'jquery-rails'
+
+gem "meta_where"
+gem "meta_search"
+
+# 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 the local environment. Make sure to
+# put test-only gems in this group so their generators
+# and rake tasks are available in development mode:
+# group :development, :test do
+# gem 'webrat'
+# end
+
+gem "hirb", :group => :development
@@ -0,0 +1,94 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.3)
+ actionpack (= 3.0.3)
+ mail (~> 2.2.9)
+ actionpack (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.13)
+ rack-test (~> 0.5.6)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.3)
+ activesupport (= 3.0.3)
+ builder (~> 2.1.2)
+ i18n (~> 0.4)
+ activerecord (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ arel (~> 2.0.2)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.3)
+ activemodel (= 3.0.3)
+ activesupport (= 3.0.3)
+ activesupport (3.0.3)
+ arel (2.0.6)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ hirb (0.3.6)
+ i18n (0.5.0)
+ jquery-rails (0.2.6)
+ rails (~> 3.0)
+ thor (~> 0.14.4)
+ mail (2.2.12)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ meta_search (1.0.1)
+ actionpack (~> 3.0.2)
+ activerecord (~> 3.0.2)
+ activesupport (~> 3.0.2)
+ arel (~> 2.0.2)
+ meta_where (1.0.1)
+ activerecord (~> 3.0.2)
+ activesupport (~> 3.0.2)
+ arel (~> 2.0.2)
+ mime-types (1.16)
+ nifty-generators (0.4.2)
+ polyglot (0.3.1)
+ rack (1.2.1)
+ rack-mount (0.6.13)
+ rack (>= 1.0.0)
+ rack-test (0.5.6)
+ rack (>= 1.0)
+ rails (3.0.3)
+ actionmailer (= 3.0.3)
+ actionpack (= 3.0.3)
+ activerecord (= 3.0.3)
+ activeresource (= 3.0.3)
+ activesupport (= 3.0.3)
+ bundler (~> 1.0)
+ railties (= 3.0.3)
+ railties (3.0.3)
+ actionpack (= 3.0.3)
+ activesupport (= 3.0.3)
+ rake (>= 0.8.7)
+ thor (~> 0.14.4)
+ rake (0.8.7)
+ sqlite3-ruby (1.2.5)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.23)
+ will_paginate (3.0.pre2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ hirb
+ jquery-rails
+ meta_search
+ meta_where
+ nifty-generators
+ rails (= 3.0.3)
+ sqlite3-ruby (= 1.2.5)
+ will_paginate (= 3.0.pre2)
@@ -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,42 @@
+class ProductsController < ApplicationController
+ def index
+ @search = Product.search(params[:search])
+ @products = @search.all
+ end
+
+ def show
+ @product = Product.find(params[:id])
+ end
+
+ def new
+ @product = Product.new
+ end
+
+ def create
+ @product = Product.new(params[:product])
+ if @product.save
+ redirect_to @product, :notice => "Successfully created product."
+ else
+ render :action => "new"
+ end
+ end
+
+ def edit
+ @product = Product.find(params[:id])
+ end
+
+ def update
+ @product = Product.find(params[:id])
+ if @product.update_attributes(params[:product])
+ redirect_to @product, :notice => "Successfully updated product."
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @product = Product.find(params[:id])
+ @product.destroy
+ redirect_to products_url, :notice => "Successfully destroyed product."
+ end
+end
@@ -0,0 +1,8 @@
+module ApplicationHelper
+ def sortable(column, title = nil)
+ title ||= column.titleize
+ css_class = column == sort_column ? "current #{sort_direction}" : nil
+ direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
+ link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
+ 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] ||= "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, :released_at
+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,16 @@
+<%= 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.label :released_at %><br />
+ <%= f.datetime_select :released_at %>
+ </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,32 @@
+<% title "Products" %>
+
+<%= form_for @search do |f| %>
+ <p>
+ <%= f.label :name_contains %>
+ <%= f.text_field :name_contains %>
+ </p>
+ <p>
+ <%= f.label :price_gte, "Price ranges from" %>
+ <%= f.text_field :price_gte, :size => 8 %>
+ <%= f.label :price_lte, "to" %>
+ <%= f.text_field :price_lte, :size => 8 %>
+ </p>
+ <p class="button"><%= f.submit "Search" %></p>
+<% end %>
+
+<p>
+ Sort by:
+ <%= sort_link @search, :name %> |
+ <%= sort_link @search, :price %> |
+ <%= sort_link @search, :released_at %>
+</p>
+
+<% for product in @products %>
+ <div class="product">
+ <h2><%= link_to product.name, product %></h2>
+ <%= number_to_currency(product.price) %> |
+ Released: <%= product.released_at.strftime("%B %e, %Y") %>
+ </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>Released At:</strong>
+ <%= @product.released_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,4 @@
+# This file is used by Rack-based servers to start the application.
+
+require ::File.expand_path('../config/environment', __FILE__)
+run Store::Application
Oops, something went wrong.

0 comments on commit b482bb8

Please sign in to comment.