Home arrow Ruby-on-Rails arrow Page 3 - Checkout and Order Processing: the Administrative Side
RUBY-ON-RAILS

Checkout and Order Processing: the Administrative Side


In this fourth part of a five-part series that shows you how to add checkout and order processing capabilities to an ecommerce application based on Ruby-on-Rails, you'll learn how to integrate a PayPal alternative, build the administrative user interface, and more. This article is excerpted from chapter nine of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
July 16, 2010
TABLE OF CONTENTS:
  1. · Checkout and Order Processing: the Administrative Side
  2. · Using the Payment Gem
  3. · Implementing the Administrator User Stories
  4. · Implementing the View Order User Story

print this article
SEARCH DEVARTICLES

</td>
    <td align="right"><%= order.status %></td>
    <td align="right"><%= order.total %></td>
    <td align="right"><%= order.books.size %></td>
    <td align="right"><%= order.created_at.strftime("%Y-%m-%d %I:%M") %></td>
    <td align="right"><%= order.updated_at.strftime("%Y-%m-%d %I:%M") %></td>
    <td><%= button_to "View", :action => 'show', :id => order %></td>
  </tr>
<% end %>
</table>
<%= 'View page:' if @order_pages.page_count > 1 %>
<%= pagination_links @order_pages %>

At the top of page, we include the navigation. Then we loop through the orders and display them in a table. We also include a button that takes us to the order details page, which we値l implement shortly. At the very bottom of the page, we use thepagination_linkshelper method to generate a menu that is used to navigate between the pages. Each page displays ten orders, and if there are more, a menu like this will be displayed:

--------------------------------------------
View page: 1 2 3 4 5 6 7 8 9 10
--------------------------------------------

That was easy! We have now implemented the View Orders user story. Before moving on to the next user story, let痴 do a quick acceptance test, by openinghttp://localhost:3000/admin/order. If you (or your customers) have created some orders, you値l see something similar to Figure 9-13.


Figure 9-13.  The view orders page displaying all orders

If you navigate to the page that shows processed orders (http://localhost:3000/admin/ order/processed), you値l see orders that the system has sent to the payment gateway, but which George still needs to ship to the customer and close. An example of this page is shown in Figure 9-14.


Figure 9-14.  The view orders page showing only processed orders


Note  For the administrator user stories, we won稚 walk through functional and integration tests. At this point, you should be familiar with developing these tests and able to write them all by yourself.



blog comments powered by Disqus
Checkout and Order Processing: the Administrative Side - Implementing the Administrator User Stories
(Page 3 of 4 )

 

Next, we値l build the administrator interface for managing orders. After the system has sent the payment request to the payment gateway, George needs to log in to the payment gateway and verify that the transaction has been settled, meaning that the funds have been transferred from the customer to George痴 bank account. There might be a delay between the request and when the funds are actually transferred.

When George goes to the order administration interface, he wants to see a list of all orders, sorted by the date they were created. He also wants to list orders by status, so we値l create a view for listing all orders, as well as individual views for listing orders by their status:open,processed,closed, andfailed.

Implementing the View Orders User Story

The first step in implementing this View Orders user story is to generate a controller:

$ script/generate controller 'admin/order' index show close

--------------------------------------------
  exists  app/controllers/admin
  exists  app/helpers/admin
  exists  app/views/admin/order
  exists  test/functional/admin
  create app/controllers/admin/order_controller.rb
  create  test/functional/admin/order_controller_test.rb
  create  app/helpers/admin/order_helper.rb
  create  app/views/admin/order/index.rhtml
  create  app/views/admin/order/show.rhtml
  create  app/views/admin/order/close.rhtml
--------------------------------------------

Openapp/controllers/admin/order_controller.rband replace the emptyindexmethod with the code shown here:

class Admin::OrderController < ApplicationController

  def index
   
@status = params[:id]
   
if @status.blank?
     
@status = 'all'
      conditions = nil
    else
      conditions = "status = '#{@status}'"
    end

    @page_title = "Listing #{@status} orders"
    @order_pages, @orders = paginate :orders, :per_page => 10,➥
:order => 'created_at desc', :conditions => conditions
  end

First, we retrieve thestatusparameter, which we値l use to filter the list of orders. If it is blank, we simply show all orders. If thestatusparameter is not blank, then we use it to build theconditionsparameter for thepaginatemethod. We also set theorderparameter so that the list is sorted.

Next, we値l create a menu that allows George to filter the list of orders easily. Save the code shown here inapp/views/admin/order/_navigation.rhtml:

<p>
  View: <%= link_to "all", :id => '' %>,
  <%= link_to "open", :id => 'open' %>,
  <%= link_to "processed", :id => 'processed' %>,
  <%= link_to "closed", :id => 'closed' %>,
  <%= link_to "failed", :id => 'failed' %>

</p>

We値l later use the same partial on the order details page, but for now, we値l just use it on the view orders page (app/views/admin/order/index.rhtml), which is shown here:

<%= render :partial => 'navigation' %> <table>
  <tr>
    <th>ID</th>
    <th>Status</th>
    <th>Total amount</th>
    <th>Size</th>
    <th>Created at</th>
    <th>Updated at</th>
    <th></th>
  </tr>
<% for order in @orders %>
  <tr>
    <td align="right"><%= order.id %>

RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials