Sha256: 59c61dc85763c2eef5a21dec0b3f78d098ecf191526729850c9a14d44b824144

Contents?: true

Size: 1.69 KB

Versions: 22

Compression:

Stored size: 1.69 KB

Contents

require "uri"

module Devise
  module Controllers
    # Provide the ability to store a location.
    # Used to redirect back to a desired path after sign in.
    # Included by default in all controllers.
    module StoreLocation
      # 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
      #
      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

      # 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_omniauth_authorize_path(:facebook)
      #
      def store_location_for(resource_or_scope, location)
        session_key = stored_location_key_for(resource_or_scope)
        uri = parse_uri(location)
        if uri
          path = [uri.path.sub(/\A\/+/, '/'), uri.query].compact.join('?')
          path = [path, uri.fragment].compact.join('#')
          session[session_key] = path
        end
      end

      private

      def parse_uri(location)
        location && URI.parse(location)
      rescue URI::InvalidURIError
        nil
      end

      def stored_location_key_for(resource_or_scope)
        scope = Devise::Mapping.find_scope!(resource_or_scope)
        "#{scope}_return_to"
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 4 rubygems

Version Path
devise-4.2.0 lib/devise/controllers/store_location.rb
devise-3.5.10 lib/devise/controllers/store_location.rb
devise-4.0.3 lib/devise/controllers/store_location.rb
devise-4.1.1 lib/devise/controllers/store_location.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/devise-4.1.0/lib/devise/controllers/store_location.rb
devise-4.1.0 lib/devise/controllers/store_location.rb
devise-3.5.9 lib/devise/controllers/store_location.rb
devise-4.0.2 lib/devise/controllers/store_location.rb
devise-4.0.1 lib/devise/controllers/store_location.rb
devise-3.5.8 lib/devise/controllers/store_location.rb
devise-3.5.7 lib/devise/controllers/store_location.rb
devise-4.0.0 lib/devise/controllers/store_location.rb
devise-4.0.0.rc2 lib/devise/controllers/store_location.rb
devise-4.0.0.rc1 lib/devise/controllers/store_location.rb
devise-3.5.6 lib/devise/controllers/store_location.rb
devise-3.5.5 lib/devise/controllers/store_location.rb
devise-3.5.4 lib/devise/controllers/store_location.rb
devise-3.5.3 lib/devise/controllers/store_location.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/devise-3.5.2/lib/devise/controllers/store_location.rb
devise-3.5.2 lib/devise/controllers/store_location.rb