Add Google Custom Search to your Rails Application (Business Edition)

Posted by anup.narkhede on July 17, 2008

Here are the steps to set up a Google CSE for your rails application. In order to use the xml api, first of all you need to have a business account. You can get the xml query format url or more precisely the cx parameter (unique ID of the CSE) from the control panel.

Setup:

Identify your resource to be searched with the help of url patterns. For ex, if the application is RESTful, it is easy to index something like http://someshop.com/products. Configure your CSE for the above url pattern so that http://someshop.com/products/1, http://someshop.com/products/2.. etc get indexed by the search engine.

Code:

The GoogleSearch class sends a GET request to fetch the results xml, which is parsed using the Hpricot parser.

Output:

Now you could get the urls of search results as an array. Likewise, you can fetch other parameters from resultant xml by referring the this doc..

Remote file storage using File Column plugin

Posted by anup.narkhede on February 10, 2008

Many times I came across a need to fetch remote data (video files/images) and store them on file system. Searching for a way, I came up with this approach.

The example is about a Video class with a video_file ‘file_column’. To fetch and store a remote file instead of getting it as a post parameter, we need to do some code workaround. The example below demonstrates one approach.

Usage:

My model looks as simple as this:

JS based easy Field Validations in Rails

Posted by anup.narkhede on January 06, 2008

Most of you will disagree with this post to use Javascript based form validations in Rails. However this is an attempt to simplify integration of DEXAGOGO - Really easy field validations in Rails. This is one of the most structured and easy validation technique written by Andrew Tetlaw and team. (Demo)

To use it in Rails, copy simple_validation.rb in application lib directory and add following lines to config/environment.rb file.

Download and Import validation.js along with prototype.js in layout from here.

Two methods are introduced to render forms.

  • validated_form_for
  • validated_remote_form_for

Usage:

:class attribute refers to validation rule (or combination of rules) and :title attribute is the validation message. Details of all rules are available at http://tetlaw.id.au/view/javascript/really-easy-field-validation

File Upload: Validating Extensions

Posted by anup.narkhede on January 05, 2008

I was searching for a way to validate a file against an allowed set of extensions before uploading. My application uses file_column plugin to store video files and I am using mongrel_upload_progress to track the upload. The maximum file size allowed is 50Mb so there was a need to have validation at both client as well as server side.

Model Validation

Client Side javascript validation

mongrel_upload_progress plugin uses following code to submit the form in a hidden iframe. Modify submitUploadForm method as shown:

The advantages of this approach are:
File is validated at both sides to avoid unnecessary uploading of file before model validation. Good for big files.
In case JS is disabled, the file is still validated.

Ajax Tree Plugin

Posted by anup.narkhede on December 19, 2007

Introduction

This is a helper plugin to generate ajax based tree structure (two level). It can be used for two models having n:m or 1:n relationships. A leaf from any branch can be dragged and dropped in another branch. A scaffolding arrangement is also done to create a new branch, add leaf etc.

Sceenshot

For example:

Installation:

Here we take the previous example of Category and Article models. Generate the ajax controller for the two models.

Usage

Example

Copy static files

Copy add.png, delete.png from /vendor/plugins/ajax_tree/ directory to /public/images directory.

Copy ajax_tree.css file from /vendor/plugins/ajax_tree/ directory to /public/stylesheets/ directory.
Include css in your application layout

Call the helpers

Specify model names for branch model and leaf model in /views/home/_show_ajax_tree.rhtml partial.

Then, call this partial in any rhtml, where you want to display the ajax tree. In this case, we have done it in /views/home/index.rhtml

home_controller.rb (Generated automatically)

index.rhtml

Contributions:

This project is hosted at http://code.google.com/p/ajax-tree/. Please leave your feedback.