Rails 3 and Gem Plugins

Will there be a way for rails 3 to load plugins from gems that have
been installed via the bundler?

Could you elaborate? An example on what gem you’re trying to use would be helpful!


I am working on a blog post now, but the basic idea is that ActionController, ActiveRecord, etc. are all “plugins” now. So anything that they can do, you can do too.

We just need to document how it works (if you’re interested, check out the railtie.rb files in the various components).

Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325

You can also have a look at http://github.com/dkubb/rails3_datamapper
for an example of a gem/plugin (whatever the difference is) that works
with rails3


Sure thing. I'm trying to get Jammit working as a gem on edge. Jammit
is an asset compressor and packager that I use on a side project. I
forked it at http://github.com/samgranieri/jammit/tree/rails3 and
proceeded to convert it to a rails 3 plugin. Jammit has a controller
for packaging the stylesheets/javascripts, a helper for displaying the
assets, and a route to display the assets.

If you install jammit as a plugin (via ./script/plugin install
git://github.com/samgranieri/jammit.git -r rails3) and type rake
routes, then the plugin loader will pick up the asset routes.

If you install jammit via the bundler, then type rake routes, then the
asset route wont show up. I tried installing it as a gem using a
locally checked out repo switched to the rails 3 branch.

Here's my gemfile lines for Jammit:

  directory "/Users/sam/Documents/Development/ruby/gems/jammit"
  gem 'jammit'

I hope this helps.

-- Sam

During the boot process, their respective “railtie.rb” scripts are required. Should we also write and later require “railtie.rb” from our plugins in application.rb, or will this be automatized (“railtie.rb” will have some magic behavior)?

This is one of the things we’re working out.

The easiest thing to do is to put the Railtie subclass in lib/my_lib.rb itself. The bundler will require the file, similar to how config.gem worked in Rails 2.3.

Another option would be to move it into lib/my_lib/railtie.rb (or whatever) and require it from my_lib.rb. You can then do require “my_lib/railtie” if defined?(Rails), since the Rails constant will always be required before your gems (check out boot.rb if you want to see how).

You could also ask your users to require my_lib/rails or :require_as => [“my_lib”, “my_lib/rails”] in bundler.

We’re looking at some other, long-term options involving Rubygems metadata to further automate that process (perhaps you’d include some metadata in Rubygems registering your gem as a “plugin for rails >= 3.0 with lib/my_lib/railtie.rb” which would tell bundler to require that file, but only if Rails was around.

Bottom line: there’s a bunch of options right now, and the community should really converge on a few really good ones. I personally like putting the railtie in lib/my_lib.rb for gems that are only used as Rails plugins.

Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325

So, the bottom line is that we (users, plugin authors) are responsible that the Railtie subclass somehow gets executed, either automatically in plugin code or explicitly with Bundler :require_as.

That’s what I wanted to know. I guess this is all we need, since with Railtie we can hook into initializer and fine-tune what our plugin does at any specific stage of application loading.

I suggest you write a post on how Railtie subclasses look like, what they can do and what are the options of hooking into the initializer other than simply appending new initialization steps to the list.

Yes, a post on this would be very useful.

Is this actually the case at the moment? As far as I can see, there's no Bundler.require_env call anywhere in the Rails initialization. Should there be one somewhere to ensure that bundled gems are required?

The only way I have been able to load my gem plugin is using an explicit require at the bottom of boot.rb.

-Sam Pohlenz

You can also look at the latest docs on railtie.rb that I just added at:


Which is commit #087b67805e3785 on lifo/docrails

It will be merged back into rails-master soon.

I am currently doing docs for all the railtie methods etc, so anything that is not clear, please let me know.


Having just re-read that again now, it needs much more work, for example it omits to mention that you need to require the railtie file and doesn’t mention the other method that Yehuda went over.

But it is a start :slight_smile:

I’ll fix it up tomorrow.