McGivrer’s Home

McGivrer’s Home

Frédéric Delorme  //  Java/J2EE Expert
Technical Manager
Web Standard evangelizer

Feb 15 / 4:46pm

Hobo: step2 and 3, add categories and some relation

Ok, we have just created the first step of our library.
Now we have hundreds of books, maybe we can dispatch all these books in some categories ?
Ok, l'est add categories to our web app "My Library"
 script/generate hobo_model_resource category name:string sorder:integer
And we hjev to update the databse with our new model:
 script/generate hobo_migration
don't forget the 'm' option to run the migration right now !
ok, just for fun, run the application with a 
 script/server
browse to http://localhost:3000/ and go to the "Categories" tab..  
1.View the Category tab
2.and add a new "Novel" category
yes ok, nothing really new here.  It's working just like Books.
Let's dye into the code:
We are going to add the link between Book and Category, knwoing that a category can have many book, and one book belong to one category.
Open the file book.rb and just after fields declaration, add the following "belongs_to": 
 class Book < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    title  :string
    author :string
    year   :integer
    resume :text
    note   :integer
    timestamps
  end

  belongs_to :category
  ...
 end
Open now the category.rb and add the small "has_many" property:
 class Category < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    name   :string
    sorder :integer
    timestamps
  end

  has_many :books
  ...
 end
we have to generate a small migration to add this field into the Book data model.
 script/generate hobo_migration
note: choose the 'm' option to apply modifications.
and start the server to test !
 script/server
let's browse the Books page
And choose one of the created book, you can see the new "Category" field at end of page.
Click the "edit book" link and you will be invited to change the value for category wich is currently set with the default "(No category)".
ok, back to your code editor and open the file category_hints.rb. and replace all the commented lines by the following single line. 
 class CategoryHints < Hobo::ViewHints

   children :books

 end
Telling that Category has Books has children will modify automatically the Category page, showing the number of book attached to a category.
1. What's new in the "Categories" page
2. Let's see the detail of a Category
3. And on the Book side, a new top/left link appear "<<Novel"
you can see that with  very small add to the generated code, you considerably inflect the default behaviour of the application and the links in the model.
Even if model link declaration and manipulation already exists in Rails, Hobo brings the corresponding mechanisms for the UI side.
Next, more to be discover in the dryml taglib and tags personalization.
Hobo man.
Note: please find attached bellow the project in each step state, from step 1 to step 3.

Click here to download:
MyLibrary-step-1.tar.gz (120 KB)

Click here to download:
MyLibrary-step-2-categories.tar.gz (122 KB)

Click here to download:
MyLibrary-step-3-hints.tar.gz (128 KB)

0 comments

Leave a comment...