Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
handle_redirect_loop_if_appears
class ApplicationController < ActionController::Base
...
before_filter :handle_redirect_loop_if_appears
...
after_filter :set_session_back_url
...
def handle_redirect_loop_if_appears
return session[:redirect_loop_count] = nil unless request_repeats?
return session[:redirect_loop_count] = 1 unless session[:redirect_loop_count]
max_observed_time_diff_betn_reqs_when_in_loop = 10000
max_loop_count = 3
session[:last_request_time] = Time.now.to_i
session[:redirect_loop_count] += 1
return if session[:redirect_loop_count] < max_loop_count
if last_request_after < max_observed_time_diff_betn_reqs_when_in_loop
session[:last_request_time] = nil
session[:redirect_loop_count] = nil
redirect_to rescue_url and return
end
end
def last_request_after
return unless session[:last_request_time]
time_diff = Time.now.to_i - session[:last_request_time].to_i
time_diff = time_diff * (-1) if time_diff < 0
end
def rescue_url
home_url
end
def home_url
'/'
end
def request_repeats?
session[:back_url] == request.fullpath
end
def set_session_back_url
return unless request.get?
return if request.xhr?
session[:back_url] = request.fullpath
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment