Miles to go …

May 4, 2009

TOTD #81: How to configure “database.yml” to be used with both JRuby and MRI ?

Filed under: rails, totd — Tags: , — arungupta @ 9:57 am

In JRuby-on-Rails tutorial at Rails Conf 2009, Nick Sieger shared a nice little tip on how to configure “database.yml” to be usable with both JRuby and MRI:

<% jdbc = defined?(JRUBY_VERSION) ? ‘jdbc’ : ” %>
  adapter: <%= jdbc %>mysql
  encoding: utf8
  reconnect: false
  database: myapp_development
  pool: 5
  username: root
  socket: /tmp/mysql.sock
# …

“JRUBY_VERSION” is defined only if your using JRuby and so the right database adapter is picked up accordingly.

The complete slides for the tutorial are available here. Learn about other related talks here.

Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. An archive of all the tips is available here.

Technorati: conf railsconf lasvegas tutorial jruby ruby rubyonrails

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

April 29, 2009

TOTD #80: Sinatra CRUD application using Haml templates with JRuby and GlassFish Gem

Filed under: glassfish, netbeans, sinatra, totd — Tags: , , — arungupta @ 2:00 am

TOTD #79 showed how to run a trivial Sinatra application using GlassFish Gem. Sinatra provides support for Haml, Erb, Builder, Sass, and Inline templates as described here. This TOTD will show how to get started with creating a Sinatra CRUD application using Haml templates.

Haml is based on one primary principle – Markup should be beautiful because beauty makes you faster.

Get started by installing the Haml gem as:

/tools/jruby-1.2.0 >./bin/jruby -S gem install haml –no-ri –no-rdoc
JRuby limited openssl loaded. gem install jruby-openssl for full support.
Successfully installed haml-2.0.9
1 gem installed

And follow the tutorial, documentation, and reference page for more details.

Sinatra is ORM-agnostic and so any Ruby ORM framework such as ActiveRecord, DataMapper, Sequel, and others. DataMapper with JRuby requires work so this TOTD will show how to use ActiveRecord instead. There is sinatras-hat which allows to create RESTy CRUD apps with Sinatra. There probably are mutiple other ways to create this application but I prefer to understanding the wiring so this blog will use a bare minimal structure.

Anyway, lets get started!

  1. Create the database as:

    ~/tools/jruby/samples/sinatra-sample >mysql –user root
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 664
    Server version: 5.1.30 MySQL Community Server (GPL)

    Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

    mysql> create database hello_development;
    Query OK, 1 row affected (0.00 sec)

    mysql> use hello_development;
    Database changed
    mysql> CREATE TABLE `runners` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `distance` float, `minutes` int(11), `created_at` datetime, `updated_at` datetime);             
    Query OK, 0 rows affected (0.06 sec)

  2. Update “hello.rb” from TOTD #79 such that it looks like:
    require ‘rubygems’
    require ’sinatra’
    require ‘activerecord’

    ## Setup

      :adapter  => “jdbcmysql”,
      :host     => “localhost”,
      :username => “root”,
      :password => “”,
      :database => “hello_development”

    ## Models

    class Runner < ActiveRecord::Base

    ## Controller Actions

    get ‘/hi’ do
      “Hello World!”

    get ‘/’ do
      @runner = Runner.find(:all)
      haml :index

    get ‘/new’ do
      haml :new

    get ‘/:id’ do
      @runner = Runner.find(params[:id])
      if (@runner)
        haml :show
        redirect ‘/’

    post ‘/’ do
      @runner = => params[:distance], :minutes => params[:minutes])
        redirect “/#{}”
        redirect ‘/’

    Firstly, it pulls in the ActiveRecord dependency. Then “ActiveRecord::Base.establish_connection” is used to establish a connection with the previously created database. “Runner” is tagged as a model class by inheriting from “ActiveRecord::Base” and uses the default table name (“runners” in this case). Add four new methods:

    1. Three GET methods to show all the runners, a form to enter new data, and show a particular log entry. Each method requires a HAML template (will be created in next step) to render the information.
    2. One POST method to save the newly created log entry in the database.
  3. Create a new directory “views” and create the following files in that directory. Each file serves as a view and rendered from an action in “hello.rb”.
    1. “index.haml”: Show all the runners

      %h1 Listing all runners …
          %th Distance
          %th Minutes
        – @runner.each do |r|
            %td= r.distance
            %td= r.minutes
        New Runner

    2. “new.haml”: Enter a new entry
      %h1 Adding a new runner log …
      %form{:method=>”post”, :action=>”/”}
        %input{:type=>”text”, :name=>”distance”}
        %input{:type=>”text”, :name=>”minutes”}
        %input{:type=>”submit”, :value=>”Submit”}

    3. “show.haml”: Show a particular log entry
      %h1 Showing a runner log …

      = @runner.distance
      = @runner.minutes
      %a{:href=>”/”}= “Show All!”

      The complete directory structure looks like:


