Class: Devise::Mapping
- Inherits:
-
Object
- Object
- Devise::Mapping
- 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
-
#as ⇒ Object
readonly
:nodoc:.
-
#name ⇒ Object
readonly
:nodoc:.
-
#path_names ⇒ Object
readonly
:nodoc:.
-
#path_prefix ⇒ Object
readonly
:nodoc:.
-
#route_options ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.default_url_options ⇒ Object
Default url options which can be used as prefix.
-
.find_by_class(klass) ⇒ Object
Find a mapping by a given class.
-
.find_by_path(path) ⇒ Object
Loop through all mappings looking for a map that matches with the requested path (ie /users/sign_in).
-
.find_scope!(duck) ⇒ Object
Receives an object and find a scope for it.
-
.register(*modules) ⇒ Object
Create magic predicates for verifying what module is activated by this map.
Instance Method Summary collapse
-
#allows?(controller) ⇒ Boolean
Check if the respective controller has a module in the mapping class.
-
#as_position ⇒ Object
Return in which position in the path prefix devise should find the as mapping.
-
#for ⇒ Object
Return modules for the mapping.
-
#initialize(name, options) ⇒ Mapping
constructor
:nodoc:.
-
#parsed_path ⇒ Object
Returns the parsed path taking into account the relative url root and raw path.
-
#raw_path ⇒ Object
Returns the raw path using path_prefix and as.
-
#to ⇒ Object
Reload mapped class each time when cache_classes is false.
Constructor Details
#initialize(name, options) ⇒ Mapping
:nodoc:
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/devise/mapping.rb', line 64 def initialize(name, ) #:nodoc: @as = (.delete(:as) || name).to_sym @klass = (.delete(:class_name) || name.to_s.classify).to_s @name = (.delete(:scope) || name.to_s.singularize).to_sym @path_prefix = "/#{.delete(:path_prefix)}/".squeeze("/") @route_options = || {} @path_names = Hash.new { |h,k| h[k] = k.to_s } @path_names.merge!(.delete(:path_names) || {}) end |
Instance Attribute Details
#as ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def as @as end |
#name ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def name @name end |
#path_names ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def path_names @path_names end |
#path_prefix ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def path_prefix @path_prefix end |
#route_options ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def @route_options end |
Class Method Details
.default_url_options ⇒ Object
Default url options which can be used as prefix.
60 61 62 |
# File 'lib/devise/mapping.rb', line 60 def self. {} end |
.find_by_class(klass) ⇒ Object
Find a mapping by a given class. It takes into account single table inheritance as well.
38 39 40 41 42 43 |
# File 'lib/devise/mapping.rb', line 38 def self.find_by_class(klass) Devise.mappings.each_value do |mapping| return mapping if klass <= mapping.to end nil 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 route && mapping.as == route.to_sym end nil end |
.find_scope!(duck) ⇒ Object
Receives an object and find a scope for it. If a scope cannot be found, raises an error. If a symbol is given, it’s considered to be the scope.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/devise/mapping.rb', line 47 def self.find_scope!(duck) case duck when String, Symbol duck else klass = duck.is_a?(Class) ? duck : duck.class mapping = Devise::Mapping.find_by_class(klass) raise "Could not find a valid mapping for #{duck}" unless mapping mapping.name end end |
.register(*modules) ⇒ Object
Create magic predicates for verifying what module is activated by this map. Example:
def confirmable?
self.for.include?(:confirmable)
end
120 121 122 123 124 125 126 127 128 |
# File 'lib/devise/mapping.rb', line 120 def self.register(*modules) modules.each do |m| class_eval <<-METHOD, __FILE__, __LINE__ + 1 def #{m}? self.for.include?(:#{m}) end METHOD end end |
Instance Method Details
#allows?(controller) ⇒ Boolean
Check if the respective controller has a module in the mapping class.
90 91 92 |
# File 'lib/devise/mapping.rb', line 90 def allows?(controller) (self.for & CONTROLLERS[controller.to_sym]).present? end |
#as_position ⇒ Object
Return in which position in the path prefix devise should find the as mapping.
95 96 97 |
# File 'lib/devise/mapping.rb', line 95 def as_position self.path_prefix.count("/") end |
#for ⇒ Object
Return modules for the mapping.
77 78 79 |
# File 'lib/devise/mapping.rb', line 77 def for @for ||= to.devise_modules end |
#parsed_path ⇒ Object
Returns the parsed path taking into account the relative url root and raw path.
105 106 107 108 109 110 111 |
# File 'lib/devise/mapping.rb', line 105 def parsed_path returning (ActionController::Base.relative_url_root.to_s + raw_path) do |path| self.class..each do |key, value| path.gsub!(key.inspect, value.to_param) end end end |
#raw_path ⇒ Object
Returns the raw path using path_prefix and as.
100 101 102 |
# File 'lib/devise/mapping.rb', line 100 def raw_path path_prefix + as.to_s end |
#to ⇒ Object
Reload mapped class each time when cache_classes is false.
82 83 84 85 86 87 |
# File 'lib/devise/mapping.rb', line 82 def to return @to if @to klass = @klass.constantize @to = klass if Rails.configuration.cache_classes klass end |