Agile Web Development with Rails, Edition 4

12.4 Playtime 12.2 Iteration G2: Atom Feeds

12.3 Iteration G3: Downloading an eBook

demonstrate streaming with ActionController::Live

Switch to puma as a server

edit Gemfile
gem 'puma'

Restart the server.

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
 
  # The priority is based upon order of creation:
  # first created -> highest priority.
  # See how all your routes lay out with "rake routes".
 
  # You can have the root of your site routed with "root"
  root 'store#index', as: 'store'
 
  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'
 
  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
 
  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products
 
  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end
 
  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end
 
  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end
 
  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable
 
  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
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.css.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;
        }
      }
    }
  }
}
 
@media all and (max-width: 800px) {
  #columns {
    flex-direction: column-reverse;
  }
}
 
@media all and (max-width: 500px) {
  #banner {
    height: 1em;
  }
 
  #banner .title {
    display: none;
  }

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
Line 0

      
Line 1

      
Line 2

      
Line 3

      
Line 4

      
Line 5

      
Line 6

      
Line 7

      
Line 8

      
Line 9

      
Line 10

      
Line 11

      
Line 12

      
Line 13

      
Line 14

      
Line 15

      
Line 16

      
Line 17

      
Line 18

      
Line 19

      
Line 20

      
Line 21

      
Line 22

      
Line 23

      
Line 24

      
Line 25

      
Line 26

      
Line 27

      
Line 28

      
Line 29

      
Line 30

      
Line 31

      
Line 32

      
Line 33

      
Line 34

      
Line 35

      
Line 36

      
Line 37

      
Line 38

      
Line 39

      
Fini.

Switch back to WEBRick

edit Gemfile
# gem 'puma'

Restart the server.

make sure that nothing is broken

rake test
Run options: --seed 13006
 
# Running:
 
........................................
 
Finished in 0.579213s, 69.0593 runs/s, 165.7422 assertions/s.
 
40 runs, 96 assertions, 0 failures, 0 errors, 0 skips

12.4 Playtime 12.2 Iteration G2: Atom Feeds