2010-01-31 21:32:28 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
2010-03-28 08:15:02 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2010-01-31 21:32:28 -05:00
|
|
|
|
2009-05-21 17:34:42 -04:00
|
|
|
module ActionController
|
2009-12-09 16:38:47 -05:00
|
|
|
def self.add_renderer(key, &block)
|
2009-12-20 21:15:20 -05:00
|
|
|
Renderers.add(key, &block)
|
2009-12-09 16:38:47 -05:00
|
|
|
end
|
|
|
|
|
2009-12-20 21:15:20 -05:00
|
|
|
module Renderers
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-21 17:34:42 -04:00
|
|
|
included do
|
2010-01-31 21:32:28 -05:00
|
|
|
class_attribute :_renderers
|
|
|
|
self._renderers = {}.freeze
|
2009-05-21 17:34:42 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-21 18:23:52 -04:00
|
|
|
module ClassMethods
|
|
|
|
def _write_render_options
|
2009-12-09 16:38:47 -05:00
|
|
|
renderers = _renderers.map do |name, value|
|
2009-05-21 18:23:52 -04:00
|
|
|
<<-RUBY_EVAL
|
2009-12-09 16:38:47 -05:00
|
|
|
if options.key?(:#{name})
|
2009-05-21 18:23:52 -04:00
|
|
|
_process_options(options)
|
2010-02-24 09:45:33 -05:00
|
|
|
return _render_option_#{name}(options.delete(:#{name}), options)
|
2009-05-21 18:23:52 -04:00
|
|
|
end
|
|
|
|
RUBY_EVAL
|
2009-05-21 17:34:42 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-21 18:23:52 -04:00
|
|
|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
|
|
|
def _handle_render_options(options)
|
|
|
|
#{renderers.join}
|
|
|
|
end
|
|
|
|
RUBY_EVAL
|
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
def use_renderers(*args)
|
2010-01-31 21:32:28 -05:00
|
|
|
new = _renderers.dup
|
2009-12-09 16:38:47 -05:00
|
|
|
args.each do |key|
|
2010-01-31 21:32:28 -05:00
|
|
|
new[key] = RENDERERS[key]
|
2009-12-09 16:38:47 -05:00
|
|
|
end
|
2010-01-31 21:32:28 -05:00
|
|
|
self._renderers = new.freeze
|
2009-05-21 18:23:52 -04:00
|
|
|
_write_render_options
|
2009-05-21 17:34:42 -04:00
|
|
|
end
|
2009-12-09 16:38:47 -05:00
|
|
|
alias use_renderer use_renderers
|
2009-05-21 18:23:52 -04:00
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-21 18:23:52 -04:00
|
|
|
def render_to_body(options)
|
|
|
|
_handle_render_options(options) || super
|
2009-05-21 17:34:42 -04:00
|
|
|
end
|
2009-05-21 17:51:29 -04:00
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
RENDERERS = {}
|
|
|
|
def self.add(key, &block)
|
|
|
|
define_method("_render_option_#{key}", &block)
|
|
|
|
RENDERERS[key] = block
|
|
|
|
All._write_render_options
|
2009-05-21 17:51:29 -04:00
|
|
|
end
|
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
module All
|
|
|
|
extend ActiveSupport::Concern
|
2009-12-20 21:15:20 -05:00
|
|
|
include Renderers
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
INCLUDED = []
|
|
|
|
included do
|
|
|
|
self._renderers = RENDERERS
|
|
|
|
_write_render_options
|
|
|
|
INCLUDED << self
|
2009-05-28 10:49:02 -04:00
|
|
|
end
|
2009-05-22 20:07:40 -04:00
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
def self._write_render_options
|
|
|
|
INCLUDED.each(&:_write_render_options)
|
2009-05-22 20:07:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
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
|
2009-05-21 17:51:29 -04:00
|
|
|
end
|
2009-05-21 19:35:40 -04:00
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
add :js do |js, options|
|
|
|
|
self.content_type ||= Mime::JS
|
|
|
|
self.response_body = js.respond_to?(:to_js) ? js.to_js : js
|
2009-05-21 19:35:40 -04:00
|
|
|
end
|
2009-05-22 20:15:46 -04:00
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
add :xml do |xml, options|
|
|
|
|
self.content_type ||= Mime::XML
|
|
|
|
self.response_body = xml.respond_to?(:to_xml) ? xml.to_xml : xml
|
|
|
|
end
|
2009-05-22 20:15:46 -04:00
|
|
|
|
2009-12-09 16:38:47 -05:00
|
|
|
add :update do |proc, options|
|
2010-03-18 18:52:43 -04:00
|
|
|
view_context = self.view_context
|
2009-12-09 16:38:47 -05:00
|
|
|
generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(view_context, &proc)
|
2010-03-12 14:39:53 -05:00
|
|
|
self.content_type = Mime::JS
|
2009-12-09 16:38:47 -05:00
|
|
|
self.response_body = generator.to_s
|
2009-05-22 20:15:46 -04:00
|
|
|
end
|
2009-05-21 17:34:42 -04:00
|
|
|
end
|
2009-05-21 23:54:28 -04:00
|
|
|
end
|