Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

25.1 rack 22 Caching

24.3 Active Resources

bundle exec /home/rubys/git/rails/bin/rails new depot_client --skip-bundle --dev
      create  
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  log
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  public/images
      create  public/images/rails.png
      create  public/stylesheets
      create  public/stylesheets/.gitkeep
      create  public/javascripts
      create  public/javascripts/application.js
      create  public/javascripts/controls.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/effects.js
      create  public/javascripts/prototype.js
      create  public/javascripts/rails.js
      create  script
      create  script/rails
      create  test
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/unit
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep
bundle install
Using rake (0.9.2.2) 
Using abstract (1.0.0) 
Using activesupport (3.0.15) 
Using builder (2.1.2) 
Using i18n (0.5.0) 
Using activemodel (3.0.15) 
Using erubis (2.6.6) 
Using rack (1.2.5) 
Using rack-mount (0.6.14) 
Using rack-test (0.5.7) 
Using tzinfo (0.3.33) 
Using actionpack (3.0.15) 
Using mime-types (1.19) 
Using polyglot (0.3.3) 
Using treetop (1.4.10) 
Using mail (2.2.19) 
Using actionmailer (3.0.15) 
Using arel (2.0.10) 
Using activerecord (3.0.15) 
Using activeresource (3.0.15) 
Using bundler (1.1.3) 
Using json (1.7.3) 
Using rdoc (3.12) 
Using thor (0.14.6) 
Using railties (3.0.15) 
Using rails (3.0.15) 
Using sqlite3 (1.3.6) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
edit app/models/product.rb
class Product < ActiveResource::Base
  self.site = 'http://dave:secret@localhost:3000/'
end
echo "Product.find(2).title" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> Product.find(2).title
=> "CoffeeScript"
>> 
edit app/controllers/line_items_controller.rb
  def create
    @cart = current_cart
    if params[:line_item]
      # ActiveResource
      params[:line_item][:order_id] = params[:order_id]
      @line_item = LineItem.new(params[:line_item])
    else
      # HTML forms
      product = Product.find(params[:product_id])
      @line_item = @cart.add_product(product.id)
    end
    @line_item.product = product
 
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to(store_url) }
        format.js   { @current_item = @line_item }
        format.xml  { render :xml => @line_item,
          :status => :created, :location => @line_item }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @line_item.errors,
          :status => :unprocessable_entity }
      end
    end
  end
edit config/routes.rb
Depot::Application.routes.draw do
  get 'admin' => 'admin#index'
  controller :sessions do
    get  'login' => :new
    post 'login' => :create
    delete 'logout' => :destroy
  end
  scope '(:locale)' do
    resources :users
    resources :orders do
      resources :line_items
    end
 
    resources :line_items
    resources :carts
    resources :products do
      get :who_bought, :on => :member
    end
    root :to => 'store#index', :as => 'store'
  end
end
echo "Product.find(2).title" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> Product.find(2).title
=> "CoffeeScript"
>> 
echo "p = Product.find(2)\\nputs p.price\\np.price -= 5\\np.save" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> p = Product.find(2)
=> #<Product:0x7f6cf155ede8 @prefix_options={}, @attributes={"price"=>#<BigDecimal:7f6cf1515fa8,'0.36E2',9(18)>, "created_at"=>Fri Jun 29 16:29:59 UTC 2012, "image_url"=>"/images/cs.jpg", "title"=>"CoffeeScript", "updated_at"=>Fri Jun 29 16:29:59 UTC 2012, "id"=>2, "description"=>"<p>\n        CoffeeScript is JavaScript done right. It provides all of JavaScript's\n\tfunctionality wrapped in a cleaner, more succinct syntax. In the first\n\tbook on this exciting new language, CoffeeScript guru Trevor Burnham\n\tshows you how to hold onto all the power and flexibility of JavaScript\n\twhile writing clearer, cleaner, and safer code.\n      </p>"}>
>> puts p.price
36.0
=> nil
>> p.price -= 5
=> #<BigDecimal:7f6cebbb01c0,'0.31E2',9(27)>
>> p.save
=> true
>> 

expire cache

rm -rf /home/rubys/git/awdwr/edition4/work-30/depot/tmp/cache

fetch storefront

get /

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$31.00
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95

fetch product (fallback in case storefront is cached)

get /login
Please Log In
post /login?locale=en
You are being redirected.
get http://localhost:3000/admin?locale=en

Welcome

It's Fri Jun 29 12:38:36 -0400 2012 We have 102 orders.
get /products/2

Title: CoffeeScript

Description: <p> CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code. </p>

Image url: /images/cs.jpg

Price: 31.0

Edit | Back
edit app/models/order.rb
class Order < ActiveResource::Base
  self.site = 'http://dave:secret@localhost:3000/'
