Possible to extend (not overwrite) a plugin method?

Hi,

Is it possible to extend a plugin method?

In a class that uses a plugin which already defines, say, a
before_create method,
is it possible to extend this before_create so the code in the plugin’s
method is also invoked ? Something in effect like calling super in an
inherited class’ overwriting method. I suppose this is either easy
with some less-known syntax, which I haven’t found, or this is
impossible currently (I know it’s not a biggie to copy over the code
from the plugin method, but…)

Thanks!

Philip T. wrote:

Hi,

Is it possible to extend a plugin method?

In a class that uses a plugin which already defines, say, a
before_create method,
is it possible to extend this before_create so the code in the plugin’s
method is also invoked ? Something in effect like calling super in an
inherited class’ overwriting method. I suppose this is either easy
with some less-known syntax, which I haven’t found, or this is
impossible currently (I know it’s not a biggie to copy over the code
from the plugin method, but…)

Thanks!

You have do a double alias_method trick

class Bar
def foo
‘foo’
end

def foo_with_bar
  foo_without_bar + 'bar'
end

alias_method :foo_without_bar, :foo
alias_method :foo, :foo_with_bar

end

Bar.new.foo #=> “foobar”

But, if you ask me, the best is the subclass it. This is what super is
for.

class MyBar < Bar
def foo
super + ‘bar’
end
end

Much cleaner. Now just use MyBar in your app instead of the class
provided by the plugin.

On 12/14/06, Alex W. [email protected] wrote:

end

class MyBar < Bar
def foo
super + ‘bar’
end
end

Much cleaner. Now just use MyBar in your app instead of the class
provided by the plugin.

I agree that subclasses are better when possible. That said, if you
do have to alias and chain, use alias_method_chain to keep it cleaner:

Thanks Alex! Unfortunately the plugin offers mixin methods, such as
one of those acts_as_… plugins… So, there is no class I can
subclass from.

What a wicked double alias… This is oddly cool in a way…

Thanks Rob! Pretty cool.

So glad to know advanced RoR’ers out there like you and Alex.