Reprocessed, by Matt Patterson

Something approaching a weblog

Getting Active Resource working in Edge Rails

I've wanted to have a look at Active Resource since it was announced, and now I need to look at it for work. It took me a while to get it working, so here is my learning.

Without the waffle

First off, the summary, to save you reading the tedious stuff... (This assumes you have rails already installed, and are on a unixy system.)

mkdir -p some_name/vendor; cd some_name
svn co http://dev.rubyonrails.org/svn/rails/trunk vendor/rails
rails .

Then, add this line to your config/environment.rb file, somewhere inside the Rails::Initializer.run do |config| block:

config.load_paths += %W( #{RAILS_ROOT}/vendor/rails/activeresource/lib )

If you're using Locomotive 2 on a Mac.

Create a new project in Locomotive, or select an existing one, start a terminal session and then navigate to somewhere else and follow the commands above (since you just need Locomotive to monkey with $PATH for you), or you can take an existing or just created project, do the svn checkout, then follow the rails upgrade instructions, essentially rails . and overwrite the two or three files it tells you to. But, as always, buyer beware.

Edge Rails

I'm kind of dumb, so it took me a while to work even this out...

First, I thought I had to do rake freeze_edge, but actually you don't... When I did that, it pulled the latest revision of Rails down, but omitted the Active Resource directory... Just invoking

svn co http://dev.rubyonrails.org/svn/rails/trunk vendor/rails

from my app root worked fine. But, when I fired up script/console, it was definitely on Edge Rails because it was pulling libs from vendor/rails, but Active Resource was not found. Boo.

Active Resource

After far too long spent mucking around with variations on the svn co theme, and issuing rails . after the checkout. Nothing. Then, in config/environment.rb I saw this:

# Skip frameworks you're not going to use
# config.frameworks -= [ :action_web_service, :action_mailer ]

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )

Hmmm, I thought. I also spoke to my boss, who'd ended up pulling the activeresource dir into vendor/plugins. Not wanting to give up just yet, I tried uncommenting the config.load_paths line and adding #{RAILS_ROOT}/vendor/rails/activeresource to the call. That didn't work, then I tried to see if Active Resource would get picked up, but no... then I tried this line:

config.frameworks += [ :active_resource ]

That blew up, and i traced the most interesting looking lines in the traceback and eventually found this, in railties/lib/initializer.rb I saw this (some lines edited out for clarity).

 def default_load_paths
   ...
   # TODO: Don't include dirs for frameworks that are not used
   paths.concat %w(
     railties
     railties/lib
     actionpack/lib
     activesupport/lib
     activerecord/lib
     actionmailer/lib
     actionwebservice/lib
   ).map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }

Aha, I thought.

I added this line to config/environment.rb, and hey presto, it worked:

config.load_paths += %W( #{RAILS_ROOT}/vendor/rails/activeresource/lib )

Ta, and indeed, Da.

Not forgetting:

This page is: