Class: Devise::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/devise/mapping.rb

Overview

Responsible for handling devise mappings and routes configuration. Each resource configured by devise_for in routes is actually creating a mapping object. You can refer to devise_for in routes for usage options.

The required value in devise_for is actually not used internally, but it’s inflected to find all other values.

map.devise_for :users
mapping = Devise.mappings[:user]

mapping.name #=> :user
# is the scope used in controllers and warden, given in the route as :singular.

mapping.as   #=> "users"
# how the mapping should be search in the path, given in the route as :as.

mapping.to   #=> User
# is the class to be loaded from routes, given in the route as :class_name.

mapping.for  #=> [:authenticable]
# is the modules included in the class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Mapping

Returns a new instance of Mapping.



27
28
29
30
31
32
33
# File 'lib/devise/mapping.rb', line 27

def initialize(name, options)
  @as    = (options[:as] || name).to_sym
  @klass = (options[:class_name] || name.to_s.classify).to_s
  @name  = (options[:singular] || name.to_s.singularize).to_sym
  @path_names = options[:path_names] || {}
  setup_path_names
end

Instance Attribute Details

#asObject (readonly)

:nodoc:



25
26
27
# File 'lib/devise/mapping.rb', line 25

def as
  @as
end

#nameObject (readonly)

:nodoc:



25
26
27
# File 'lib/devise/mapping.rb', line 25

def name
  @name
end

#path_namesObject (readonly)

:nodoc:



25
26
27
# File 'lib/devise/mapping.rb', line 25

def path_names
  @path_names
end

Instance Method Details

#allows?(controller) ⇒ Boolean

Check if the respective controller has a module in the mapping class.

Returns:

  • (Boolean)


49
50
51
# File 'lib/devise/mapping.rb', line 49

def allows?(controller)
  self.for.include?(CONTROLLERS[controller.to_sym])
end

#forObject

Return modules for the mapping.



36
37
38
# File 'lib/devise/mapping.rb', line 36

def for
  @for ||= to.devise_modules
end

#toObject

Reload mapped class each time when cache_classes is false.



41
42
43
44
45
46
# File 'lib/devise/mapping.rb', line 41

def to
  return @to if @to
  klass = @klass.constantize
  @to = klass if Rails.configuration.cache_classes
  klass
end