Bubble Validations

We were developing a Content Management System which is composed of different components, with each component having dependency on other components in the hierarchy.

Our CMS has the following structure :

  • Site has_many pages
  • Page has_many sections
  • Section has_many embedded_modules
  • EmbeddedModule has_many elements

Problem Statement

For any component to be publishable, we need to ensure that not only a particular component, but also its descendants(children) should satisfy the criteria of publishability.
In our case a Page could be publishable, if all sections belonging to it were publishable. A section would be publishable if all embedded modules belonging to it were publishable. A EmbeddedModule would be publishable if all elements belonging to it were publishable.

If at any step of the hierarchy the criteria fails, not only that component should be marked as unpublishable but also upchain should be notified of unpublishablity.
If any EmbeddedModule gets unpublished then it must unpublish the upper hierarchy i.e its associated section, page and site.

Our Solution

To achieve this we identified that a shared module would be needed which can handle publishability of a component and should also be able to notify the publishability change to its parent.
Continue reading

 

Installing gem on Leopard

May be you guys have noticed that when you try to install a new gem on your machine it says some thing like “Updating meta data for 500 gems” and display one dot per gem. These dots moves very slowly and stops in some time.

I faced this situation many times and found a solution some where on net. I don’t remember the link but it worked.

According to that, you just need to add:
require ‘resolv-replace’
as the first require in /usr/bin/gem file.

enjoy!!

 

New in Ruby 1.9: Threads

The ruby is getting changed and improved a lot with every release. No doubt the existing demerits of rails are soon going to be history.

Currently for my rails applications I am running Ruby1.8… and exploring 1.9 to get ready as we are heading with nice speed in the rails developments. Modrails has already started making waves all around.
Soon I would be publishing a couple of posts on threads, processes and sockets in ruby… here is a bit on how they are going to be different in Ruby1.9.

In Ruby1.9 the threads implementation is going to be really different than what we all know/do in Ruby1.8. The Ruby1.8 uses a single native thread and runs all Ruby threads within that… which certainly protects the Ruby threads to take on much responsibilities and to completely leverage the hardware with multiple processors.

Now, the good news, the Ruby1.9 allocates a separate native thread for every Ruby thread. But at this very moment it is not fully functional… as because of some C libraries, used here, are not thread-safe that it will not allow Ruby1.9 to take control on multiple native threads. Still, great signs of improvements in these directions… It has been mentioned that this restriction will be removed in the upcoming versions of Ruby1.9 and there will be full fledged implementation of multithreading in Ruby with multiple native threads, and the charm of parallel execution… means a lot more scope of improvements for all related libraries, frameworks built on the top of Ruby.

 

Ride the Rails: Still skeptical?

Ok, so we had been shouting ourselves hoarse, claiming that Rails is all about developer productivity and joy. So is that all about it?, huh!, was the normal reaction. But isn’t that a big enough reason. Not for many people though.

Yes, we accept that there are some pain points, like hosting Rails applications at shared hosts. No we don’t need those in production, but don’t you wish it was easier to deploy a rails app for a quick review with a client (a client who can’t run it on his own machine). Yes php scores there, just throw the code on the server and you are done. Why do I still run this blog on wordpress and not typo or mephisto? The big reason is that it’s easy to let just apache handle everything.

But things might change soon with the launch of passenger aka modrails.

And the other classic allegation against Rails has been performance. Remember the discussion between JDD and DHH about CPU cycles vs. developer cycles. We are definitely headed in the direction of lesser CPU cycles for our Rails app. Rails2 made some advances towards that and with Ruby1.9 and YARV and Rubinium, we have high expectations. Also you have heard about Ruby Enterprise Edition , haven’t you?

So things might change, when people try to figure out the fastest web language or framwework , the next time around.

