aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/metal/helpers.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2016-05-05 12:02:28 -0500
committerRafael Mendonça França <rafaelmfranca@gmail.com>2016-05-05 16:17:17 -0500
commit541a51ecf8ee04f956b7d8efb13935640aa831ce (patch)
tree6345e2f9dc8ab3db011a7eed0a001c43438e7fa0 /actionpack/lib/action_controller/metal/helpers.rb
parentcece50d3a6b432f848ca04a92da331f8b032d51f (diff)
downloadrails-541a51ecf8ee04f956b7d8efb13935640aa831ce.tar.gz
rails-541a51ecf8ee04f956b7d8efb13935640aa831ce.tar.bz2
rails-541a51ecf8ee04f956b7d8efb13935640aa831ce.zip
Implement helpers proxy in controller instance level
It is a common pattern in the Rails community that when people want to :xa use any kind of helper that is defined inside app/helpers they includes the helper module inside the controller like: module UserHelper def my_user_helper # ... end end class UsersController < ApplicationController include UserHelper def index render inline: my_user_helper end end This has problem because the helper can't access anything that is defined in the view level context class. Also all public methods of the helper become available in the controller what can lead to undesirable methods being routed and behaving as actions. Also if you helper depends on other helpers or even Action View helpers you need to include each one of these dependencies in your controller otherwise your helper is not going to work. We already have a helpers proxy at controller class level but that proxy doesn't have access to the instance variables defined in the controller. With this new instance level helper proxy users can reuse helpers in the controller without having to include the modules and with access to instance variables defined in the controller. class UsersController < ApplicationController def index render inline: helpers.my_user_helper end end
Diffstat (limited to 'actionpack/lib/action_controller/metal/helpers.rb')
-rw-r--r--actionpack/lib/action_controller/metal/helpers.rb7
1 files changed, 6 insertions, 1 deletions
diff --git a/actionpack/lib/action_controller/metal/helpers.rb b/actionpack/lib/action_controller/metal/helpers.rb
index d3853e2e83..22493dea50 100644
--- a/actionpack/lib/action_controller/metal/helpers.rb
+++ b/actionpack/lib/action_controller/metal/helpers.rb
@@ -5,7 +5,7 @@ module ActionController
#
# In addition to using the standard template helpers provided, creating custom helpers to
# extract complicated logic or reusable functionality is strongly encouraged. By default, each controller
- # will include all helpers. These helpers are only accessible on the controller through <tt>.helpers</tt>
+ # will include all helpers. These helpers are only accessible on the controller through <tt>#helpers</tt>
#
# In previous versions of \Rails the controller will include a helper which
# matches the name of the controller, e.g., <tt>MyController</tt> will automatically
@@ -113,5 +113,10 @@ module ActionController
all_helpers_from_path(helpers_path)
end
end
+
+ # Provides a proxy to access helpers methods from outside the view.
+ def helpers
+ @_helper_proxy ||= view_context
+ end
end
end