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.

Wednesday, May 21, 2008

Behaviour-Driven Development - Have a definite edge in the outsourced software development

Behaviour-driven development is an “outside-in” methodology. It starts at the outside by identifying business outcomes, and then drills down into the feature set that will achieve those outcomes. Each feature is captured as a “story”, which defines the scope of the feature along with its acceptance criteria” – DAN NORTH

Behaviour-driven development is the test-first approach agile development which focus on design, documentation and behaviour. "Behaviour" makes more sense than "Test" since developers code for the behaviour rather test.

Challenges in outsourced development


Communication is the major challenge in the outsourced software development.

Developers often complaint that they are not getting the detail requirements and design, and they are doing the requirement analysis and design analysis though it is done already. They back-up their delay in delivery with this reason.

Is it true?

Let's assume the project is extended team model where the requirement analysis and design is done at customer end, and developer needs to follow the design and do the implementation. The customer needs to send the requirements and design in detail. If that doesn't happen, then the developer needs to analyse the requirements and design though it is done already if he needs to deliver what the customer wants.

Let's discuss some other challenges. Customer often complaint that the code is not met the expected behaviours (requirements). This might be because the developer not understood the expected behaviours or it is not communicated to him properly. There should be proper communication between the co-ordinator and the developer. Our experience proves that all the stake holders should be aligned on the requirements and there should be a tool to support that.

Customer expects the working output in short iterations, and the world is moving towards the agile development. They provide the requirements in call/email, and the developers needs to deliver it in short iteration. The project manager needs to pick up the best agile method to deliver the output in short iteration

One common challenge in the software development is delivering the quality output and adding the value to the customer. How can we make sure the code quality is good? One way is doing the code review as per coding standards. The other way is test-first approach. It is imperative to do the test first approach to meet the customer expectations. The TDD and BDD facilitates the test-first approach.

Behaviour-Driven Development Experience

We started exploring the BDD before 5 months and following the practice in one of our Ruby-On-Rails project. We picked up the BDD for the following reasons:

1) We are working on the extended team model. The customer do the requirement analysis and most of the design. Our people needs to do the implementation. Our people do the requirement analysis and the design analysis though it is already done, and create the documentation for the CMMI process. We wanted to come out of the repetition and improve the productivity of the developers. We initially expected the detail requirement document from the customer, but the customer didn't find time to create the detail word document. BDD helps us to communicate the requirements through text stories, and this is also serves as documentation for the CMMI process.

2) We wanted to make sure our code is accepted by the customer by acceptence testing

3) Agile Development

We tried out TDD initially, but that didn't work out. It facilitated the unit testing and acceptence testing but we communicated the requirements and design through basecamp.

Then we tried out BDD text stories. The customer is expected to send the plain text stories with all the scenarios. The developer's work is nailed down here. He just code for the scenarios. We pick up one feature per iteration (Each feature is a RSPEC story). Normaly we deliver in very short iterations (2-3 days). We do SVN check-in the code and the spec. Customer run the spec for acceptence testing

We use RSPEC for BDD development since our technology is Ruby On Rails. There are some other BDD frameworks available for Java, PHP and .NET

Behaviour-Driven Development Lifecyle