Forum: Rails Engines development Extending models from engines

Posted by Peter Bex (Guest)
on 2007-02-21 14:32
(Received via mailing list)
Hi there,

We've been busy on writing our own little engine, and we didn't want to
put our engine's model code in a lib or make it an abstract class, since
that isn't very clean :)  Of course, copying the model code completely 
into
the application's model is right out.

While thinking about this, it occurred to me that it's very easy to let 
Ruby
override the engine's model:

In RAILS_ROOT/app/models/foo.rb:

require RAILS_ROOT + "/vendor/plugins/my_engine/app/models/foo"

class Foo < ActiveRecord::Base
  def bar
    qux()
  end
end

This simply works.  When you call Foo.new.blah(), it calls the engine's
model's qux() function.  Perhaps this should be added to the 
documentation as
this is a lot cleaner (IMHO) than the other two suggestions that are in 
the
documentation?

Regards,
Peter Bex
Solide ICT - http://www.solide-ict.nl
Posted by Peter Bex (Guest)
on 2007-02-21 15:55
(Received via mailing list)
Hi there,

Oddly enough, now it appears to work.  I don't know what changed,
unfortunately.  We moved a lot of code around and made some major
changes, so it might have accidentally been fixed.  Thanks for the
reply, anyway.

Regards,
Peter Bex
Solide ICT - http://www.solide-ict.nl
Posted by Peter Bex (Guest)
on 2007-02-22 09:02
(Received via mailing list)
On Wed, Feb 21, 2007 at 03:56:27PM +0100, Peter Bex wrote:
> Hi there,
> 
> Oddly enough, now it appears to work.  I don't know what changed,
> unfortunately.  We moved a lot of code around and made some major
> changes, so it might have accidentally been fixed.  Thanks for the
> reply, anyway.

Hmm, this should've been a reply to my other thread about testing.
Sorry for the noise.

Regards,
Peter Bex
Solide ICT - http://www.solide-ict.nl
Posted by Chuck Vose (Guest)
on 2007-02-23 04:46
(Received via mailing list)
On 2/21/07, Peter Bex <Peter.Bex@solide-ict.nl> wrote:
> While thinking about this, it occurred to me that it's very easy to let Ruby
> override the engine's model:
>
> In RAILS_ROOT/app/models/foo.rb:
>
> require RAILS_ROOT + "/vendor/plugins/my_engine/app/models/foo"
>
> class Foo < ActiveRecord::Base
> end

I knew there had to be a better way. I just started monkeying around
with this myself but I'm pleased you beat me to it. You would think
that someone would have thought of this before though. Maybe there's
some hidden risk that you and I are both missing?

-Chuck Vose
Posted by James Adam (Guest)
on 2007-02-23 11:18
(Received via mailing list)
There's no drawback - you're manually doing what the 'code mixing'
process for controllers and helpers does automatically :)

- James
Posted by ben wiseley (Guest)
on 2007-02-26 17:12
(Received via mailing list)
Any idea why putting:

   include Reloadable

in the class would hose things up?  The idea below works great but I 
have to
restart webbrick/mongrel every time I make a change.  If I put in 
"include
Reloadable" it seems to break everything.

-ben
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.