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.
This commit is contained in:
Xavier Noria 2014-09-04 00:18:39 +02:00
parent faa0f32434
commit 4dfe140ef3

@ -34,14 +34,15 @@ def use_renderers(*args)
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 @@ def _handle_render_options(options)
# 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 @@ def _handle_render_options(options)
# <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 @@ def self.add(key, &block)
# 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 All
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