Home arrow Ruby-on-Rails arrow Page 3 - Checkout and Order Processing for an Ecommerce Website

Checkout and Order Processing for an Ecommerce Website

It's vital for an ecommerce website to get the checkout and ordering process correct. This five-part article series shows you how to use Ruby-on-Rails to manage it with very little difficulty. It's 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 / 4
July 08, 2010
  1. · Checkout and Order Processing for an Ecommerce Website
  2. · Implementing the Check Out User Story
  3. · Specifying the Associations
  4. · Adding Validations to the Model

print this article

Checkout and Order Processing for an Ecommerce Website - Specifying the Associations
(Page 3 of 4 )

Next, we want to set up the link between the orders and ordered items. We can do this by first changing theOrdermodel (app/models/order.rb) as shown here:

class Order < ActiveRecord::Base
  attr_protected :id, :customer_ip, :status, :error_message,➥
:updated_at, :created_at

  has_many :order_items
  has_many :books, :through => :order_items

  def total
    order_items.inject(0) {|sum, n| n.price * n.amount + sum}

Next, changeapp/models/order_item.rbas follows:

class OrderItem < ActiveRecord::Base
belongs_to :order
  belongs_to :book

Note that we protect theid,customer_ip,status,error_message,updated_at, andcreated_atfields, so that no one can hack the form and assign a value to these fields, which are used only internally. Protecting the fields is done with theattr_protectedmethod, which we introduced in Chapter 8.

Note  In our example, we allow MySQL to generate the orderid. If you want more complex order numbers, youíll need to generate them yourself.

We have also added a method for calculating the total price of the order. To verify that the relationship between theOrder,Order_Item, andBookmodels are set up correctly, letís create a simple unit test. Opentest/unit/order_test.rband add the test shown in Listing 9-1 to it.

Listing 9-1. Unit Test for Model Relationships

  def test_that_we_can_create_a_valid_order
order = Order.new(
      # Contact Information
      :email => 'abcdef@gmail.com',
      :phone_number => '3498438943843',
      # Shipping Address
      :ship_to_first_name => 'Hallon',
      :ship_to_last_name => 'Saft',
      :ship_to_address => 'Street',
      :ship_to_city => 'City',
      :ship_to_postal_code => 'Code',
      :ship_to_country => 'Iceland',
      # Billing Information
      :card_type => 'Visa',
      :card_number => '4007000000027',
      :card_expiration_month => '1',
      :card_expiration_year => '2009',
      :card_verification_value => '333'
    # Private parts
    order.customer_ip = ''
    order.status = 'processed'

    order.order_items << OrderItem.new(
      :book_id => 1,
      :price => 100.666,
      :amount => 13

    assert order.save


    assert_equal 1, order.order_items.size
    assert_equal 100.666, order.order_items[0].price

Note that we canít set thecustomer_ipandstatus fields in the same way as the other fields, because they are now protected fields.

Before running the test, remember to copy the changes from the development database to the test by executing the following command:

$ rake db:test:clone_structure

If you run the test now, it should generate errors related to the billing information fields.

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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