Module: Devise::Controllers::Helpers

Defined in:
lib/devise/controllers/helpers.rb

Overview

Those helpers are convenience methods added to ApplicationController.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/devise/controllers/helpers.rb', line 6

def self.included(base)
  base.class_eval do
    helper_method :warden, :signed_in?, :devise_controller?,
                  *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten

    # Use devise default_url_options. We have to declare it here to overwrite
    # default definitions.
    def default_url_options(options=nil)
      Devise::Mapping.default_url_options
    end
  end
end

Instance Method Details

#after_sign_in_path_for(resource_or_scope) ⇒ Object

The default url to be used after signing in. This is used by all Devise controllers and you can overwrite it in your ApplicationController to provide a custom hook for a custom resource.

By default, it first tries to find a resource_root_path, otherwise it uses the root path. For a user scope, you can define the default url in the following way:

map.user_root '/users', :controller => 'users' # creates user_root_path

map.resources :users do |users|
  users.root # creates user_root_path
end

If none of these are defined, root_path is used. However, if this default is not enough, you can customize it, for example:

def (resource)
  if resource.is_a?(User) && resource.can_publish?
    publisher_url
  else
    super
  end
end


118
119
120
121
122
# File 'lib/devise/controllers/helpers.rb', line 118

def (resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  home_path = :"#{scope}_root_path"
  respond_to?(home_path, true) ? send(home_path) : root_path
end

#after_sign_out_path_for(resource_or_scope) ⇒ Object

Method used by sessions controller to sign out an user. You can overwrite it in your ApplicationController to provide a custom hook for a custom scope. Notice that differently from after_sign_in_path_for this method receives a symbol with the scope, and not the resource.

By default is the root_path.



130
131
132
# File 'lib/devise/controllers/helpers.rb', line 130

def after_sign_out_path_for(resource_or_scope)
  root_path
end

#authenticate(scope) ⇒ Object

Attempts to authenticate the given scope by running authentication hooks, but does not redirect in case of failures.



35
36
37
# File 'lib/devise/controllers/helpers.rb', line 35

def authenticate(scope)
  warden.authenticate(:scope => scope)
end

#authenticate!(scope) ⇒ Object

Attempts to authenticate the given scope by running authentication hooks, redirecting in case of failures.



41
42
43
# File 'lib/devise/controllers/helpers.rb', line 41

def authenticate!(scope)
  warden.authenticate!(:scope => scope)
end

#devise_controller?Boolean

Return true if it’s a devise_controller. false to all controllers unless the controllers defined inside devise. Useful if you want to apply a before filter to all controller, except the ones in devise:

before_filter :my_filter, :unless => { |c| c.devise_controller? }

Returns:

  • (Boolean)


29
30
31
# File 'lib/devise/controllers/helpers.rb', line 29

def devise_controller?
  false
end

#sign_in(resource_or_scope, resource = nil) ⇒ Object

Sign in an user that already was authenticated. This helper is useful for logging users in after sign up.

Examples:

 :user, @user    # sign_in(scope, resource)
 @user           # sign_in(resource)


59
60
61
62
63
# File 'lib/devise/controllers/helpers.rb', line 59

def (resource_or_scope, resource=nil)
  scope      = Devise::Mapping.find_scope!(resource_or_scope)
  resource ||= resource_or_scope
  warden.set_user(resource, :scope => scope)
end

#sign_in_and_redirect(resource_or_scope, resource = nil, skip = false) ⇒ Object

Sign in an user and tries to redirect first to the stored location and then to the url specified by after_sign_in_path_for.

If just a symbol is given, consider that the user was already signed in through other means and just perform the redirection.



139
140
141
142
143
144
# File 'lib/devise/controllers/helpers.rb', line 139

def (resource_or_scope, resource=nil, skip=false)
  scope      = Devise::Mapping.find_scope!(resource_or_scope)
  resource ||= resource_or_scope
  (scope, resource) unless skip
  redirect_to stored_location_for(scope) || (resource)
end

#sign_out(resource_or_scope) ⇒ Object

Sign out a given user or scope. This helper is useful for signing out an user after deleting accounts.

Examples:

sign_out :user     # sign_out(scope)
sign_out @user     # sign_out(resource)


73
74
75
76
77
78
# File 'lib/devise/controllers/helpers.rb', line 73

def sign_out(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  warden.user(scope) # Without loading user here, before_logout hook is not called
  warden.raw_session.inspect # Without this inspect here. The session does not clear.
  warden.logout(scope)
end

#sign_out_and_redirect(resource_or_scope) ⇒ Object

Sign out an user and tries to redirect to the url specified by after_sign_out_path_for.



148
149
150
151
152
# File 'lib/devise/controllers/helpers.rb', line 148

def sign_out_and_redirect(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  sign_out(scope)
  redirect_to after_sign_out_path_for(scope)
end

#signed_in?(scope) ⇒ Boolean

Check if the given scope is signed in session, without running authentication hooks.

Returns:

  • (Boolean)


47
48
49
# File 'lib/devise/controllers/helpers.rb', line 47

def signed_in?(scope)
  warden.authenticate?(:scope => scope)
end

#stored_location_for(resource_or_scope) ⇒ Object

Returns and delete 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


87
88
89
90
# File 'lib/devise/controllers/helpers.rb', line 87

def stored_location_for(resource_or_scope)
  scope = Devise::Mapping.find_scope!(resource_or_scope)
  session.delete(:"#{scope}.return_to")
end

#wardenObject

The main accessor for the warden proxy instance



20
21
22
# File 'lib/devise/controllers/helpers.rb', line 20

def warden
  request.env['warden']
end