Port of an J2EE application (with Hibernate) to Ruby on Rails (1.2.3)

Last week, I ported a J2EE application, using Hibernate for persistence and Capestry for the MVC part of the application to Ruby.
I logically choosed Ruby on Rails as it is the main Web framework for Ruby.
In this post, I describe how I did to perform such a port…
!Important! the application I first wrote this application in Franch, I tried to refactor the code in English, but… I haven(t finished at this time, … sorry folks !

Introducing the bank on line application

This application is composed of 3 models : Client, Compte (account) and Operation
A client has n Comptes (through reference_client) and a Compte has n Operations (through compte_id) :
Bank on line
The application allow the user to :
*login
*see a list of his accounts (after being logged in)
*see the details of one of his account (actually a list of the operations on an account)
*order a transfer between 2 accounts provided that the balance is positive (and the accounts exist)
Quite simple from a user point of view…

Creating the application with Ruby on Rails 1.2.3

The best way to port this new application to RoR, was to create a new RoR project
rails bankonline -d sqlite3
(by the way, I was using the sqlite3 database, to allow an easy installation of the application, the J2EE application was using HSQLDB)
cd bankonline
I was using for this Rad Rails visual editor on a windows platform (no comment…)
The first step to port this application was to create the 3 models.
ruby script/generate model Client
ruby script/generate model Compte
ruby script/generate model Operation
Then I put the contraints on each of these models (the has_many and belongs_to relationships to stick to my diagram), editing their definition files in app/models and also the database definition files, in db/migrate
Once this is done, I can run
rake db:migrate
to load all my db/migrate scripts. (in case I want to revert the changes, I can use rake db:migrate VERSION=0, or VERSION=1 if only want to reverse the migration after the script)

To fill the database with data, I add another migration script :
ruby script/generate migration add_data
I edit it to add some lines in the database and then I migrate :
rake db:migrate

Then the 3 corresponding controllers :
ruby script/generate controller login
ruby script/generate controller gestion

Implementing the login system (session based)

For this, I took example on the depot application login system, from the pragmatic programmers’ book « Agile Web Development with Rails », but I chose to use not encrypted passwords.
I also chose to use database based sessions, so I edited the config/environment.rb file, line 31, to allow :
config.action_controller.session_store = :active_record_store
Then, I add to create a new table to hold the session data :
rake db:sessions:create

Of course, you need a little bit of code in your app/controllers/login_controller.rb, you have to implement the login and logout methods (or actions, to speak rails); you may also notice the apparition of a « before_filter » call.
The authorize filter is defined in the app/controllers/application.rb file, it will be loaded for every controller, and will check tha t a valid client is in session, or else, it will redirect to the login action, in the login controller.
Finally, you will have to define the views, both in app/views/login, they’re called login.rhtml and logout.rhtml
One last thing : you also need to modify the layout, in app/views/layouts/login.rhtml to display the flash notice before the yield (which includes the display of the view)

<%= flash[:notice] %>

Implementing the account listings and the transfer functionality

We have to code the controller, GestionController, located in gestion_controller.rb
You’ll notice that I’ve replaced the service class in Hibernate by methods inside the controller in Rails, I didn’t want to put it in the model, since the model can’t know how to transfer money.(I think we can discuss that point, but as long as the helpers are dedicated to the view…)
I’ve also used exceptions to raise errors (if the origin account balance is smaller than the transfer amount)
You also have to include the flash notice in the layout, app/views/layouts/gestion.rhtml

That’s it !
launch your rails app
ruby script/server
and try it out in your web browser at : http://localhost:3000/login/login

In a few days I’ll complete this article with the tests and fitnesse fixtures …
To continue …

First version of Bank online RoR to download

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *