Created
December 27, 2008 01:19
-
-
Save royw/40167 to your computer and use it in GitHub Desktop.
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
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