2009
10.26

Ruby allows instance and class methods to be added to any class through modules.
For example:

A good Example:


has_many :users

has_many adds a lot of new methods to the activerecord objects, but only when we add the ‘has_many’ line in the model.

Some popular examples: acts_as_list acts_as_taggable_on_steroids etc

Here is a Standard recipe for a Mixin like those mentioned above:

2009
10.25

While configuring Mongrel to work with SSL I ran into this issue.

[error] ap_proxy_connect_backend disabling worker for (127.0.0.1)

[error] (111)Connection refused: proxy: HTTP: attempt to connect to 127
.0.0.1:8008 (127.0.0.1) failed

The issue was me not adding the following lines into the SSL virtual host config

RequestHeader set X_FORWARDED_PROTO ‘https’
SSLProxyEngine on

2009
10.24
  • Make a directory /etc/mongrel_cluster
  • Copy the mongrel_cluster.yml file from your <project-dir>/config directory into /etc/mongrel_cluster directory
  • if you are going to install multiple applications on the same machine, rename the .yml into <project>.yml in the /etc/mongrel_cluster directory
  • Copy the mongrel_cluster_ctl script from your ruby/bin directory into /etc/init.d/ directory
  • Go to the /etc/init.d/ directory and rename the mongrel_cluster_ctl script to mongrel_cluster
  • Add the following line in the /etc/init.d/mongrel_cluster file in the top comment section
  • # chkconfig: – 85 15
    # description: Mongrel cluster runs the ruby on rails servers
  • Run the chkconfig command as follows “/sbin/chkconfig mongrel_cluster on”
  • Just to verify run the script /etc/init.d/mongrel_cluster stop and /etc/init.d/mongrel_cluster start
2009
10.23

Ruby: Intersection of 2 arrays (a & b)

By satya

Yeah, just a continuation to Array union.

[1,2,3] & [2] # Returns [2]

As simple as it.

I love RUBY.

2009
10.23

How to check if number is in a range

By satya

The following code returns if a number lies with-in a specified range.

It uses === operator.

Syntax: (Range) === (number)

Eg: (9..15) === 12 # Returns True

(9..15) === 21 # Returns False

All, the === operator does is RANGE_MIN <= NUMBER <= RANGE_MAX

Note: This is not any substitute for include?. Include iterates on every item and does  the == operator on it.

2009
10.10

Gravatar

By satya

This is a slightly sophisticated version of another-blog-post-of-mine.

GravatarGlobally Recognized Avatar.

The concept of Gravatar is to save your email & profile-pic combination at one website (Gravatar) and let all other websites use the same. This way, your profile-info is kinda centralized (of-course, only the profile-pic, not the entire profile).

Consider, I register at Gravatar with email as ola@bala.com and add a profile pic to this account.

Now, any of the websites where i have registered with the same email and posted any content (forum,comments,answers & etc…) can display my profile pic along-sides the content by fetching the pic from Gravatar by specifying my email.

Advantages :

  1. The other  websites don’t have to allocate  space for saving my image. Its GRAVATAR’s headache.
  2. From the developers stand-point, its quite easy. As simple as counting 123., trust me :D .

Implementation:

Based on the email of the user whose Gravatar-pic we need, we have to formulate a special URL, which will be the source of Gravatar pic.

The following are the 3-steps to formulate the URL:

  1. The URL should start with “http://www.gravatar.com/avatar/” or “https://secure.gravatar.com/avatar/” (obvious)
  2. Append the MD5 encoded string of the LOWER_CASED email address at the end of URL you chose above.
  3. Voila, that’s it. You have the URL ready. (Well, celebration does take time, so i added it as a task)

Eg: the final url for the email “satya dot techsavy at gmail dot com” is https://secure.gravatar.com/avatar/7379d033523d1963a64f38fe88fa97e1

And here is the pic:

Satyas Gravatar pic

Satya's Gravatar pic

Now, there are host of options available to fetch the image:

  1. Height
  2. Width
  3. Default-pic, if the specified email is not registered with Gravatar
  4. Restrict the pic based on its rating (G,X,…..)
  5. And lots more….

Extra links:

  1. Elaborated implementation details.
2009
10.10

Permalink_fu uses one/many of the object attributes to generate the permalink.
If one of those attributes (say title) have non-printable characters, the plugin throws an error.

The following patch to the code is required to fix the above-mentioned error.
Replace:

With:

2009
10.10

Using Permalinks in ROR with permalink_fu plugin is pretty straight forward.

In my current project the permalinks don’t have to be :

  1. foo-bar
  2. foo-bar-1
  3. foo-bar-2

They can be:

  1. foo-bar
  2. foo-bar
  3. foo-bar

Its OK for multiple objects to have same permalinks.

Reason: We prefix the object-id to the permalink in the to_param method of the class.

This way, the url generated for each object will anyways be distinct. Say:

  1. /books/1-foo-bar (assuming object-id = 1)
  2. /books/2-foo-bar(assuming object-id = 2)
  3. /books/3-foo-bar(assuming object-id = 3)

As the object_id is responsible for making each URL distinct, we don’t need the permalink also to have a post-fix to make it distinct for each object.

But adding the post-fix to the permalink is the default behavior of the permalink_fu.

Below is a patch to permalink_fu which makes this default behavior mutable. (The following methods are to be replaced/added in RAILS_ROOT/vendor/plugins/permalink_fu/lib/permalink_fu.rb)

Replace the following Methods:

Add Private Method:

Now, after changing the permalink_fu.rb, we have to specify in the model’s has_permalink association the :unique option. If true, it will generate a distinct permalink for each object (bypassing the changes to permalink_fu.rb). Else, will generate a permalink irrespective of the other permalinks. Eg:

The whole patched file is available at : satya – Github

Note: Even after this tweak, the default behavior of permalink_fu remains the same i.e observing the unique constraint. We explicitly have to mention (:unique=>false) in the model for this tweak to work. This way, an existing models using this plugin would not be effected.

2009
10.09

Auto linking is a nifty technique for SEO, in which we search for keywords in user generated content and link them to an information page dedicated to them ( Wikipedia is the best example for this. )

While doing this, we need to careful as we don’t want to remove/overrwrite links submitted by users.

Here is a code snippet that helps us filter out unnecessary HTML and use plain text for processing:

2009
10.06

Regex: How to identify HTML tags

By Swanand

<\/?[^>]*>