Agile Web Development with Rails, Edition 5

12.4 Playtime 12.2 Iteration G2: Atom Feeds

12.3 Iteration G3: Downloading an eBook

demonstrate streaming with ActionController::Live

add a route for downloading a product

edit config/routes.rb
Rails.application.routes.draw do
  resources :orders
  resources :line_items
  resources :carts
  get 'store/index'
 
  resources :products do
    get :download, :on => :member
    get :who_bought, on: :member
  end
 
  # For details on the DSL available within this file, see
  # http://guides.rubyonrails.org/routing.html
 
  # Serve websocket cable requests in-process
  # mount ActionCable.server => '/cable'
  root 'store#index', as: 'store'
 
end

mock streaming implementation for download

edit app/controllers/products_controller.rb
  include ActionController::Live
  def download
    response.headers['Content-Type'] = 'text/plain'
    40.times do |i|
      response.stream.write "Line #{i}\n\n"
      sleep 0.10
    end
    response.stream.write "Fini.\n"
  ensure
    response.stream.close
  end

add order to the session

edit app/controllers/orders_controller.rb
  def create
    @order = Order.new(order_params)
    @order.add_line_items_from_cart(@cart)
 
    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        session[:order_id] = @order.id
        format.html { redirect_to store_url, notice: 
          'Thank you for your order.' }
        format.json { render :show, status: :created,
          location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors,
          status: :unprocessable_entity }
      end
    end
  end

render order in the side bar

edit app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
  <title>Pragprog Books Online Store</title>
  <%= stylesheet_link_tag    "application", media: "all",
    "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body class="<%= controller.controller_name %>">
  <div id="banner">
    <%= image_tag("logo.png") %>
    <span class="title"><%= @page_title || "Pragmatic Bookshelf" %></span>
  </div>
  <div id="columns">
    <div id="side">
      <% if @cart %>
        <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
          <%= render @cart %>
        <% end %>
      <% end %>
 
      <%= render Order.find(session[:order_id]) if session[:order_id] -%>
 
      <ul>
        <li><a href="http://www....">Home</a></li>
        <li><a href="http://www..../faq">Questions</a></li>
        <li><a href="http://www..../news">News</a></li>
        <li><a href="http://www..../contact">Contact</a></li>
      </ul>
    </div>
    <div id="main">
      <%= yield %>
    </div>
  </div>
</body>
</html>

implement order partial

edit app/views/orders/_order.html.erb
<div id="order">
<h2>Your Downloads</h2>
<table data-no-turbolink>
  <% order.line_items.each do |item| %>
  <tr>
    <td><%= link_to item.product.title, download_product_path(item.product) %></td>
  </tr>
  <% end %>
</table>
</div>

css tweaks

edit app/assets/stylesheets/application.scss
  #side {
    padding: 1em 2em;
    background: #141;
 
    form, div {
      display: inline;
    }  
 
    input {
      font-size: small;
    }
 
    #cart, #order {
      font-size: smaller;
      color:     white;
 
      a, a:hover {
        color: white;
        background-color: #141;
      }
 
      table {
        border-top:    1px dotted #595;
        border-bottom: 1px dotted #595;
        margin-bottom: 10px;
      }
    }
 
    ul {
      padding: 0;
 
      li {
        list-style: none;
 
        a {
          color: #bfb;
          font-size: small;
        }
      }
    }
  }

place an order

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.

$36.00
Ruby

Programming Ruby 1.9 & 2.0

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
post /line_items?product_id=2
You are being redirected.
get http://localhost:3000/

Your Cart

CoffeeScript $36.00
Total $36.00

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.

$36.00
Ruby

Programming Ruby 1.9 & 2.0

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
get /orders/new

Your Cart

CoffeeScript $36.00
Total $36.00
Please Enter Your Details
post /orders
You are being redirected.
get http://localhost:3000/

Your Downloads

CoffeeScript

Thank you for your order.

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.

$36.00
Ruby

Programming Ruby 1.9 & 2.0

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

click download

get /products/2/download

cleanup - precompile assets for inclusion in results

rails assets:precompile
I, [2016-05-06T21:44:44.968243 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/cs-f56ef62bc41b040664e801a38f068082a75d506d9048307e8096737463503d0b.jpg
I, [2016-05-06T21:44:44.969304 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/logo-c6cc13d09c4262e637d9af74fa20f7cc0ba8cb57864863b3ce3135f37cbf308c.png
I, [2016-05-06T21:44:44.970128 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/rails-5ad6d454af23f597c4c39dbe4ec6e12784f8e382997a4bc04944ab8547d48881.png
I, [2016-05-06T21:44:44.971137 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/rtp-d3d2240713cce773840785de837acf822d235d1b77040a3e1ed9d8c014cff086.jpg
I, [2016-05-06T21:44:44.971906 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/ruby-836fee16e3757bcd4e2c21d064eee4fa81d7f93e4e9a44b1ffcd6c1d41c92a88.jpg
I, [2016-05-06T21:44:45.030824 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/application-c3bf8f6f4fe2195a9e9e6ab386624ea1ad8c0ba319310b5bb657f18d4a82c9cd.js
I, [2016-05-06T21:44:45.030970 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/application-c3bf8f6f4fe2195a9e9e6ab386624ea1ad8c0ba319310b5bb657f18d4a82c9cd.js.gz
I, [2016-05-06T21:44:45.053969 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/application-eb0662fc4ec6d1bf2c876be25e4007dfc57bdbf1ff75633b227250b68d818115.css
I, [2016-05-06T21:44:45.054136 #4904]  INFO -- : Writing /home/rubys/git/awdwr/edition4/work-224-500beta3/depot/public/assets/application-eb0662fc4ec6d1bf2c876be25e4007dfc57bdbf1ff75633b227250b68d818115.css.gz

make sure that nothing is broken

rails test
Run options: --seed 33969
 
# Running:
 
........................................
 
Finished in 0.653140s, 61.2426 runs/s, 140.8580 assertions/s.
 
40 runs, 92 assertions, 0 failures, 0 errors, 0 skips

12.4 Playtime 12.2 Iteration G2: Atom Feeds