# RJS: how to generate different code? plugins?

**URL:** <https://www.ruby-forum.com/t/rjs-how-to-generate-different-code-plugins/81399>\
**Category:** Rails\
**Created:** [January 8, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/rjs-how-to-generate-different-code-plugins/81399 "2007-01-08T20:35:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![duma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@duma](https://www.ruby-forum.com/u/duma)\
**Post date:** [January 8, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/rjs-how-to-generate-different-code-plugins/81399/1 "2007-01-08T20:35:41Z")

</div>

Hi everyone 🙂

If I wanted to make an RJS template generate something, how would I go  
about  
doing that? Like, right now you have page.insert\_html, etc., but let’s  
say  
I wanted to define my\_page.insert\_html, and I want it to generate  
different  
code. How would I do that?

## Thanks! Sean

View this message in context:  
[http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8224678](http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8224678)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![duma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@duma](https://www.ruby-forum.com/u/duma)\
**Post date:** [January 9, 2007, 12:29am UTC](https://www.ruby-forum.com/t/rjs-how-to-generate-different-code-plugins/81399/2 "2007-01-09T00:29:21Z")

</div>

duma wrote:

> Hi everyone 🙂
> 
> If I wanted to make an RJS template generate something, how would I go  
> about  
> doing that? Like, right now you have page.insert\_html, etc., but let’s  
> say  
> I wanted to define my\_page.insert\_html, and I want it to generate  
> different  
> code. How would I do that?
> 
> ## Thanks! Sean
> 
> View this message in context:  
> [http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8224678](http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8224678)  
> Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

It seems that that page object is a:

ActionView::Helpers::PrototypeHelper::JavaScriptGenerator

So you should be able to extend that. I just tested this out and it  
seems to work fine.

class ActionView::Helpers::PrototypeHelper::JavaScriptGenerator  
def foo(m)  
call “alert”, m  
#same as: call “alert(‘#{m}’)”  
end  
end

then you can do:

render(:update) { |page| page.foo(‘Foo!’) }  
#=\> “alert(‘foo’)”

If you want to override one the original methods, you can use  
alias\_method\_chain.

class ActionView::Helpers::PrototypeHelper::JavaScriptGenerator  
def alert\_with\_foo(message)  
alert\_without\_foo(“#{message} Foo!”)  
end  
alias\_method\_chain :alert, :foo  
end

render(:update) { |page| page.alert(‘Hi’) }  
#=\> “alert(‘Hi Foo!’)”

---

<div class="post-metadata">

**Author:** ![duma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8c91f0/32.png) [@duma](https://www.ruby-forum.com/u/duma)\
**Post date:** [January 9, 2007, 1:53am UTC](https://www.ruby-forum.com/t/rjs-how-to-generate-different-code-plugins/81399/3 "2007-01-09T01:53:07Z")

</div>

Nice!! Thank you so much!! 🙂

Paul Johnson-18 wrote:

> > different
> 
> end  
> def alert\_with\_foo(message)
> 
> >

–  
View this message in context:  
[http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8230019](http://www.nabble.com/RJS%3A-how-to-generate-different-code---plugins--tf2941465.html#a8230019)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).
