# Possible to extend (not overwrite) a plugin method?

**URL:** <https://www.ruby-forum.com/t/possible-to-extend-not-overwrite-a-plugin-method/79411>\
**Category:** Rails\
**Created:** [December 14, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/possible-to-extend-not-overwrite-a-plugin-method/79411 "2006-12-14T22:01:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amcniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@amcniece](https://www.ruby-forum.com/u/amcniece)\
**Post date:** [December 14, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/possible-to-extend-not-overwrite-a-plugin-method/79411/1 "2006-12-14T22:01:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![amcniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@amcniece](https://www.ruby-forum.com/u/amcniece)\
**Post date:** [December 15, 2006, 6:49am UTC](https://www.ruby-forum.com/t/possible-to-extend-not-overwrite-a-plugin-method/79411/2 "2006-12-15T06:49:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![amcniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@amcniece](https://www.ruby-forum.com/u/amcniece)\
**Post date:** [December 15, 2006, 6:56am UTC](https://www.ruby-forum.com/t/possible-to-extend-not-overwrite-a-plugin-method/79411/3 "2006-12-15T06:56:09Z")

</div>

On 12/14/06, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

> **[New in Rails: Module#alias\_method\_chain](https://rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain)**
>
> Though not an outward facing feature, changeset 4276 introduces a nice method to DRY up and encapsulate the common pattern of aliasing a method so that you can build behavior on top of it.All over the internals of Rails you’ll find code like this in...

- Rob  
–  
[http://www.robsanheim.com](http://www.robsanheim.com)  
[http://www.seekingalpha.com](http://www.seekingalpha.com)  
[http://www.ajaxian.com](http://www.ajaxian.com)

---

<div class="post-metadata">

**Author:** ![amcniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@amcniece](https://www.ruby-forum.com/u/amcniece)\
**Post date:** [December 16, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/possible-to-extend-not-overwrite-a-plugin-method/79411/4 "2006-12-16T23:39:13Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![amcniece](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8edcca/32.png) [@amcniece](https://www.ruby-forum.com/u/amcniece)\
**Post date:** [December 16, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/possible-to-extend-not-overwrite-a-plugin-method/79411/5 "2006-12-16T23:45:14Z")

</div>

Thanks Rob! Pretty cool.

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