Class: DeviseController

Inherits:
Object
  • Object
show all
Includes:
Devise::Controllers::ScopedViews
Defined in:
app/controllers/devise_controller.rb

Overview

All Devise controllers are inherited from here.

Instance Method Summary collapse

Instance Method Details

#_prefixesObject

Override prefixes to consider the scoped view. Notice we need to check for the request due to a bug in Action Controller tests that forces _prefixes to be loaded before even having a request object.

This method should be public as it is in ActionPack itself. Changing its visibility may break other gems.



28
29
30
31
32
33
34
# File 'app/controllers/devise_controller.rb', line 28

def _prefixes #:nodoc:
  @_prefixes ||= if self.class.scoped_views? && request && devise_mapping
    ["#{devise_mapping.scoped_path}/#{controller_name}"] + super
  else
    super
  end
end

#assert_is_devise_resource!Object (protected)

Checks whether it’s a devise mapped resource or not.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/devise_controller.rb', line 65

def assert_is_devise_resource! #:nodoc:
  unknown_action! <<-MESSAGE unless devise_mapping
Could not find devise mapping for path #{request.fullpath.inspect}.
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

devise_scope :user do
  get "/some/route" => "some_devise_controller"
end

2) You are testing a Devise controller bypassing the router.
 If so, you can explicitly tell Devise which mapping to use:

 @request.env["devise.mapping"] = Devise.mappings[:user]

MESSAGE
end

#clean_up_passwords(object) ⇒ Object (protected)



198
199
200
# File 'app/controllers/devise_controller.rb', line 198

def clean_up_passwords(object)
  object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
end

#devise_i18n_options(options) ⇒ Object (protected)



178
179
180
# File 'app/controllers/devise_controller.rb', line 178

def devise_i18n_options(options)
  options
end

#devise_mappingObject (protected)

Attempt to find the mapped route for devise based on request path



60
61
62
# File 'app/controllers/devise_controller.rb', line 60

def devise_mapping
  @devise_mapping ||= request.env["devise.mapping"]
end

#find_message(kind, options = {}) ⇒ Object (protected)

Get message for given



183
184
185
186
187
188
189
# File 'app/controllers/devise_controller.rb', line 183

def find_message(kind, options = {})
  options[:scope] ||= translation_scope
  options[:default] = Array(options[:default]).unshift(kind.to_sym)
  options[:resource_name] = resource_name
  options = devise_i18n_options(options)
  I18n.t("#{options[:resource_name]}.#{kind}", **options)
end

Returns real navigational formats which are supported by Rails



85
86
87
# File 'app/controllers/devise_controller.rb', line 85

def navigational_formats
  @navigational_formats ||= Devise.navigational_formats.select { |format| Mime::EXTENSION_LOOKUP[format.to_s] }
end

#require_no_authenticationObject (protected)

Helper for use in before_actions where no authentication is required.

Example:

before_action :require_no_authentication, only: :new


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/devise_controller.rb', line 103

def require_no_authentication
  assert_is_devise_resource!
  return unless is_navigational_format?
  no_input = devise_mapping.no_input_strategies

  authenticated = if no_input.present?
    args = no_input.dup.push scope: resource_name
    warden.authenticate?(*args)
  else
    warden.authenticated?(resource_name)
  end

  if authenticated && resource = warden.user(resource_name)
    set_flash_message(:alert, 'already_authenticated', scope: 'devise.failure')
    redirect_to (resource)
  end
end

#resourceObject (protected)

Gets the actual resource stored in the instance variable



39
40
41
# File 'app/controllers/devise_controller.rb', line 39

def resource
  instance_variable_get(:"@#{resource_name}")
end

#resource=(new_resource) ⇒ Object (protected)

Sets the resource creating an instance variable



95
96
97
# File 'app/controllers/devise_controller.rb', line 95

def resource=(new_resource)
  instance_variable_set(:"@#{resource_name}", new_resource)
end

#resource_classObject (protected)

Proxy to devise map class



50
51
52
# File 'app/controllers/devise_controller.rb', line 50

def resource_class
  devise_mapping.to
end

#resource_nameObject (protected) Also known as: scope_name

Proxy to devise map name



44
45
46
# File 'app/controllers/devise_controller.rb', line 44

def resource_name
  devise_mapping.name
end

#resource_paramsObject (protected)



208
209
210
# File 'app/controllers/devise_controller.rb', line 208

def resource_params
  params.fetch(resource_name, {})
end

#respond_with_navigational(*args, &block) ⇒ Object (protected)



202
203
204
205
206
# File 'app/controllers/devise_controller.rb', line 202

def respond_with_navigational(*args, &block)
  respond_with(*args) do |format|
    format.any(*navigational_formats, &block)
  end
end

#set_flash_message(key, kind, options = {}) ⇒ Object (protected)

Sets the flash message with :key, using I18n. By default you are able to set up your messages using specific resource scope, and if no message is found we look to the default scope. Set the “now” options key to a true value to populate the flash.now hash in lieu of the default flash hash (so the flash message will be available to the current action instead of the next action). Example (i18n locale file):

en:
  devise:
    passwords:
      #default_scope_messages - only if resource_scope is not found
      user:
        #resource_scope_messages

Please refer to README or en.yml locale file to check what messages are available.



155
156
157
158
159
160
161
162
# File 'app/controllers/devise_controller.rb', line 155

def set_flash_message(key, kind, options = {})
  message = find_message(kind, options)
  if options[:now]
    flash.now[key] = message if message.present?
  else
    flash[key] = message if message.present?
  end
end

#set_flash_message!(key, kind, options = {}) ⇒ Object (protected)

Sets flash message if is_flashing_format? equals true



165
166
167
168
169
# File 'app/controllers/devise_controller.rb', line 165

def set_flash_message!(key, kind, options = {})
  if is_flashing_format?
    set_flash_message(key, kind, options)
  end
end

#set_minimum_password_lengthObject (protected)

Sets minimum password length to show to user



172
173
174
175
176
# File 'app/controllers/devise_controller.rb', line 172

def set_minimum_password_length
  if devise_mapping.validatable?
    @minimum_password_length = resource_class.password_length.min
  end
end

#signed_in_resourceObject (protected)

Returns a signed in resource from session (if one exists)



55
56
57
# File 'app/controllers/devise_controller.rb', line 55

def signed_in_resource
  warden.authenticate(scope: resource_name)
end

#successfully_sent?(resource) ⇒ Boolean (protected)

Helper for use after calling send_*_instructions methods on a resource. If we are in paranoid mode, we always act as if the resource was valid and instructions were sent.

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/devise_controller.rb', line 124

def successfully_sent?(resource)
  notice = if Devise.paranoid
    resource.errors.clear
    :send_paranoid_instructions
  elsif resource.errors.empty?
    :send_instructions
  end

  if notice
    set_flash_message! :notice, notice
    true
  end
end

#translation_scopeObject (protected)

Controllers inheriting DeviseController are advised to override this method so that other controllers inheriting from them would use existing translations.



194
195
196
# File 'app/controllers/devise_controller.rb', line 194

def translation_scope
  "devise.#{controller_name}"
end

#unknown_action!(msg) ⇒ Object (protected)

Raises:

  • (AbstractController::ActionNotFound)


89
90
91
92
# File 'app/controllers/devise_controller.rb', line 89

def unknown_action!(msg)
  logger.debug "[Devise] #{msg}" if logger
  raise AbstractController::ActionNotFound, msg
end