diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-08-06 19:52:11 -0300 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-08-06 19:52:11 -0300 |
commit | bd6b61be88dfe6eb1ff1dcc5c17542d804a842c7 (patch) | |
tree | 13be5181476dc71aca97bbefb795eeb814949e0e /actionpack/lib/action_controller/metal/render_options.rb | |
parent | 52798fd479d4acbf823d093b03bdd1acf8e86b62 (diff) | |
download | rails-bd6b61be88dfe6eb1ff1dcc5c17542d804a842c7.tar.gz rails-bd6b61be88dfe6eb1ff1dcc5c17542d804a842c7.tar.bz2 rails-bd6b61be88dfe6eb1ff1dcc5c17542d804a842c7.zip |
Rename /base to /metal and make base.rb and metal.rb top-level to reflect their module locations
Diffstat (limited to 'actionpack/lib/action_controller/metal/render_options.rb')
-rw-r--r-- | actionpack/lib/action_controller/metal/render_options.rb | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/metal/render_options.rb b/actionpack/lib/action_controller/metal/render_options.rb new file mode 100644 index 0000000000..65ee09883e --- /dev/null +++ b/actionpack/lib/action_controller/metal/render_options.rb @@ -0,0 +1,103 @@ +module ActionController + module RenderOptions + extend ActiveSupport::Concern + + included do + extlib_inheritable_accessor :_renderers + self._renderers = [] + end + + module ClassMethods + def _write_render_options + renderers = _renderers.map do |r| + <<-RUBY_EVAL + if options.key?(:#{r}) + _process_options(options) + return _render_#{r}(options[:#{r}], options) + end + RUBY_EVAL + end + + class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def _handle_render_options(options) + #{renderers.join} + end + RUBY_EVAL + end + + def _add_render_option(name) + _renderers << name + _write_render_options + end + 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 + end + end + + module Renderers + module Json + extend RenderOption + register_renderer :json + + 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 + end + end + + module Js + extend RenderOption + register_renderer :js + + def _render_js(js, options) + self.content_type ||= Mime::JS + self.response_body = js + 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 + 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 + end + + module All + extend ActiveSupport::Concern + + include ActionController::Renderers::Json + include ActionController::Renderers::Js + include ActionController::Renderers::Xml + include ActionController::Renderers::RJS + end + end +end |