Enhancing model in one engine from another

Hi,

I need some help. I’ve got two engines. From within the second
engine, I need to extend a model in the first engine.

I’ve tried putting the code everywhere. It kind of works when it’s
in the lib directory. But only the first time after the engine
starts. Then it can’t it can’t find the extra functions anymore.
Where is the right place to do this in an engine?

For the interested, I’m extending the User model in active_rbac to
hook into a more extensive permission system.

(…time…)

Ok, now I remember that it says in the read me you can’t really do
this, yet. Does this have something to do with the engine loading
system? Is there hope for the future?

Thanks,
Brett

Hi Brett,

With models (and other library code), the Engines plugin will only
load the first file for a given name that it finds. So, if you have an
engine with a ‘Blah’ model, and a second engine which has a newer
‘Blah’ model that you want to inherit from the first engine, you’re
going to need to use namespaces to distinguish between those models.

However, if you have

vendor/plugins/engine1/app/models/model1.rb:
class Model1 < ActiveRecord::Base; end

and

vendor/plugins/engine2/app/models/model2.rb:
class Model2 < Model1

… then that should work (you’ll need to set up your DB table to
deal with STI though).

If you’re trying to add methods to an existing model, rather than
subclass it, that’s where the current wrinkles are with the engine
model loading process. I’ve recommended that engine developers provide
their model classes as ‘stubs’ with most of the functionality in
models for this reason (see the login/user engines for examples of
this).

The above said, I’m not 100% familiar with how ActiveRBAC is built, so
you might want to get in touch with those guys directly.

  • james

On 3/20/06, Brett W. [email protected] wrote:

For the interested, I’m extending the User model in active_rbac to


engine-developers mailing list
[email protected]
http://lists.rails-engines.org/listinfo.cgi/engine-developers-rails-engines.org

  • J *
    ~