Monday 1 June 2009

Help? ActiveResource common config

So we have a system that communicates with a "local" API - ie all the objects in our system are on another database, which we access via an API[1]. A very large number of our objects are accessed using HyperactiveResource[2].

Because so many of our resources are hosted on the same system - it makes sense to put the common access-information (eg "site", "user" and "password") into a commonly-accessible spot.

So I tried just plonking something like:

module HyperactiveResource
  self.site = API_SITE_ADDRESS
  self.user = API_SITE_USER
  self.password= API+SITE_PASS
end

into config/environment.rb... which sort of worked... but seemed to cause some clashes with the actual HyRes objects later - because they aren't loaded until *after* config.environment.rb

Maybe I'm just being thick, but I wasn't able to figure out how to get this stuff to be pulled into every HyRes object without it happening before I've really defined HyRes... and it would then get really upset when I tried to "redefine" HyRes when the same was loaded afterwards.

My quick fix was to define these using the following instead:

module ActiveResource
  class Base
    self.site = API_SITE_ADDRESS
    self.user = API_SITE_USER
    self.password= API+SITE_PASS
  end
end

Which means we don't get a HyRes conflict, but I'm sure there must be a better way... any suggestions would be welcome!

Notes:
[1] There's various business-reasons for why this is. Mainly - because the existing codebase is very large, and we only want to migrate to Rails one chunk at a time. This allows us to keep the existing systems running on the existing code-base, but add new functionality (or convert existing chunks) without disturbing the operation of that section.
[2] My extension to ActiveResource that actually works more like ActiveRecord (unlike vanilla ARes)

No comments: