Mass Image Processing
I ran into a requirement to create thumbs of all images in my content folder. This shell script can be used to create thumbs of all images or for mass conversion of file formats. The script scans for images with the specified format in all subdirectories and puts the processed file in same path.
The constants can be modified to define the source directory, resolution, source and target filetypes. You need to have ImageMagick installed and configured to get this script working.
The script will create thumbs for each image file in same directory with ‘.thumb’ before the extension.
For ex: my_cool_pic.jpg will have a thumbnail named as my_cool_pic.thumb.png.
Add Google Custom Search to your Rails Application (Business Edition)
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..
Batch Mailing in Rails
Recently I worked on a Mass Mailing system for a Rails application using ar_mailer. This tutorial is just an effort to consolidate tips needed to get the mailing system ready and save time on some commonly known problems.
I wanted to implement this for a newsletter application, where a mail needs to be sent to hundreds of recipients for delivery. One of the good choice is to store these mails in a database queue and allow a background process ‘ar_sendmail’ to deliver them one by one.
1. Install ARMailer
Follow this tutorial : http://blog.segment7.net/articles/2006/08/15/ar_mailer.
One of the commonly faced error:
Solution:
Verify that Email model, migration and emails table are created and configured properly.
2. Configuration
Add following lines in environment.rb file
3. Create Mailers
I wanted to have two type of Mailers, one for immediate delivery (alert messages, signup notifications etc) and other for batch delivery (newsletters for mass mailing, which are scheduled to deliver on a particular time). QuickMailer class is an Actionmailer for instant delivery using smtp, and BatchMailer uses ar_mailer to queue the mails in database for delivery using ar_sendmail process.
Note the statement ActionMailer::Base.delivery_method = :smtp at the top, which forces UserMailer to send mail using smtp directly instead of calling ARMailer’s perform_delivery_activerecord(mail).
4. Using Gmail SMTP server
I have used local smtp server to send mails, to avoid authentication for each call and save time. However here are the steps to configure Gmail smtp server.
http://ruby.dzone.com/news/rails-gmail-simple-email-deliv
Install plugin action_mailer_tls from https://openrain.com/opensource/public/rails/plugins/action_mailer_tls
Configure your environment.rb
5. Test
Remote file storage using File Column plugin
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
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


