McGivrer’s Home

McGivrer’s Home

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

Mar 16 / 12:14pm

Hobo: Step 4 - adding a cover image

As we've now get a running web application to manage our library, I propose you to add an image field management to our Book model.
To manage this part, we are going to need 3 things:
  • an imaging tool like mini_magick or image_magick,
  • the paperclip plugin for rail
  • and the paper_clip_with_hobo to be used with Hobo framework.
1. The image tool
My choice go to imagemagick, but i know that this is working well with others tools like

2. Paperclip plugin
You will find very interesting infoirmation about this plugin usage at this page: http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/
And you  will be able to download it from http://github.com/thoughtbot/paperclip

To install this small plugin, just go to your hobo application root path and run the following command:

script/plugin install git://github.com/thoughtbot/paperclip.git
Now you have downloaded it, you are ready to use it.

3. Get the paperclip_with_hobo
Just fololowing the same process, you have to download/install this rails plugin from git:

script/plugin install git:http://github.com/tablatom/paperclip_with_hobo.git


ok. let's apply some model modification to our Book modelisation.

Book.rb

we need to add some new field to our app/model/book.rb

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

has_attached_file :cover, :styles => {
:full=>"600x600>",
:medium => "300x300>",
:thumb => "100x100>",
:mini => "60x60>" },
:path => " :rails_root/public/media/books/covers/:id/:style.:extension",
:url => "media/books/covers/:id/;style.:extension"
...
end

Adding such "has_attached_file" field to our Book model add in reality 4 fields to our database model after running the rake db:migrate standard rails operation :

see db/migrate/20100221172242_add_attachements_cover_to_book.rb migration file

class AddAttachmentsCoverToBook < ActiveRecord::Migration
def self.up
add_column :books, :cover_file_name, :string
add_column :books, :cover_content_type, :string
add_column :books, :cover_file_size, :integer
add_column :books, :cover_updated_at, :datetime
end

def self.down
remove_column :books, :cover_file_name
remove_column :books, :cover_content_type
remove_column :books, :cover_file_size
remove_column :books, :cover_updated_at
end
end


Ok, now our model is updated, database too. we are ready to modify some dryml file to show this image.

Go to app/views/taglibs/application.dryml and add the following declaration:

At file beginning in existing "include" lines:

<include src="paperclip" plugin="paperclip_with_hobo"/>

And at end of file :

<extend tag="form" for="Book">
<old-form merge multipart>
<field-list fields="title, author, year, resume, note, cover, category" param/>
<div param="actions">
<submit label="#{ht 'books.actions.save', :default=>['Save']}" param/><or-cancel param="cancel"/>
</div>
</old-form>
</extend>

This is a view modifier for a Book, adding the cover field and removing the 4 database fields.

Then, modify the card dryml for Book model, in the same previous file (application.dryml) :

<extend tag="card" for="Book">
  <card>
    <h5><a /></h5>
    <p><view:title /></p>
    <p>
     
      <view:resume />
    </p>
    <p>
      <%= I18n.t :uploaded_label %><br />
      <view:created_at format="%B %d, %Y" /> by <you:user />.
    </p>
    <delete-button class="actions" />
   
  </card>
</extend>

Let's test our new cover field !
Filed under  //  development   hobo   image   rad   rails   tutorial   web  

Comments (0)

Mar 7 / 4:42am

RedCar, it's not a red car !

RedCar is a brand new programmer's editor ruby oriented, and ruby developed.
And on of the main feature of this new text editor, it's to be JRuby compatible natively !  A good news for the multiplatform world.


to be tested to help dev team to improve this cool programmer's tool.

Is it possible to optimize such editor for Rails and Hobo ?

McG.
Filed under  //  code   editor   hobo   java   linux   multiplatform   rails   tools   windows  

Comments (0)

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)

Filed under  //  hints   hobo   rad   rails   relation   tutorial   web  

Comments (0)

Feb 15 / 2:52pm

My first Hobo application: My Library

Hello, just as I have published the previous article about Hobo, discovering the full featured rails over layer framework, I can't wait to publish my first test: creating a very simple Web Application to manage a library.

The first step is just to provide an interface to list all my books
So please, follow the guide !
Installation of the framework
Just run the following command in your prefered terminal (gnome-terminal for example in Ubuntu):
 sudo gem install rails
 sudo gem install hobo
 sudo gem install sqlite3-ruby -v=1.2.3
 sudo apt-get install sqlite3
First App
Now we are going to create our first Hobo application
 hobo MyLibrary
focus in the just create app directory
 cd MyLibrary
And now create our first datamodel :  a book modelisation
 script/generate hobo_model_resource book title:string author:string year:integer resume:text note:integer
now, we are going to create this model persistence into the default sqlite3  database
 script/generate hobo_migration 
(choose the 'm' option)
Ok, done.  now run the rails server (WebBrick):
 script/server
Look at the title of the application :"My Library" !  Just because we call our project "MyLibrary" (without any space), Hobo kindly titled our welcome page with a beautiful "My Library" in a plain old good english. 
Identify yourself by creating the first user account which will act as administrator for this new application.
And now start playing with your books collection !
1. your first connection to "My Library"
2. The welcome page (also modifiable 
3. Let see our books collection
4. Go and create our first book in our collection
5. Fill all the fields, and "Create Book"
6. and it's done, the first book is ready to be parsed
Yes I am impressed to discover how the Hobo dev team has been very careful with all details.
The new Hobo player.
Filed under  //  firstapp   framework   hobo   installation   rails  

Comments (0)

Jan 26 / 4:39pm

How to install rmagick extension to ruby on Ubuntu ?

That's a very good question as the rubygems embedded in the debian package is a limited edition where you are not able to make an "update --system".

So first thing to do:

 sudo apt-get remove rubygems

Then, from www.rubyonrails.com, follow the rubygems installation tutorial:
- download the archive delivered from rubyforge,
- tar -zxvf rubygems-1.3.1.tgz

and:
 cd /rubygems-1.3.1
 sudo ruby setup.rb

and follow with a :
 sudo gem update --system

OK, rubygems is now up to date.

Go on installation of rmagick:

 sudo apt-get install ruby1.8-dev
 sudo apt-get install g++ automake autoconf
 sudo apt-get install libmagick++10
 sudo apt-get install libmagick++9-dev
 sudo gem install rmagick

and then you will get the following console output:

frederic@nostromo2:~/Logiciels/development/ruby/rubygems-1.3.1$ sudo gem install rmagick
Building native extensions.  This could take a while...
Successfully installed rmagick-2.9.0
1 gem installed

And now, you will be able to use the Rails plugin named file_column; cd into your rails project:
 cd myprojectsrails

 script/plugin install http://opensvn.csie.org/rails_file_column/plugins/file_column/trunk


And enjoy !
Filed under  //  file_column   imagemagick   rails   rmagick   ruby   ubuntu  

Comments (0)