module Devise::Controllers::StoreLocation

Provide the ability to store a location. Used to redirect back to a desired path after sign in. Included by default in all controllers.

Public Instance Methods

store_location_for(resource_or_scope, location) click to toggle source

Stores the provided location to redirect the user after signing in. Useful in combination with the ‘stored_location_for` helper.

Example:

store_location_for(:user, dashboard_path)
redirect_to user_facebook_omniauth_authorize_path
# File lib/devise/controllers/store_location.rb, line 36
def store_location_for(resource_or_scope, location)
  session_key = stored_location_key_for(resource_or_scope)
  
  path = extract_path_from_location(location)
  session[session_key] = path if path
end
stored_location_for(resource_or_scope) click to toggle source

Returns and delete (if it’s navigational format) the url stored in the session for the given scope. Useful for giving redirect backs after sign up:

Example:

redirect_to stored_location_for(:user) || root_path
# File lib/devise/controllers/store_location.rb, line 18
def stored_location_for(resource_or_scope)
  session_key = stored_location_key_for(resource_or_scope)

  if is_navigational_format?
    session.delete(session_key)
  else
    session[session_key]
  end
end

Private Instance Methods

add_fragment_back_to_path(uri, path) click to toggle source
# File lib/devise/controllers/store_location.rb, line 71
def add_fragment_back_to_path(uri, path)
  [path, uri.fragment].compact.join('#')
end
extract_path_from_location(location) click to toggle source
# File lib/devise/controllers/store_location.rb, line 56
def extract_path_from_location(location)
  uri = parse_uri(location)

  if uri 
    path = remove_domain_from_uri(uri)
    path = add_fragment_back_to_path(uri, path)

    path
  end
end
parse_uri(location) click to toggle source
# File lib/devise/controllers/store_location.rb, line 45
def parse_uri(location)
  location && URI.parse(location)
rescue URI::InvalidURIError
  nil
end
remove_domain_from_uri(uri) click to toggle source
# File lib/devise/controllers/store_location.rb, line 67
def remove_domain_from_uri(uri)
  [uri.path.sub(/\A\/+/, '/'), uri.query].compact.join('?')
end
stored_location_key_for(resource_or_scope) click to toggle source
# File lib/devise/controllers/store_location.rb, line 51
def stored_location_key_for(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  "#{scope}_return_to"
end