Database Articles

  Home arrow Database Articles arrow Handling Publishers in a Book Inventor...
DATABASE ARTICLES

Handling Publishers in a Book Inventory Management System
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2011-10-05

    Table of Contents:
  • Handling Publishers in a Book Inventory Management System
  • Completing the View Publisher User Story

  •  
     

    SEARCH CODEWALKERS

    Handling Publishers in a Book Inventory Management System


    (Page 1 of 2 )

    In this third part of a multi-part series on the scaffolding feature of Ruby on Rails, we'll pick up where we left off last time, and complete the add publisher user story and the view publisher user story. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

    Completing the Add Publisher User Story

    The scaffolding script has created a functional test in test/functional/admin/publisher_controller_test.rb. On closer inspection, we can see that it requires some modifications for it to be helpful in our efforts at producing bug-free code. For example, the test_createmethod doesnít specify a name for the publisher it creates. This should have made the test fail when we ran the test, but it didnít. We will fix that soon, but the first thing we will do is add validations. You never know what kind of data a user will try to enter into your application. As explained in the previous chapter, validations help ensure that only valid data is inserted into the database.

    Adding Validations to the Model

    Adding validations will make the test fail and show you where the parameters should be specified. So letís begin by adding a validation for thenamefield in thePublishermodel. Openapp/models/publisher.rband modify it to look as follows:

    class Publisher < ActiveRecord::Base
     
    validates_length_of :name, :in => 2..255
    validates_uniqueness_of :name
    end

    As you might remember, we specified the maximum length of thenamefield in thepublisherstable to be 255 characters. We add a validation to the model to verify this constraint and specify that the minimum length of thenamefield to be 2 characters. We also add a validation that checks that the publisher name is unique.

    Modifying the Generated Fixture Data

    The fixture data generated by scaffolding is not very descriptive of our project. We can do better. Opentest/fixtures/publishers.ymlin your editor and remove everything from the file. Then add the following:

    apress:
    id: 1
    name: Apress

    Again, execute the functional tests withrake test:functionals. You should see the tests fail with the following error message:

    --------------------------------------------
    Expected response to be a <:redirect>, but was <200>
    --------------------------------------------

    The test fails as expected because thetest_createmethod doesnít provide any parameters to thepost method that is supposed to create the publisher. To fix this, weíll change the functional test.

    Modifying the Generated Functional Test

    Opentest/functional/admin/publisher_controller_test.rbin your editor and change thetest_createmethod as follows:

    def test_create
    num_publishers = Publisher.count

    post :create, :publisher => {:name => 'The Monopoly Publishing Company'}

    assert_response :redirect
    assert_redirected_to :action => 'list'

    assert_equal num_publishers + 1, Publisher.count
    end

    The only change is that we pass a hash to thepost method, instead of no data at all. The hash contains the name for the publisher the test should create.

    Run the tests again, and you should see the functional test pass. You should also do a quick test in the browser to verify that the Add Publisher user story functionality works. Openhttp://localhost:3000/admin/publisher/newin your browser. Test the validations you just added by clicking the Create button, without specifying a name. You should see the error message shown in Figure 3-2. This error message is automatically generated by Rails, and explains exactly what you should do in order to fix the error.


    Figure 3-2. Testing the Add Publisher user story

    We further examine the functional tests and see that those for the List Publishers and Delete Publisher user stories are satisfactory. However, the test for the View Publisher and Edit Publisher user stories require some work.

    More Database Articles Articles
    More By Apress Publishing

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - MariaDB, a MySQL Alternative, Opens Foundati...
    - Oracle Supporting HP Itanium Again, Finally
    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap