diff --git a/lib/turbolinks.rb b/lib/turbolinks.rb index 4dbecf89..877d6a0b 100644 --- a/lib/turbolinks.rb +++ b/lib/turbolinks.rb @@ -11,8 +11,13 @@ class Engine < ::Rails::Engine ActiveSupport.on_load(:action_controller) do ActionController::Base.class_eval do include XHRHeaders, Cookies, XDomainBlocker, Redirection - before_filter :set_xhr_redirected_to, :set_request_method_cookie - after_filter :abort_xdomain_redirect + if respond_to?(:before_action) + before_action :set_xhr_redirected_to, :set_request_method_cookie + after_action :abort_xdomain_redirect + else + before_filter :set_xhr_redirected_to, :set_request_method_cookie + after_filter :abort_xdomain_redirect + end end ActionDispatch::Request.class_eval do @@ -25,7 +30,11 @@ def referer ActiveSupport.on_load(:action_view) do (ActionView::RoutingUrlFor rescue ActionView::Helpers::UrlHelper).module_eval do - include XHRUrlFor + if defined?(prepend) && Rails.version >= '4' + prepend XHRUrlFor + else + include LegacyXHRUrlFor + end end end unless RUBY_VERSION =~ /^1\.8/ end diff --git a/lib/turbolinks/redirection.rb b/lib/turbolinks/redirection.rb index 27722402..d6a36746 100644 --- a/lib/turbolinks/redirection.rb +++ b/lib/turbolinks/redirection.rb @@ -3,13 +3,13 @@ module Turbolinks # will respond with a JavaScript call to Turbolinks.visit(url). module Redirection extend ActiveSupport::Concern - + def redirect_via_turbolinks_to(url = {}, response_status = {}) redirect_to(url, response_status) self.status = 200 self.response_body = "Turbolinks.visit('#{location}');" - response.content_type = Mime::JS + response.content_type = Mime[:js] end end -end \ No newline at end of file +end diff --git a/lib/turbolinks/xhr_url_for.rb b/lib/turbolinks/xhr_url_for.rb index 5739911e..01b2429a 100644 --- a/lib/turbolinks/xhr_url_for.rb +++ b/lib/turbolinks/xhr_url_for.rb @@ -1,15 +1,22 @@ module Turbolinks - # Corrects the behavior of url_for (and link_to, which uses url_for) with the :back - # option by using the X-XHR-Referer request header instead of the standard Referer + # Corrects the behavior of url_for (and link_to, which uses url_for) with the :back + # option by using the X-XHR-Referer request header instead of the standard Referer # request header. - module XHRUrlFor + module LegacyXHRUrlFor def self.included(base) base.alias_method_chain :url_for, :xhr_referer end - + def url_for_with_xhr_referer(options = {}) options = (controller.request.headers["X-XHR-Referer"] || options) if options == :back url_for_without_xhr_referer options end end + + module XHRUrlFor + def url_for(options = {}) + options = (controller.request.headers["X-XHR-Referer"] || options) if options == :back + super options + end + end end