Skip to content

Instantly share code, notes, and snippets.

@sshaw sshaw/gist:386070

Created May 1, 2010
Embed
What would you like to do?
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
You can’t perform that action at this time.