Thursday, February 4, 2010

Ruby on Rails

I stumbled upon Ruby especially WATIR, Web Application Testing in Ruby, in my current job. Along with Perl, Ruby has been a God send. As the Principal Security resource, I am responsible for developing, maintaining, and managing Application Security for 40,000+ end users and 1000+ back-office staffs with 10 online presences.

The Security requirements have been captured and defined in the MS Excel Security matrix, and it is my job to "execute" those requirements correctly so that Security is correct and Federal laws such as HIPAA and FERPAA are also complied....that means specifically that if one has "Viewer" only access, then he or she can only see the last 4-digits SSN for example.

I will go through that experience in more details in my next blogs but for now...I want to take you to ride the wonderful journey with Rails, the popular Web frameworks, and in the spirit of Superbowl 2010, develop a Web-based NFL team app using Ruby on Rails.

With backgrounds also heavy in Java, I count my blessings and am very excited with the JRuby on Rails.
With JRuby, you have the ability to incorporate the existing Java libraries and code-base that you have, but losing the native (C-compiled) Ruby libraries, also called Ruby gems.

So without further ado,....

Install JRuby

wget http://dist.codehaus.org/jruby/1.2.0/jruby-bin-1.2.0.tar.gz
tar xzvf jruby-bin*
mv jruby-1.2.0 /cygdrive/c/jruby
echo "export PATH="/cygdrive/c/jruby/bin:$PATH"" >> ~/.bashrc
export PATH="/cygdrive/c/jruby/bin:$PATH"
Install Rails
jruby -S gem install rails

Now let us create our application by firing up rails....




Now, since we are working with JRuby, we need to modify the database backend by working with JDBC

So go and edit the config/database.yml file and change the adapter from sqlite3 to jdbcsqlite3 as follows:


In our application, each NFL team would have many players, whereas a player belongs to a team.


Next, use the scaffold facility that is available for both the team and the player model/entity.





Notice that for each singular entity, (team or player), the scaffold creates the plurals version.
team => table teams, teams_controller
player => table players, players_controller

You can verify this DRY, Do Not Repeat, principle by looking at the db/schema.rb


establishing the Associations between the Team and the Player entity is done as follows
in the model itself








create the DB tables by issuing the command

jruby -S rake db:migrate



What I often have to do is to populate those tables in the DB...You can fire up the Webrick or Mongrel Web server that Rails comes with and enter the data manually, after all the CRUD facility is there through scaffolding, or another option is to use the test fixtures





players.yml

allred:
number: 56
name: Allred, Colin
position: LB
team_id: 1

amano:
number: 54
name: Amano, Eugene
position: G/C
team_id: 1

amato:
number: 58
name: Amato, Ken
position: LB/LS
team_id: 1

bakhtiari:
number: 99
name: Bakhtiari, Eric
position: DE
team_id: 1

ball:
number: 98
name: Ball, David
position: DE
team_id: 1

bironas:
number: 2
name: Bironas, Rob
position: K
team_id: 1

britt:
number: 18
name: Britt, Kenny
position: WR
team_id: 1

brown:
number: 79
name: Brown, Kareem
position: DL
team_id: 1

brown:
number: 97
name: Brown, Tony
position: DT
team_id: 1

bulluck:
number: 53
name: Bulluck, Keith
position: OLB
team_id: 1

collins:
number: 5
name: Collins, Kerry
position: QB
team_id: 1

cook:
number: 89
name: Cook, Jared
position: TE
team_id: 1

teams.yml

titans:
id: 1
name: Titans
city: Nashville
sport: football

raiders:
id: 2
name: Raiders
city: Oakland
sport: football

Load the fixtures into the DB through the following command

jruby -S rake db:fixtures:load

Fires up rails and starts riding the Rails experience ................







No comments:

Post a Comment

Followers