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  #=> [:authenticatable]
# is the modules included in the class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Mapping

:nodoc:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/devise/mapping.rb', line 42

def initialize(name, options) #:nodoc:
  @as    = (options.delete(:as) || name).to_sym
  @klass = (options.delete(:class_name) || name.to_s.classify).to_s
  @name  = (options.delete(:singular) || name.to_s.singularize).to_sym
  @path_names  = options.delete(:path_names) || {}
  @path_prefix = options.delete(:path_prefix) || ""
  @path_prefix << "/" unless @path_prefix[-1] == ?/
  @route_options = options || {}

  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

#path_prefixObject (readonly)

:nodoc:



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

def path_prefix
  @path_prefix
end

#route_optionsObject (readonly)

:nodoc:



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

def route_options
  @route_options
end

Class Method Details

.default_url_optionsObject

Default url options which can be used as prefix.



38
39
40
# File 'lib/devise/mapping.rb', line 38

def self.default_url_options
  {}
end

.find_by_path(path) ⇒ Object

Loop through all mappings looking for a map that matches with the requested path (ie /users/sign_in). If a path prefix is given, it’s taken into account.



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

def self.find_by_path(path)
  Devise.mappings.each_value do |mapping|
    route = path.split("/")[mapping.as_position]
    return mapping if mapping.as == route.to_sym
  end
  nil
end

Instance Method Details

#allows?(controller) ⇒ Boolean

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

Returns:

  • (Boolean)


68
69
70
# File 'lib/devise/mapping.rb', line 68

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

#as_positionObject

Return in which position in the path prefix devise should find the as mapping.



73
74
75
# File 'lib/devise/mapping.rb', line 73

def as_position
  self.path_prefix.count("/")
end

#forObject

Return modules for the mapping.



55
56
57
# File 'lib/devise/mapping.rb', line 55

def for
  @for ||= to.devise_modules
end

#parsed_pathObject

Returns the parsed path. If you need meta information in your path_prefix, you should overwrite this method to use it. The only information supported by default is I18n.locale.



86
87
88
89
90
91
92
# File 'lib/devise/mapping.rb', line 86

def parsed_path
  returning raw_path do |path|
    self.class.default_url_options.each do |key, value|
      path.gsub!(key.inspect, value.to_s)
    end
  end
end

#raw_pathObject

Returns the raw path using path_prefix and as.



78
79
80
# File 'lib/devise/mapping.rb', line 78

def raw_path
  path_prefix + as.to_s
end

#toObject

Reload mapped class each time when cache_classes is false.



60
61
62
63
64
65
# File 'lib/devise/mapping.rb', line 60

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