Skip to content

Instantly share code, notes, and snippets.

@sshaw
Created May 1, 2010 05:29
Show Gist options
  • Save sshaw/386070 to your computer and use it in GitHub Desktop.
Save sshaw/386070 to your computer and use it in GitHub Desktop.
Rails current_page? Style Referer Function
# 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