diff --git a/actionpack/Rakefile b/actionpack/Rakefile index 5968317c60..2041f5a844 100644 --- a/actionpack/Rakefile +++ b/actionpack/Rakefile @@ -62,8 +62,9 @@ Rake::TestTask.new(:test_new_base_on_old_tests) do |t| # Dir.glob( "test/{dispatch,template}/**/*_test.rb" ).sort + t.test_files = %w( addresses_render base benchmark caching capture content_type dispatcher - flash mime_responds record_identifier redirect render rescue url_rewriter - webservice verification request_forgery_protection send_file + flash mime_responds record_identifier redirect + render render_json + send_file request_forgery_protection rescue url_rewriter verification webservice ).map { |name| "test/controller/#{name}_test.rb" } end diff --git a/actionpack/lib/action_controller/new_base.rb b/actionpack/lib/action_controller/new_base.rb index 58c7382661..95808decd5 100644 --- a/actionpack/lib/action_controller/new_base.rb +++ b/actionpack/lib/action_controller/new_base.rb @@ -7,6 +7,8 @@ module ActionController autoload :Rails2Compatibility, "action_controller/new_base/compatibility" autoload :Redirector, "action_controller/new_base/redirector" autoload :Renderer, "action_controller/new_base/renderer" + autoload :RenderOptions, "action_controller/new_base/render_options" + autoload :Renderers, "action_controller/new_base/render_options" autoload :Rescue, "action_controller/new_base/rescuable" autoload :Testing, "action_controller/new_base/testing" autoload :UrlFor, "action_controller/new_base/url_for" diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index b8674d5099..6e1f92c45d 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -11,6 +11,8 @@ module ActionController include ActionController::UrlFor include ActionController::Redirector include ActionController::Renderer + include ActionController::RenderOptions + include ActionController::Renderers::Json include ActionController::Layouts include ActionController::ConditionalGet diff --git a/actionpack/lib/action_controller/new_base/render_options.rb b/actionpack/lib/action_controller/new_base/render_options.rb new file mode 100644 index 0000000000..e7ed2bd278 --- /dev/null +++ b/actionpack/lib/action_controller/new_base/render_options.rb @@ -0,0 +1,39 @@ +module ActionController + module RenderOptions + extend ActiveSupport::DependencyModule + + included do + extlib_inheritable_accessor :_renderers + self._renderers = [] + end + + def render_to_body(options) + _renderers.each do |renderer| + if options.key?(renderer) + _process_options(options) + return send("_render_#{renderer}", options[renderer], options) + end + end + super + end + end + + module Renderers + module Json + extend ActiveSupport::DependencyModule + + depends_on RenderOptions + + included do + _renderers << :json + end + + def _render_json(json, options) + json = ActiveSupport::JSON.encode(json) unless json.respond_to?(:to_str) + json = "#{options[:callback]}(#{json})" unless options[:callback].blank? + response.content_type ||= Mime::JSON + self.response_body = json + end + end + end +end \ No newline at end of file