Permalink
Browse files

Simpler RenderOption API -- removes the need for registering the type…

…s and extending a module
  • Loading branch information...
1 parent 9fbde11 commit f9d570bdd80010825c21eb32204471ba525915fa Carlhuda committed Dec 9, 2009
@@ -1,19 +1,24 @@
module ActionController
+
+ def self.add_renderer(key, &block)
+ RenderOptions.add(key, &block)
+ end
+
module RenderOptions
extend ActiveSupport::Concern
included do
extlib_inheritable_accessor :_renderers
- self._renderers = []
+ self._renderers = {}
end
module ClassMethods
def _write_render_options
- renderers = _renderers.map do |r|
+ renderers = _renderers.map do |name, value|
<<-RUBY_EVAL
- if options.key?(:#{r})
+ if options.key?(:#{name})
_process_options(options)
- return render_#{r}(options[:#{r}], options)
+ return _render_option_#{name}(options[:#{name}], options)
end
RUBY_EVAL
end
@@ -25,79 +30,63 @@ def _handle_render_options(options)
RUBY_EVAL
end
- def _add_render_option(name)
- _renderers << name
+ def use_renderers(*args)
+ args.each do |key|
+ _renderers[key] = RENDERERS[key]
+ end
_write_render_options
end
+ alias use_renderer use_renderers
end
def render_to_body(options)
_handle_render_options(options) || super
end
- end
-
- module RenderOption #:nodoc:
- def self.extended(base)
- base.extend ActiveSupport::Concern
- base.send :include, ::ActionController::RenderOptions
- def base.register_renderer(name)
- included { _add_render_option(name) }
- end
+ RENDERERS = {}
+ def self.add(key, &block)
+ define_method("_render_option_#{key}", &block)
+ RENDERERS[key] = block
+ All._write_render_options
end
- end
- module RenderOptions
- module Json
- extend RenderOption
- register_renderer :json
+ module All
+ extend ActiveSupport::Concern
+ include RenderOptions
- def render_json(json, options)
- json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
- json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
- self.content_type ||= Mime::JSON
- self.response_body = json
+ INCLUDED = []
+ included do
+ self._renderers = RENDERERS
+ _write_render_options
+ INCLUDED << self
end
- end
-
- module Js
- extend RenderOption
- register_renderer :js
- def render_js(js, options)
- self.content_type ||= Mime::JS
- self.response_body = js.respond_to?(:to_js) ? js.to_js : js
+ def self._write_render_options
+ INCLUDED.each(&:_write_render_options)
end
end
- module Xml
- extend RenderOption
- register_renderer :xml
-
- def render_xml(xml, options)
- self.content_type ||= Mime::XML
- self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml : xml
- end
+ add :json do |json, options|
+ json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str)
+ json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
+ self.content_type ||= Mime::JSON
+ self.response_body = json
end
- module RJS
- extend RenderOption
- register_renderer :update
-
- def render_update(proc, options)
- generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(view_context, &proc)
- self.content_type = Mime::JS
- self.response_body = generator.to_s
- end
+ add :js do |js, options|
+ self.content_type ||= Mime::JS
+ self.response_body = js.respond_to?(:to_js) ? js.to_js : js
end
- module All
- extend ActiveSupport::Concern
+ add :xml do |xml, options|
+ self.content_type ||= Mime::XML
+ self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml : xml
+ end
- include ActionController::RenderOptions::Json
- include ActionController::RenderOptions::Js
- include ActionController::RenderOptions::Xml
- include ActionController::RenderOptions::RJS
+ add :update do |proc, options|
+ generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(view_context, &proc)
+ self.content_type = Mime::JS
+ self.response_body = generator.to_s
end
end
end
@@ -2,6 +2,11 @@
require 'controller/fake_models'
require 'pathname'
+ActionController.add_renderer :simon do |says, options|
+ self.content_type = Mime::TEXT
+ self.response_body = "Simon says: #{says}"
+end
+
class RenderOtherTest < ActionController::TestCase
class TestController < ActionController::Base
protect_from_forgery
@@ -109,6 +114,10 @@ def render_alternate_default
end
end
+ def render_simon_says
+ render :simon => "foo"
+ end
+
private
def default_render
if @alternate_default_render
@@ -240,4 +249,9 @@ def test_should_render_with_alternate_default_render
xhr :get, :render_alternate_default
assert_equal %(Element.replace("foo", "partial html");), @response.body
end
+
+ def test_using_custom_render_option
+ get :render_simon_says
+ assert_equal "Simon says: foo", @response.body
+ end
end

0 comments on commit f9d570b

Please sign in to comment.