Created
August 5, 2019 07:11
-
-
Save mikker/e3b0e3b660edeb09fec72464a0ee739c to your computer and use it in GitHub Desktop.
Monkey patch action controller in Rails 6.0.0.rc2 to avoid deprecation warning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Require this in config/application.rb after require "action_controller/railtie" | |
module ActionController | |
module Renderers | |
remove :json | |
remove :js | |
remove :xml | |
add :json do |json, options| | |
json = json.to_json(options) unless json.kind_of?(String) | |
if options[:callback].present? | |
if media_type.nil? || media_type == Mime[:json] | |
self.content_type = Mime[:js] | |
end | |
"/**/#{options[:callback]}(#{json})" | |
else | |
self.content_type = Mime[:json] if media_type.nil? | |
json | |
end | |
end | |
add :js do |js, options| | |
self.content_type = Mime[:js] if media_type.nil? | |
js.respond_to?(:to_js) ? js.to_js(options) : js | |
end | |
add :xml do |xml, options| | |
self.content_type = Mime[:xml] if media_type.nil? | |
xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml | |
end | |
end | |
end | |
module ActionController #:nodoc: | |
module RequestForgeryProtection | |
private | |
def non_xhr_javascript_response? | |
%r(\A(?:text|application)/javascript).match?(media_type) && !request.xhr? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment