Getting started with engines development in rails 2.3

February 4, 2009 4 comments

Rails 2.3 RC1 has just been released. There are number of new features like application templates, Metal points, Rack integration and support for Engines. Earlier engines plugins were not in much favour. But now since merb has similar feature in form of slices they have recently been back in favour and now the engines have made it to core. but not all feature has been ported as pointed out by post on rails engines site.

Lets get started with developing a simple plugin that uses engines feature of rails 2.3 RC1. This tutorial is based on rails flickr demo application. lets install latest 2.3 gems on your machine.


gem install rails --source http://gems.rubyonrails.org

Lets generate empty rails application and run plugin generator. There is no seperate generator for engines as engine plugin is type of plugin. Lets call our plugin flickr_search from application root issue this command


$ script/generate plugin flickr_search

This will generate a flickr_search folder in vendor/plugin which contains whole bunch of files which is empty skeleton for new plugin. since our plugin is based on flickr api you need to install flickr gem and get API key from http://www.flickr.com/services/api/misc.api_keys.html.


$ sudo gem install flickr

Now edit init.rb file in flickr_search folder and write out this code. Make sure you replace your API key in code


require 'rubygems'
require 'flickr'
MY_KEY='Enter your Flicker API Key'
class Flickr
alias old_initialize initialize
def initialize(api_key=MY_KEY, email=nil, password=nil)
puts "new_initialize " + MY_KEY
old_initialize(api_key, email, password)
@host="http://api.flickr.com"
@activity_file='flickr_activity_cache.xml'
end
end

With engines support in rails you can have same app folders in your plugin as well. so lets create app folder in flickr_search folder also create sub folders like controller, views, models and helpers as in any rails application. Lets create a controller file in controllers folder called flickr_controller.rb and create index action. Noew in views folder create layout folder and add layout file called flickrlayout.html.erb. Add this code to layout

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Flickr</title>
<%= javascript_include_tag :defaults %>
<%= stylesheet_link_tag 'flickr' %>
</head>
<body>
<%= yield %>
</body>
</html>

Notice that the code has some asset helpers. currently rails does not have support for seperate assets for plugin so we need to copy these style sheets and other assets to rails public directory. This can easily be done by writing few lines of code in install.rb file. but in our case we will simply create flickr.css file in public/stylesheets directory of main rails application. so lets create this file and copy these styles in it.


body {
background-color: #888;
font-family: Lucida Grande;
font-size: 11px;
margin: 25px;
}

form {
margin: 0;
margin-bottom: 10px;
background-color: rgb(222,231,236);
border: 5px solid #333;
padding: 25px;
}

fieldset {
border: none;
}

#spinner {
float: right;
margin: 10px;
}

#photos img {
border: 1px solid #000;
width: 75px;
height: 75px;
margin: 5px;
}

Lets create a view for searching flickr images. In app/views directory of you plugin create index.html.erb file. Copy this code in it


<% form_remote_tag :url => {:action => 'search'}, :update => 'photos' do %>
<fieldset>
<label for="tags">Tags:</label>

<%= text_field_tag 'tags' %>
<%= submit_tag 'Find' %>
</fieldset>

<div id="photos"></div>
<% end %>

Create a search action in flickr_controller.rb file that gets called by AJAX action in index.html.erb view. Now your controller code should look like this


class FlickrController < ApplicationController

layout "flickrlayout"

def index
end

def search
flickr = Flickr.new

if params[:tags].empty?
render :text => '<h2>Please enter a search string</h2>'
else
begin
photos = flickr.photos(:tags => params[:tags], :per_page => '24')
render :partial => 'photo', :collection => photos
rescue NoMethodError
render :text => '<h2>No matching photos found</h2>'
end
end
end
end

Also create _photo.html.erb partial in views/flickr folder. this will contain this line of code


<img class='photo' src="<%= photo.sizes[0]['source'] %>">

With engine you can also add routes with routes.rb file. so lets create config folder in plugin root folder and add routes.rb file. you can add custom route for flickr search here


ActionController::Routing::Routes.draw do |map|
map.pics '/pics', :controller => 'flickr'
end

