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.



49
50
51
52
53
54
55
# File 'app/controllers/devise_controller.rb', line 49

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

#assert_is_devise_resource!Object (protected)

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



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

def assert_is_devise_resource! #:nodoc:
  unknown_action! "Could not find devise mapping for path \#{request.fullpath.inspect}.\nThis may happen for two reasons:\n\n1) You forgot to wrap your route inside the scope block. For example:\n\ndevise_scope :user do\n  get \"/some/route\" => \"some_devise_controller\"\nend\n\n2) You are testing a Devise controller bypassing the router.\n If so, you can explicitly tell Devise which mapping to use:\n\n @request.env[\"devise.mapping\"] = Devise.mappings[:user]\n\n" unless devise_mapping
end

#build_resource(hash = nil, options = {}) ⇒ Object (protected)

Build a devise resource. Assignment bypasses attribute protection when :unsafe option is passed



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/devise_controller.rb', line 98

def build_resource(hash = nil, options = {})
  hash ||= resource_params || {}

  if options[:unsafe]
    self.resource = resource_class.new.tap do |resource|
      hash.each do |key, value|
        setter = :"#{key}="
        resource.send(setter, value) if resource.respond_to?(setter)
      end
    end
  else
    self.resource = resource_class.new(hash)
  end
end

#clean_up_passwords(object) ⇒ Object (protected)



180
181
182
# File 'app/controllers/devise_controller.rb', line 180

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

#devise_mappingObject

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



41
42
43
# File 'app/controllers/devise_controller.rb', line 41

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

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

Get message for given



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

def find_message(kind, options = {})
  options[:scope] = "devise.#{controller_name}"
  options[:default] = Array(options[:default]).unshift(kind.to_sym)
  options[:resource_name] = resource_name
  options = devise_i18n_options(options) if respond_to?(:devise_i18n_options, true)
  I18n.t("#{options[:resource_name]}.#{kind}", options)
end

Returns real navigational formats which are supported by Rails



82
83
84
# File 'app/controllers/devise_controller.rb', line 82

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_filters where no authentication is required.

Example:

before_filter :require_no_authentication, :only => :new


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/devise_controller.rb', line 117

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)
    flash[:alert] = I18n.t("devise.failure.already_authenticated")
    redirect_to (resource)
  end
end

#resourceObject

Gets the actual resource stored in the instance variable



16
17
18
# File 'app/controllers/devise_controller.rb', line 16

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

#resource=(new_resource) ⇒ Object (protected)

Sets the resource creating an instance variable



92
93
94
# File 'app/controllers/devise_controller.rb', line 92

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

#resource_classObject

Proxy to devise map class



27
28
29
# File 'app/controllers/devise_controller.rb', line 27

def resource_class
  devise_mapping.to
end

#resource_nameObject Also known as: scope_name

Proxy to devise map name



21
22
23
# File 'app/controllers/devise_controller.rb', line 21

def resource_name
  devise_mapping.name
end

#resource_paramsObject



31
32
33
# File 'app/controllers/devise_controller.rb', line 31

def resource_params
  params[resource_name]
end

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



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

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 setup your messages using specific resource scope, and if no one is found we look to default scope. 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.



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

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

#signed_in_resourceObject

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



36
37
38
# File 'app/controllers/devise_controller.rb', line 36

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)


138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/devise_controller.rb', line 138

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 if is_navigational_format?
    true
  end
end

#unknown_action!(msg) ⇒ Object (protected)

Raises:

  • (AbstractController::ActionNotFound)


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

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