Skip to content

Instantly share code, notes, and snippets.

@royw
Created December 27, 2008 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royw/40167 to your computer and use it in GitHub Desktop.
Save royw/40167 to your computer and use it in GitHub Desktop.
class Application
before :back_navigation
# maintain a back trace stack in session[:page_history], removing
# any path loops. back_resource() in global_helper will then
# use session[:page_history].
def back_navigation
session[:page_history] ||= []
previous_page = request.env['HTTP_REFERER'].to_s
current_page = (request.env['rack.url_scheme'] +
'://' +
request.env['HTTP_HOST'] +
request.env['REQUEST_PATH']
).to_s
unless previous_page.nil?
session[:page_history] << previous_page
session[:page_history].compact!
index = session[:page_history].index(current_page)
unless index.nil?
if index == 0
session[:page_history].clear
else
session[:page_history] = session[:page_history][0..(index - 1)]
end
end
end
end
end
module Merb
module GlobalHelpers
# returns a resource, either then last one in the session[:page_history]
# stack or if none available, the resource specified in the args
def back_resource(*args)
session[:page_history] ||= []
session[:page_history].last || resource(*args)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment