Database Articles

  Home arrow Database Articles arrow Completing a Book Inventory Management...
DATABASE ARTICLES

Completing a Book Inventory Management System
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2011-12-07

    Table of Contents:
  • Completing a Book Inventory Management System
  • Completing the Edit Book User Story

  •  
     

    SEARCH CODEWALKERS

    Completing a Book Inventory Management System


    (Page 1 of 2 )

    In this conclusion to a 12-part article series on building a book inventory management system with the scaffolding feature of Ruby on Rails, we'll finish the View Book User and Edit Book User stories. This article is excerpted from chapter three of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

    Completing the View Book User Story

    The View Book user story also needs some cleaning up before George is happy. The code created by the scaffolding displays the values of all database columns directly to the user. This means, for example, that the publisher’s ID is shown instead of the publisher’s name. We’ll fix this and also add code that displays the authors of the book and the book cover.

    Changing the View

    First, changeapp/views/admin/book/show.rhtmlas follows:

    <dl>
    <dt>Title</dt>
    <dd><%= @book.title %></dd>
    <dt>Publisher</dt>
    <dd><%= @book.publisher.name %></dd>
    <dt>Published at</dt>
    <dd><%= @book.published_at.strftime("%m/%d/%Y at %I:%M%p") %></dd>
    <dt>Authors</dt>
    <dd><%= @book.authors.collect{|author| author.name }.join(', ') %></dd>
    <dt>ISBN</dt>
    <dd><%= @book.isbn %></dd>
    <dt>Blurb</dt>
    <dd><%= textilize @book.blurb %></dd>
    <dt>Price</dt>
    <dd><%= @book.price %></dd>
    <dt>Page count</dt>
    <dd><%= @book.page_count %></dd>
    <dt>Cover image</dt>
    <% if @book.cover_image.nil? %>
    <dd>N/A</dd>
    <% else %>
    <dd><%= image_tag url_for_file_column(:book, :cover_image) %></dd>
    <% end %>
    </dl>

    <p><%= link_to "Edit", :action => "edit", :id => @book %> |
    <%= link_to "Back", :action => "list" %></p>

    Note that we useimage_tagand the methodurl_for_file_columnto display the uploaded image of the book cover, but only if it exists. We also format the fieldpublished_atto use a standard format.

    Recall that George wanted the Blurb field to be easy to edit. This is why we have used the Textile markup language in the Blurb field, instead of HTML. The Textile markup we entered in the Blurb field is passed through thetextilizemethod in the view:

    <%= textilize @book.blurb %>

    This translates the Textile markup in the Blurb field to HTML. You’ll see this in action in the next section.


    Note Thetextilizemethod is resource-intensive and should be executed only once (when the object is saved). The resulting HTML should be stored in a database field, for example,blurb_html. The conversion can easily be done using abefore_savefilter in the Book model, and then changing the view to display theblurb_htmlcolumn’s value, instead of running the conversion for each request.


    Changing the Controller

    There’s one more thing to fix. The view expects to find the instance variablepage_title, which means you should change the controller’sshow action, as follows:

    def show
    @book = Book.find(params[:id])

    @page_title = #{@book.title}
    end

    You can now access the book details page by clicking the Show link located next to a book on the books list page. Figure 3-10 shows the page after all the changes have been done. Note that the uploaded image is shown at the bottom of the page.


    Figure 3-10. Testing the View Book user story

    Another thing to note about Figure 3-10 is that the Blurb field shows a heading, a bulleted, and a numbered list. In Figure 3-10, we entered the following into the Blurb field:

    h1. This is a heading

    * Item 1
    ## Item 1.1
     
    * Step 2
    ## Step 2.1

    Tip Seehttp://en.wikipedia.org/wiki/Textile_(markup_language)for more information about the Textile markup language.


    Adding an Integration Test

    We’ll also add an integration test for the View Book user story. This is a simple test that verifies that the page doesn’t throw an error. Add the following code to the DSL.

    def show_book(book)
    get "/admin/book/show/#{book.id}"
    assert_response :success
    assert_template "admin/book/show"
    end

    Theshow_bookmethod takes a book as a parameter, which it uses to call theshowaction.

    Also add the highlighted line, shown in the following code, to the last line of thetest_book_administrationmethod, right after the linegeorge.list_books:

    george.list_books
    george.show_book ruby_for_dummies
    end

    This calls the test using the book we created earlier in the test. Run the integration test again to verify that it still passes:

    $ test/integration/book_test.rb

    --------------------------------------------
    Loaded suite test/integration/book_test Started
    .
    Finished in 0.531 seconds.

    1 tests, 12 assertions, 0 failures, 0 errors
    --------------------------------------------

    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