sa125
sa125

Reputation: 28971

Resolving Namespace conflicts between a class and a gem

In my Rails 2.3 app, I found that the problem emanates from namespace collisions between an existing ActiveRecord model and a gem I recently added.

Specifically, I was trying to add jammit support to the app. One of jammit's dependant gems require's 'platform', which introduces a Platform module into the app's namespace. However, I already have an AR class with that name, and renaming it will introduce a lot of additional work and possible bugs.

I was wondering if there's a relatively simple way (using ruby's metamagic or whatnot) to prevent this namespace collision without having to rename my class or alter the actual gem. Thanks.

EDIT:

Instead of renaming/wrapping the class Platform throughout my app, and since the module Platform is only required by one other gem, I resulted to forking both gems, renaming Platform to XPlatform (both for the original gem and the one that imports it), and rebuilding them both. That seemed to work [sigh].

Upvotes: 3

Views: 3624

Answers (2)

Davie Overman
Davie Overman

Reputation: 114

At the top of the class before any actions, you could just make an alias.

NewPlatform = ActiveRecord::

And just use the alias in the action for that class to avoid namespace conflicts.

Upvotes: 0

AnkitG
AnkitG

Reputation: 6568

if the conditions are unavoidable when you cannot change the name

try wrapping your activerecord class in a module

module YourModule
 class Platform < Activerecord::Base
 named_scope :your_scope, lambda {#your code here}

 end
end

So the only change that you have to do is something like

YourModule::Platform.your_scope

Upvotes: 4

Related Questions