end
echo "Order.find(1).name\\nOrder.find(1).line_items\\n" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> Order.find(1).name
=> "Dave Thomas"
>> Order.find(1).line_items
NoMethodError: undefined method `line_items' for #<Order:0x7f6491c7bb98>
	from /home/rubys/git/rails/activeresource/lib/active_resource/base.rb:1401:in `method_missing'
	from (irb):2
	from :0
>> 
edit app/models/line_item.rb
class LineItem < ActiveResource::Base
  self.site = 'http://dave:secret@localhost:3000/orders/:order_id'
end
post /logout?locale=en
You are being redirected.
get http://localhost:3000/en

Logged out

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$31.00
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95
echo "LineItem.find(:all, :params => {:order_id=>1})" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> LineItem.find(:all, :params => {:order_id=>1})
=> [#<LineItem:0x7f308c51d010 @prefix_options={:order_id=>1}, @attributes={"price"=>#<BigDecimal:7f308c524f90,'0.36E2',9(18)>, "created_at"=>Fri Jun 29 16:32:51 UTC 2012, "product_id"=>2, "quantity"=>1, "updated_at"=>Fri Jun 29 16:33:20 UTC 2012, "id"=>10, "cart_id"=>nil}>, #<LineItem:0x7f308c51cf70 @prefix_options={:order_id=>1}, @attributes={"price"=>#<BigDecimal:7f308c520be8,'0.36E2',9(18)>, "created_at"=>Fri Jun 29 16:36:24 UTC 2012, "product_id"=>2, "quantity"=>2, "updated_at"=>Fri Jun 29 16:36:35 UTC 2012, "id"=>11, "cart_id"=>nil}>]
>> 
echo "li = LineItem.find(:all, :params => {:order_id=>1}).first\\nputs li.price\\nli.price *= 0.8\\nli.save" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> li = LineItem.find(:all, :params => {:order_id=>1}).first
=> #<LineItem:0x7fb464d013d8 @prefix_options={:order_id=>1}, @attributes={"price"=>#<BigDecimal:7fb464d09358,'0.36E2',9(18)>, "created_at"=>Fri Jun 29 16:32:51 UTC 2012, "product_id"=>2, "quantity"=>1, "updated_at"=>Fri Jun 29 16:33:20 UTC 2012, "id"=>10, "cart_id"=>nil}>
>> puts li.price
36.0
=> nil
>> li.price *= 0.8
=> 28.8
>> li.save
=> true
>> 
get /orders/1/line_items.xml
<?xml version="1.0" encoding="UTF-8"?>
<line-items type="array">
  <line-item>
    <price type="decimal">28.8</price>
    <created-at type="datetime">2012-06-29T16:32:51Z</created-at>
    <product-id type="integer">2</product-id>
    <quantity type="integer">1</quantity>
    <order-id type="integer">1</order-id>
    <updated-at type="datetime">2012-06-29T16:33:20Z</updated-at>
    <id type="integer">10</id>
    <cart-id type="integer" nil="true"></cart-id>
  </line-item>
  <line-item>
    <price type="decimal">36.0</price>
    <created-at type="datetime">2012-06-29T16:36:24Z</created-at>
    <product-id type="integer">2</product-id>
    <quantity type="integer">2</quantity>
    <order-id type="integer">102</order-id>
    <updated-at type="datetime">2012-06-29T16:36:35Z</updated-at>
    <id type="integer">11</id>
    <cart-id type="integer" nil="true"></cart-id>
  </line-item>
</line-items>
echo "LineItem.format = :xml\\nli = LineItem.find(:all, :params => {:order_id=>1}).first\\nputs li.price\\nli.price *= 0.8\\nli.save" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> LineItem.format = :xml
=> :xml
>> li = LineItem.find(:all, :params => {:order_id=>1}).first
=> #<LineItem:0x7fc21796b348 @prefix_options={:order_id=>1}, @attributes={"price"=>#<BigDecimal:7fc2179732c8,'0.288E2',18(18)>, "created_at"=>Fri Jun 29 16:32:51 UTC 2012, "product_id"=>2, "quantity"=>1, "updated_at"=>Fri Jun 29 16:33:20 UTC 2012, "id"=>10, "cart_id"=>nil}>
>> puts li.price
28.8
=> nil
>> li.price *= 0.8
=> 23.04
>> li.save
=> true
>> 
echo "li2 = LineItem.new(:order_id=>1, :product_id=>2, :quantity=>1, :price=>0.0)\\nli2.save" | IRBRC=tmp/irbrc rails console
Loading development environment (Rails 3.0.15)
>> li2 = LineItem.new(:order_id=>1, :product_id=>2, :quantity=>1, :price=>0.0)
=> #<LineItem:0x7fbbd8431e80 @prefix_options={:order_id=>1}, @attributes={"price"=>0.0, "product_id"=>2, "quantity"=>1}>
>> li2.save
=> true
>> 

25.1 rack 22 Caching