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:
parent
e693f45e15
commit
2daac47d58
4 changed files with 46 additions and 2 deletions
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
39
actionpack/lib/action_controller/new_base/render_options.rb
Normal file
39
actionpack/lib/action_controller/new_base/render_options.rb
Normal 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
|
Loading…
Reference in a new issue