Class: DeviseController
- Inherits:
-
Object
- Object
- DeviseController
- Includes:
- Devise::Controllers::ScopedViews
- Defined in:
- app/controllers/devise_controller.rb
Overview
All Devise controllers are inherited from here.
Direct Known Subclasses
Devise::ConfirmationsController, Devise::OmniauthCallbacksController, Devise::PasswordsController, Devise::RegistrationsController, Devise::SessionsController, Devise::UnlocksController
Class Method Summary collapse
-
.internal_methods ⇒ Object
Override internal methods to exclude ‘_prefixes` from action methods since we override it above.
Instance Method Summary collapse
-
#_prefixes ⇒ Object
Override prefixes to consider the scoped view.
-
#assert_is_devise_resource! ⇒ Object
protected
Checks whether it’s a devise mapped resource or not.
- #clean_up_passwords(object) ⇒ Object protected
- #devise_i18n_options(options) ⇒ Object protected
-
#devise_mapping ⇒ Object
protected
Attempt to find the mapped route for devise based on request path.
-
#find_message(kind, options = {}) ⇒ Object
protected
Get message for given.
-
#navigational_formats ⇒ Object
protected
Returns real navigational formats which are supported by Rails.
-
#require_no_authentication ⇒ Object
protected
Helper for use in before_actions where no authentication is required.
-
#resource ⇒ Object
protected
Gets the actual resource stored in the instance variable.
-
#resource=(new_resource) ⇒ Object
protected
Sets the resource creating an instance variable.
-
#resource_class ⇒ Object
protected
Proxy to devise map class.
-
#resource_name ⇒ Object
(also: #scope_name)
protected
Proxy to devise map name.
- #resource_params ⇒ Object protected
- #respond_with_navigational(*args, &block) ⇒ Object protected
-
#set_flash_message(key, kind, options = {}) ⇒ Object
protected
Sets the flash message with :key, using I18n.
-
#set_flash_message!(key, kind, options = {}) ⇒ Object
protected
Sets flash message if is_flashing_format? equals true.
-
#set_minimum_password_length ⇒ Object
protected
Sets minimum password length to show to user.
-
#signed_in_resource ⇒ Object
protected
Returns a signed in resource from session (if one exists).
-
#successfully_sent?(resource) ⇒ Boolean
protected
Helper for use after calling send_*_instructions methods on a resource.
-
#translation_scope ⇒ Object
protected
Controllers inheriting DeviseController are advised to override this method so that other controllers inheriting from them would use existing translations.
- #unknown_action!(msg) ⇒ Object protected
Class Method Details
.internal_methods ⇒ Object
Override internal methods to exclude ‘_prefixes` from action methods since we override it above.
There was an intentional change in Rails 7.1 that will allow it to become an action method because it’s a public method of a non-abstract controller, but we also can’t make this abstract because it can affect potential actions defined in the parent controller, so instead we ensure ‘_prefixes` is going to be considered internal. (and thus, won’t become an action method.) Ref: github.com/rails/rails/pull/48699
45 46 47 |
# File 'app/controllers/devise_controller.rb', line 45 def self.internal_methods #:nodoc: super << :_prefixes end |
Instance Method Details
#_prefixes ⇒ Object
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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/controllers/devise_controller.rb', line 78 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)
211 212 213 |
# File 'app/controllers/devise_controller.rb', line 211 def clean_up_passwords(object) object.clean_up_passwords if object.respond_to?(:clean_up_passwords) end |
#devise_i18n_options(options) ⇒ Object (protected)
191 192 193 |
# File 'app/controllers/devise_controller.rb', line 191 def () end |
#devise_mapping ⇒ Object (protected)
Attempt to find the mapped route for devise based on request path
73 74 75 |
# File 'app/controllers/devise_controller.rb', line 73 def devise_mapping @devise_mapping ||= request.env["devise.mapping"] end |
#find_message(kind, options = {}) ⇒ Object (protected)
Get message for given
196 197 198 199 200 201 202 |
# File 'app/controllers/devise_controller.rb', line 196 def (kind, = {}) [:scope] ||= translation_scope [:default] = Array([:default]).unshift(kind.to_sym) [:resource_name] = resource_name = () I18n.t("#{[:resource_name]}.#{kind}", **) end |
#navigational_formats ⇒ Object (protected)
Returns real navigational formats which are supported by Rails
98 99 100 |
# File 'app/controllers/devise_controller.rb', line 98 def @navigational_formats ||= Devise..select { |format| Mime::EXTENSION_LOOKUP[format.to_s] } end |
#require_no_authentication ⇒ Object (protected)
Helper for use in before_actions where no authentication is required.
Example:
before_action :require_no_authentication, only: :new
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/controllers/devise_controller.rb', line 116 def require_no_authentication assert_is_devise_resource! return unless 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) (:alert, 'already_authenticated', scope: 'devise.failure') redirect_to after_sign_in_path_for(resource) end end |
#resource ⇒ Object (protected)
Gets the actual resource stored in the instance variable
52 53 54 |
# File 'app/controllers/devise_controller.rb', line 52 def resource instance_variable_get(:"@#{resource_name}") end |
#resource=(new_resource) ⇒ Object (protected)
Sets the resource creating an instance variable
108 109 110 |
# File 'app/controllers/devise_controller.rb', line 108 def resource=(new_resource) instance_variable_set(:"@#{resource_name}", new_resource) end |
#resource_class ⇒ Object (protected)
Proxy to devise map class
63 64 65 |
# File 'app/controllers/devise_controller.rb', line 63 def resource_class devise_mapping.to end |
#resource_name ⇒ Object (protected) Also known as: scope_name
Proxy to devise map name
57 58 59 |
# File 'app/controllers/devise_controller.rb', line 57 def resource_name devise_mapping.name end |
#resource_params ⇒ Object (protected)
221 222 223 |
# File 'app/controllers/devise_controller.rb', line 221 def resource_params params.fetch(resource_name, {}) end |
#respond_with_navigational(*args, &block) ⇒ Object (protected)
215 216 217 218 219 |
# File 'app/controllers/devise_controller.rb', line 215 def (*args, &block) respond_with(*args) do |format| format.any(*, &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.
168 169 170 171 172 173 174 175 |
# File 'app/controllers/devise_controller.rb', line 168 def (key, kind, = {}) = (kind, ) if [:now] flash.now[key] = if .present? else flash[key] = if .present? end end |
#set_flash_message!(key, kind, options = {}) ⇒ Object (protected)
Sets flash message if is_flashing_format? equals true
178 179 180 181 182 |
# File 'app/controllers/devise_controller.rb', line 178 def (key, kind, = {}) if is_flashing_format? (key, kind, ) end end |
#set_minimum_password_length ⇒ Object (protected)
Sets minimum password length to show to user
185 186 187 188 189 |
# File 'app/controllers/devise_controller.rb', line 185 def set_minimum_password_length if devise_mapping.validatable? @minimum_password_length = resource_class.password_length.min end end |
#signed_in_resource ⇒ Object (protected)
Returns a signed in resource from session (if one exists)
68 69 70 |
# File 'app/controllers/devise_controller.rb', line 68 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.
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/controllers/devise_controller.rb', line 137 def successfully_sent?(resource) notice = if Devise.paranoid resource.errors.clear :send_paranoid_instructions elsif resource.errors.empty? :send_instructions end if notice :notice, notice true end end |
#translation_scope ⇒ Object (protected)
Controllers inheriting DeviseController are advised to override this method so that other controllers inheriting from them would use existing translations.
207 208 209 |
# File 'app/controllers/devise_controller.rb', line 207 def translation_scope "devise.#{controller_name}" end |
#unknown_action!(msg) ⇒ Object (protected)
102 103 104 105 |
# File 'app/controllers/devise_controller.rb', line 102 def unknown_action!(msg) logger.debug "[Devise] #{msg}" if logger raise AbstractController::ActionNotFound, msg end |