Now go ahead and run your application the flickr search should be working on http://0.0.0.0:3000/pics

One thing about migrations there is still no way to have seperate migration in engines plugin you would still need to generate and copy migrations to main applications db/migrate folder.

Categories: Ruby Language

What you think about merger of merb and rails

January 2, 2009 1 comment

I think that development of merb should continue independently as well. what do you think?

First version of rails with threat safety is here

November 22, 2008 1 comment

Rails 2.2 was released yesterday which happens to be first version to support thread safety. Previously lack of threads in rails has been cause of scalabilty concerns especially with twitter problems coming to limelight. So new version of rails will solve these issue? “Not so fast!” says most of people in ruby community.

One of the main reasons why not all new rails application will become multi threaded overnight is native libraries (i.e mysql client library etc). All most all rails applications use these libraries and these libraries block ruby intrpreter from calling these libraries with multiple threads. One answer to this problem is Mysql Plus library which uses never block api.

Another issue with rails multi threading is that it is being added as after thought and has not been extensively tested. if you really has to develop multi threaded ruby web application i think you should go with merb. The merb 1.0 was recently released as well. There are some open source multi threaded applications already out there.

Another issue threads support in Ruby VM. Current Ruby 1.8 VM does not support native threads but next version of Ruby 1.9 VM will support native threads. JRuby VM currently has much more support for threading and it is expected that both Merb and future version of Rails will have great performance and scalibility on JRuby VM. Upcoming Java 7 Runtime will have additional support for hosted dynamic languages like JRuby and Jython which will address many concerns about performance of JVM hosted languages like Groovy, Scala and JRuby.

Categories: Ruby Language, Ruby on rails Tags: , ,

Simplicant Goes into Public Beta

April 21, 2008 Leave a comment

The public beta of Simplicant was officially released few days ago. As a follow up to my previous post . Here is some details according to official press release.

Simplicant allows SMEs to move towards a more organized, cleaner job applications management system. simplicant is the first web 2.0 applicant tracking system and brings the spirit of web 2.0 into hiring. It is completely web-based (Software as a Service) and makes use of powerful web 2.0 concepts like tags, rankings, search and collaboration to make the hiring experience easier, smoother and quicker.

Here is link to official press release:


Simplicant Goes into Public Beta 

Tags: , ,

BitNami JRubyStack makes JRuby on Rails setup simple

March 18, 2008 Leave a comment

BitNami JRubyStack makes JRuby on Rails setup simple. The stack comes bundled with JRuby, Rails, Tomcat, Glassfish Gem, Mysql and Subversion. You can run your Rails app either with Glassfish or make a war and deloy to tomcat. The whole package is distributed under Apache 2.0 License.

There has been few sites that use JRuby proving that JRuby does scale. Thoughtworks project management application mingle is powered by JRuby and Rails. Sun mediacast site is also now running on JRuby and Rails

Tags: ,

Categories: Ruby on rails

Matz talks about Ruby 1.9 (Yarv)

March 18, 2008 Leave a comment

Matz talks about new Ruby version. Ruby 1.9 beta was released on christmas day at 25 december 2007.
Matz presents about new ruby version at google conference. The new version was earlier known as Yet Another Ruby Virtual machine (YARV). The main feature of Ruby 1.9 is support for platform threads. The Tread class in earlier Ruby VM’s were green threads.

Categories: Ruby Language Tags: , ,

Simplicant Public Beta will be launched soon

September 8, 2007 2 comments

It has been almost an year since my first Ruby on Rails project was launced as private beta. The Simplicant is simplest web based applicant tracking system. Based on years of experience in hiring of medium and small companies, simplicant gives you headstart when you want hire applicants in your comapnies. This Applicant tracking system not only allows you to post jobs on job board and recieve resumes, but also allows you to ask questions before you interview the applicant. After interview simplicant allows you to communicate with applicants

the Simplest applicant tracking system on web 

Simplicant has been totally overhauled  for performance and scalability. The web based interface has features that are found in todays Web 2.0 applications.

The company behind Simplicant is confiz solutions which is known for developing many of todays major Web 2.0 apps. The new public beta for will be launched soon.

Follow

Get every new post delivered to your Inbox.