diff options
author | Xavier Noria <fxn@hashref.com> | 2014-09-04 00:18:39 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2014-09-04 00:45:01 +0200 |
commit | 4dfe140ef3fbed34e6ae8af3b32402630a4690a1 (patch) | |
tree | 279954dc1f540a5b4c38502f0e5155902a6bd657 /actionpack | |
parent | faa0f324345f48c9f41f02c626055a7fadf1ddf4 (diff) | |
download | rails-4dfe140ef3fbed34e6ae8af3b32402630a4690a1.tar.gz rails-4dfe140ef3fbed34e6ae8af3b32402630a4690a1.tar.bz2 rails-4dfe140ef3fbed34e6ae8af3b32402630a4690a1.zip |
code gardening in ActionController::Renderers
* Renames _handle_render_options to _render_to_body_with_renderer, which is more
intention-revealing.
* The name of the dynamically generated method for a renderer with key :js was
"_render_option_js".
That name is too weak. :js is an option if you see the render argument as just
a generic options hash, but in the context of renderers that's the renderer
key, is what identifies the renderer.
Now "_render_with_renderer_js" is generated instead, which is crystal clear.
* The name of the dynamically generated method for the renderer was constructed
using string literals in a few places. That is now encapsulated in a method.
* Since we were on it, also removed a couple of redundant selfs.
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/metal/renderers.rb | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/actionpack/lib/action_controller/metal/renderers.rb b/actionpack/lib/action_controller/metal/renderers.rb index 02c4e563f5..bc94536c8c 100644 --- a/actionpack/lib/action_controller/metal/renderers.rb +++ b/actionpack/lib/action_controller/metal/renderers.rb @@ -34,14 +34,15 @@ module ActionController end def render_to_body(options) - _handle_render_options(options) || super + _render_to_body_with_renderer(options) || super end - def _handle_render_options(options) + def _render_to_body_with_renderer(options) _renderers.each do |name| if options.key?(name) _process_options(options) - return send("_render_option_#{name}", options.delete(name), options) + method_name = Renderers._render_with_renderer_method_name(name) + return send(method_name, options.delete(name), options) end end nil @@ -51,6 +52,10 @@ module ActionController # Default values are <tt>:json</tt>, <tt>:js</tt>, <tt>:xml</tt>. RENDERERS = Set.new + def self._render_with_renderer_method_name(key) + "_render_with_renderer_#{key}" + end + # Adds a new renderer to call within controller actions. # A renderer is invoked by passing its name as an option to # <tt>AbstractController::Rendering#render</tt>. To create a renderer @@ -84,7 +89,7 @@ module ActionController # <tt>ActionController::MimeResponds::ClassMethods.respond_to</tt> and # <tt>ActionController::MimeResponds#respond_with</tt> def self.add(key, &block) - define_method("_render_option_#{key}", &block) + define_method(_render_with_renderer_method_name(key), &block) RENDERERS << key.to_sym end @@ -95,8 +100,8 @@ module ActionController # ActionController::Renderers.remove(:csv) def self.remove(key) RENDERERS.delete(key.to_sym) - method = "_render_option_#{key}" - remove_method(method) if method_defined?(method) + method_name = _render_with_renderer_method_name(key) + remove_method(method_name) if method_defined?(method_name) end module All @@ -112,7 +117,7 @@ module ActionController json = json.to_json(options) unless json.kind_of?(String) if options[:callback].present? - if self.content_type.nil? || self.content_type == Mime::JSON + if content_type.nil? || content_type == Mime::JSON self.content_type = Mime::JS end |