I and those around me here, are generally biased towards rails. Ruby makes us happy. For us the pleasure points in Rails were always far more than the pain points. The basic Rails principles of DRY and Convention Over Configuration clicked with us. Ruby’s and Rail’s simplicity and beauty clicked with us. We did not need hosting on shared hosts. We could work with Rails caching to improve performance. No wonder we were one of the early adopters of Rails in India. But today, I would like to thank the critics whose untiring rants have moved Rails in the direction of being much more than what is was a couple of years ago.

Some of those changes have been in rails, but more have been around it. If you would have noticed, most of these development are not in rails as such, but in the ruby ecosystem.

Rails provides developer productivity and joy; ease of deployment; and ever-improving performance. And no, now you don’t need to go back to Java. We knew it, I am just repeating it for you.

Extrapolate this one year old graph for yourself.

So what is your reason for not having rail-ed yet?

Update: Charles Nutter has a post on upcoming Ruby implementations here

 

Rails Plugin Annotate Models For Spec And Spec Fixtures

I have been using “Annotate Models” rails plugin written by Dave Thomas since I found it around one and half year ago. It really helps while writing fixtures. But if you use RSpec you might miss schema info at the top of your rspec fixture file as I do. So, today I modify the plugin file so that it prepends schema info to spec file and fixture file. Below are the links of patch file:

svn patch to add schema info to spec file and spec fixture file
git patch to add schema info to spec file and spec fixture file

 

New in Ruby 1.9: Block Scoping

Ruby 1.9 provides the block scoping of the local variables. It shadows the outside local variable in the block and allocates a new object_id in the memory to the block variable, which essentially means much more flexibility in using the lambdas without worrying about the name of the block variable. Remember that instance, class and global variables are still the same and can be used outside and inside the block as usual.

ruby <= 1.8

ruby 1.9 (shadows the local variable a)

 

Ruby interface for OpenSSL

If you have installed ruby using apt-get or if you have not mentioned the configuration option for openssl while installing from source, you can expect openssl will not work with ruby.

Here is the solution for the same…

Step 1

Install gcc and make(if you don’t have already on your box)

Step 2

Install the system level OpenSSL and dev libraries

Step 3

Install the ruby’s openssl interface to the system’s openssl

switch the directory to the ruby’s source code’s openssl

install the ruby’s openssl interface

try requiring the openssl in irb

if returns true, installed successfully!

 

Migration: Adding/Removing columns are now much easier

You may have noticed by now, that in Rails 2.0 changeset 7422, you can specify columns you want to add/remove in your migration by passing attribute:type pairs to the migration generator.

For example, lets assume that we need to add a column ‘role’ in users table(User model). In this case generate a migration like:

Output:

Here AddRoleToUser plays the main role. ‘Add’ specifies the we want to add column(s) and ‘User’ separated by ‘To’ specifies the table.

Similarly, if we need to remove a column ‘role’ :

Output:

Here RemoveRoleFromUser plays the main role. ‘Remove’ specifies the we want to remove column(s) and ‘User’ separated by ‘From’ specifies the table.

Isn’t it cool?

 

Ruby Script for SVN commit notification with log message, list of updated files and readable colored SVN Diff

Some days ago I wrote a post about “SVN commit notification” which uses a perl script for sending commit notification with svn diff by mail. In this mail you can find svn diff from the last committed revision. I used to love this mail, soon I realized that it is a bit ugly and difficult to read. Also there were some important information missing. Like the name of user committing the code, the log message etc…

And then I started writing my own ruby script for same purpose but with some addition and modification. Commit notification script is that script, you can download and configure it with your SVN post commit hook as follows.

Add following line at the bottom of your post-commit file:

* Please remember to change the path of you commit-email ruby script.

Now open commit-email ruby file and modify the following section according to your requirement:

You are done with that, now onwards whenever someone commits the code, you’ll get the commit notification mail like:

Commit Email Preview

 

Defining function inside a function using def

The function(s) defined within a function works fine with any hierarchy only within the main object but gives NoMethodError inside a class.

Lets exemplify it!

Here are the example of both main object and inside a class…

Its an unexpected behavior in the main object. I don’t know whether its a bug or the feature. But as Matz says it will be removed in the future version of ruby, might be in 2.0