Rails Plugins: Will somebody PLEASE think of the children?

Posted by trevor Wed, 26 Oct 2005 16:33:00 GMT

First of all, if plugins are new to you then you should definitely look at Jamis Buck’s guide to plugins before going any further.

Okay, so by now you know everything about plugins right?

So, I had one of those ‘uh-oh’ moments last night.

The Issue

I was worried that putting my acts_as_enumerated into ActiveRecord::Acts::Enumerated was a bad idea. I emailed Jamis and asked if plugin authors should avoid any rails module namespaces and he said:

That’s a really interesting question. I think I would answer “Yes”, but I’m not sure why. :)

It feels more natural to me if people follow a naming convention similar to how things are done in Javaland, where modules are namespaced by their vendor. So:

TrevorSquires::Acts::Enumerated

or something :)

He went on to point out that right now there are no guidelines or best practices.

Considering the number of plugins that have already been written it’s probably a good idea to start thinking about this now.

The Opinion

So here’s my opinion, presented in a way that I hope everyone from newbies (like me), to seasoned pros can understand. I’d appreciate feedback from others on this.

A plugin author has two namespaces to be concerned about:

  • $LOAD_PATH
  • Modules

The two namespaces kind-of overlap, but they don’t have to. I have a file called /lib/active_record/virtual_enumerations.rb that defines a ActiveRecord::VirtualEnumerations module and re-opens the ruby Module class. The name of the file doesn’t have to dictate its contents.

But the name of the file plays a role in being able to load your code in the first place – via the require keyword. If there are two plugins that have a /lib/chunky_bacon.rb file then require 'chunky_bacon' will load the first one found in $LOAD_PATH.

Now, as long as your init.rb does a require of all necessary files during initialization this won’t be an issue because during init your plugin’s /lib directory is the first one in $LOAD_PATH.

However, it may become an issue if you, or any other package that happens to require 'chunky_bacon', decide to defer require calls until a later time.

Even though for most people the $LOAD_PATH won’t be an issue, I believe plugin authors should be encouraged to use a directory structure, unique to themselves, within their /lib directory. Using someone else’s plugin for an example:


/lib/techno_weenie/acts_as_paranoid.rb

Not only does it solve potential $LOAD_PATH problems but it also serves as a reminder that you really ought to make your Module namespaces unique as well (as Jamis said above).

Comments?

Comments are disabled