1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added the ability to register methods to handle specific render option keys and render :json

This commit is contained in:
Yehuda Katz + Carl Lerche 2009-05-21 14:34:42 -07:00
parent e693f45e15
commit 2daac47d58
4 changed files with 46 additions and 2 deletions

View file

@ -62,8 +62,9 @@ Rake::TestTask.new(:test_new_base_on_old_tests) do |t|
# Dir.glob( "test/{dispatch,template}/**/*_test.rb" ).sort + # Dir.glob( "test/{dispatch,template}/**/*_test.rb" ).sort +
t.test_files = %w( t.test_files = %w(
addresses_render base benchmark caching capture content_type dispatcher addresses_render base benchmark caching capture content_type dispatcher
flash mime_responds record_identifier redirect render rescue url_rewriter flash mime_responds record_identifier redirect
webservice verification request_forgery_protection send_file render render_json
send_file request_forgery_protection rescue url_rewriter verification webservice
).map { |name| "test/controller/#{name}_test.rb" } ).map { |name| "test/controller/#{name}_test.rb" }
end end

View file

@ -7,6 +7,8 @@ module ActionController
autoload :Rails2Compatibility, "action_controller/new_base/compatibility" autoload :Rails2Compatibility, "action_controller/new_base/compatibility"
autoload :Redirector, "action_controller/new_base/redirector" autoload :Redirector, "action_controller/new_base/redirector"
autoload :Renderer, "action_controller/new_base/renderer" 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 :Rescue, "action_controller/new_base/rescuable"
autoload :Testing, "action_controller/new_base/testing" autoload :Testing, "action_controller/new_base/testing"
autoload :UrlFor, "action_controller/new_base/url_for" autoload :UrlFor, "action_controller/new_base/url_for"

View file

@ -11,6 +11,8 @@ module ActionController
include ActionController::UrlFor include ActionController::UrlFor
include ActionController::Redirector include ActionController::Redirector
include ActionController::Renderer include ActionController::Renderer
include ActionController::RenderOptions
include ActionController::Renderers::Json
include ActionController::Layouts include ActionController::Layouts
include ActionController::ConditionalGet include ActionController::ConditionalGet

View file

@ -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