Created
May 1, 2010 05:29
-
-
Save sshaw/386070 to your computer and use it in GitHub Desktop.
Rails current_page? Style Referer Function
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
# Usage: | |
# referer? :controller => 'beezies', :action => 'holla', :type => 'popozuda' | |
# referer? %r|/\d+/edit| | |
def referer?(options) | |
referer = request.headers["Referer"] | |
return (options.blank? ? true : false) if referer.blank? | |
if options.is_a?(Regexp) | |
referer =~ options | |
else | |
# Same logic as current_page? in actionpack/lib/action_view/helpers/url_helper.rb | |
url_string = CGI.unescapeHTML(url_for(options)) | |
# We ignore any extra parameters in the referer if the | |
# submitted url doesn't have any either. This lets the function | |
# work with things like ?order=asc | |
referer = referer.split("?").first unless url_string.index("?") | |
if url_string =~ /^\w+:\/\// | |
referer == url_string | |
else | |
referer == "#{request.protocol}#{request.host_with_port}#{url_string}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment