# A kind of monkey patch and an invitation for criticism for all you (who know this stuff better than

**URL:** <https://www.ruby-forum.com/t/a-kind-of-monkey-patch-and-an-invitation-for-criticism-for-all-you-who-know-this-stuff-better-than/130860>\
**Category:** Rails\
**Created:** [March 4, 2008, 12:08am UTC](https://www.ruby-forum.com/t/a-kind-of-monkey-patch-and-an-invitation-for-criticism-for-all-you-who-know-this-stuff-better-than/130860 "2008-03-04T00:08:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara\_vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a87d85/32.png) [@ara\_vartanian](https://www.ruby-forum.com/u/ara_vartanian)\
**Post date:** [March 4, 2008, 12:08am UTC](https://www.ruby-forum.com/t/a-kind-of-monkey-patch-and-an-invitation-for-criticism-for-all-you-who-know-this-stuff-better-than/130860/1 "2008-03-04T00:08:45Z")

</div>

I’ve often felt the need including Rails’ view helpers within my  
controllers, if only for some simple things like formatting my flash  
notices with text helpers or html escaping. I loved this suggestion,  
which helps by not cluttering the namespace of controllers:

[http://snippets.dzone.com/posts/show/1799](http://snippets.dzone.com/posts/show/1799)

But I wanted to generalize this a little to automatically include  
whatever helper modules are included in the views. So I dreamt up 35  
lines of goodness:

class Object  
def metaclass; class \<\< self; self; end; end  
end

module ActionController  
class Base  
class\_inheritable\_accessor :controller\_helper\_module  
class\_inheritable\_accessor :controller\_helper\_object  
self.controller\_helper\_module = Module.new  
self.controller\_helper\_object = Object.new

```
class << self
  def add_template_helper_with_controller_helper(helper_module)
    self.controller_helper_module.send(:include, helper_module)
    recreate_controller_helper_object
    add_template_helper_without_controller_helper(helper_module)
  end
  alias_method_chain :add_template_helper, :controller_helper

  def inherited_with_controller_helper(child)
    inherited_without_controller_helper(child)
    child.controller_helper_module.send :include,

```

controller\_helper\_module  
child.recreate\_controller\_helper\_object  
end  
alias\_method\_chain :inherited, :controller\_helper

```
  def recreate_controller_helper_object
    self.controller_helper_object = Object.new
    self.controller_helper_object.metaclass.send(:include,

```

controller\_helper\_module)  
end  
end

```
protected

def help
  self.class.controller_helper_object
end

```

end  
end

===

I understand some people will balk at the idea of including these  
helpers in controllers, and I understand their reasons. I guess I’m  
just looking for criticism of the code, given that some people do want  
to do this kind of thing.

It seems to work just fine in Rails 2.0, but I wonder if there are any  
performance problems or if there is something technically incorrect  
that jumps out at people.

– Ara
