Wednesday, July 16, 2008

RAILS 2.1 - :through

Active Record has an interesting feature :through. It helps to create the association through the association. For example:

class Court < ActiveRecord::Base
has_many :court_users
has_many users :through=> :court_users
end

class CourtUser < ActiveRecord::Base
belongs_to :court
belongs_to :user
end

In the above example court has_many users through the court_users since the association between court and the user is exists only in the court_users table.

Before 2.1, only has_many method has :through. Now, has_one method has the option :through

Monday, July 14, 2008

Dynamic Attributes

Did you ever faced the situation where you need to store the object in the table column? Did you ever faced the situation where you need to add the attribute dynamically to the table?

Rails allows us to store the object in the table column using 'serialize'. This is somewhat tricky to explain the use of storing the object in the database. You can easily understand when you face the scenario. Let's think out a simple example:

create_table :users do t
t.string :login
t.string :email
t.string :address
end

Note that address is a string field.

class user < ActiveRecord::Base
serialize :address
end

We can save the address as follows:

address = Addres.new(params[:address])
user = User.first
user.address = address
user.save

You can also store hash in the table column like below:

user = User.first
user.address[:street]= "Mettu Street"
user.address[:zip]= 23456
user.save

Use this concept when we want to add attribute to the table dynamically

Thursday, July 10, 2008

Rails composed_of

Initially, I had the difficulty in understanding the composed_of.
It is very simple. It is used to associate the value object to the class (Newbie's, please note that the object is value object, not the active record object). Let's say an example we used in our project.

We have the FilterField class and SortField class. If you notice the common thing in both the classes you will come to know both are field classes. Right?

Why don't we have the seperate class for Field and associate it to FilterField class and SortField class. This will help us to group the field related attributes in Field class, sorting related attributes in SortField class and filter related attributes in FilterField class.

class Field
attr_reader :class_name
attr_reader :attribute_name
def initialize (class_name, attribute_name)
@class_name = class_name
@attribute_name = attribute_name
end
end


class SortField < ActiveRecord::Base
composed_of :query_field, :class_name=>'Field',
:mapping=>[%w(class_name class_name), %w(attribute_name attribute_name)]
end

class FilterField< ActiveRecord::Base
composed_of :query_field, :class_name=>'Field',
:mapping=>[%w(class_name class_name), %w(attribute_name attribute_name)]
end

SortField object can be created like below:

field => Field.new(:class_name=>'Court', :attribute_name=> 'Court_Name')
SortField.new(:query_field => field, :sorting_order => 'Ascending')

Wednesday, July 2, 2008

Integrating the scanner device to ROR application

We got the requirement to scan and upload the image to the web application.
We have analyzed all the commercial twain solutions like DynamicWebTwain, csxImage,TwainControlX,TwainConnect, jTwain, JSane and others. Many
of the commercial solutions are very costly. Finally, we picked up the DynamicWebTwain for the following reasons:

1) DynamSoft is a Microsoft Gold Certified Partner
2) Stable software
3) The customers of DynamicWebTwain include IBM, HP and LockHeed Martin
4) Compatible with IE, FireFox and Mozilla
5) Good Technical Support
6) Cheaper one
7) Supports basic image editing features including Rotate, Crop, Miror, Flip and Change Image size

We used Ruby on Rails for our web application. Since the DynamicWebTwain is the ActiveX objects, we simply embeded the ActiveX object using html object tag in the rhtml page. We called the object methods through javascript and uploaded the image to the URL. Here is the javascript code used by us to upload the image.

var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
strActionPage = CurrentPath + "court_images/save_scanned_image?court_id="+court_id;
frmScan.DynamicWebTwain1.HTTPPort = 3000;
frmScan.DynamicWebTwain1.HTTPUploadThroughPost("aspire76", 0, strActionPage, "imageData.pdf");

To the ROR developers, we used attachment_fu plugin to save the image in the file system. Here is the code for save_scanned_image action (See the above URL)

params.sanitize_keys!
court_image = CourtImage.new(:uploaded_data => params[:RemoteFile],:court_id => params[:court_id] )
respond_to do format
if court_image.save
format.html{render :text=>"#{court_image.id}"}

court_image is the active_recod which has_one attachment

has_attachment :storage => :file_system,
:size => 0.kilobytes..1.megabytes,
:resize_to => '320x200>',
:thumbnails => { :thumb => '100x100>' }

Plz note the below code where we are rendering back the court image id through response.
We can receive the response in javascript where we called the URL to upload the image (see above). Here is the javascript code to get the response:

var court_image_id = frmScan.DynamicWebTwain1.HTTPPostResponseString;

Hope, this will help the ROR and other web application developers to integrate the scanner device to the web application. The scanner device should be connected to the client (browser) system.