That’s it, now run the application as:

~/tools/jruby/samples/sinatra-sample >../../bin/jruby -S glassfish
Starting GlassFish server at: in development environment…
Writing log messages to: /Users/arungupta/tools/jruby-1.2.0/samples/sinatra-sample/log/development.log.
Press Ctrl+C to stop.

The main page is available at “http://localhost:3000/” and looks like:

Clicking on “New Runner” gives …

Enter the data, and click on “Submit” to show …

Click on “Show All!” to see all the entries added so far …

And after adding few entries the main page looks like …

This application shows Create and Read from the CRUD, it’s fairly easy to add Update and Delete functionality as well but that’s an excercise left for the readers :-)

You’ll hear all about it at Develop with Pleasure, Deploy with Fun: GlassFish and NetBeans for a Better Rails Experience at Rails Conf next week.

Technorati: totd glassfish jruby sinatra crud

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

November 19, 2007

TOTD #18: How to Build The GlassFish v3 Gem for JRuby ?

Filed under: general, totd — Tags: , , , , , , — arungupta @ 12:02 am

Jerome posted the instructions to build GlassFish v3 Gem for JRuby – very simple and easy. A binary version of Gem is available here.

  1. Software pre-requisite
    1. Subversion client (for example Tigris)
    2. Maven 2.0.x
    3. JRuby 1.0.x (I used JRuby 1.0.2 and lets say installed in JRUBY_HOME).  Make sure JRUBY_HOME/bin is in your path.
  2. Build the Gem
    1. As explained in Jerome’s entry, you can check out complete GlassFish v3 workspace or just the Gem code. Here is how you’ll check out only the Gem code on a Windows machine using Tigris Subversion client:

      And after the check out is complete, you’ll see:

    2. Build the gem by giving the command:

      mvn install

      And success is achieved by seeing the following in the console:

      [INFO] (in C:/workspaces/glassfish/gem/target/dependency/glassfish)
      [WARNING] mkdir -p pkg
      [INFO] Successfully built RubyGem
      [INFO] Name: GlassFish
      [WARNING] mv GlassFish-10.0.0-java.gem pkg/GlassFish-10.0.0-java.gem
      [INFO] Version: 10.0.0
      [INFO] File: GlassFish-10.0.0-java.gem
      [INFO] [install:install]
      [INFO] Installing C:\workspaces\glassfish\gem\target\gem-10.0-SNAPSHOT.jar to C:\Users\Arun Gupta\.m2\repository\org\glassfish\distributions\gem\10.0-SNAPSHOT\gem-10.0-SNAPSHOT.jar
      [INFO] [install:install-file {execution: install-gem}]
      [INFO] Installing C:\workspaces\glassfish\gem\target\dependency\glassfish\pkg\GlassFish-10.0.0-java.gem to C:\Users\Arun Gupta\.m2\repository\org\glassfish\distributions\GlassFish-Gem\10.0-SNAPSHOT\GlassFish-Gem-10.0-SNAPSHOT.gem
      [INFO] ------------------------------------------------------------------------
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time: 2 minutes 2 seconds
      [INFO] Finished at: Fri Nov 16 17:56:12 PST 2007
      [INFO] Final Memory: 11M/20M
      [INFO] ------------------------------------------------------------------------

      The Gem is available in target\dependency\glassfish\pkg directory.

  3. Install the Gem
    1. Change to the directory where the Gem is available

      cd target\dependency\glassfish\pkg

    2. Install the Gem as:

      C:\testbed\ruby\jruby-1.0.2\bin\jruby -S gem install GlassFish-10.0.0-java.gem
      Successfully installed GlassFish, version 10.0.0

And use it!

Please leave suggestions on other TOTD that you’d like to see. A complete archive is available here.

Technorati: totd v3 jruby ruby rubyonrails glassfish gem jrubyonglassfish

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

Powered by WordPress