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.modules #=> [:authenticatable]
# is the modules included in the class
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
:nodoc:.
-
#controllers ⇒ Object
readonly
:nodoc:.
-
#format ⇒ Object
readonly
:nodoc:.
-
#path ⇒ Object
readonly
:nodoc:.
-
#path_names ⇒ Object
readonly
:nodoc:.
-
#scoped_path ⇒ Object
readonly
:nodoc:.
-
#sign_out_via ⇒ Object
readonly
:nodoc:.
-
#singular ⇒ Object
(also: #name)
readonly
:nodoc:.
-
#used_helpers ⇒ Object
readonly
:nodoc:.
-
#used_routes ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.add_module(m) ⇒ Object
Create magic predicates for verifying what module is activated by this map.
- .find_by_path!(path, path_type = :fullpath) ⇒ Object
-
.find_scope!(duck) ⇒ Object
Receives an object and find a scope for it.
Instance Method Summary collapse
- #authenticatable? ⇒ Boolean
- #constraints ⇒ Object
- #defaults ⇒ Object
- #fullpath ⇒ Object
-
#initialize(name, options) ⇒ Mapping
constructor
:nodoc:.
-
#modules ⇒ Object
Return modules for the mapping.
- #no_input_strategies ⇒ Object
- #routes ⇒ Object
- #strategies ⇒ Object
-
#to ⇒ Object
Gives the class the mapping points to.
Constructor Details
#initialize(name, options) ⇒ Mapping
:nodoc:
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/devise/mapping.rb', line 49 def initialize(name, ) #:nodoc: @scoped_path = [:as] ? "#{[:as]}/#{name}" : name.to_s @singular = ([:singular] || @scoped_path.tr('/', '_').singularize).to_sym @class_name = ([:class_name] || name.to_s.classify).to_s @ref = Devise.ref(@class_name) @path = ([:path] || name).to_s @path_prefix = [:path_prefix] mod = [:module] || "devise" @controllers = Hash.new { |h,k| h[k] = "#{mod}/#{k}" } @controllers.merge!([:controllers] || {}) @controllers.each { |k,v| @controllers[k] = v.to_s } @path_names = Hash.new { |h,k| h[k] = k.to_s } @path_names.merge!(:registration => "") @path_names.merge!([:path_names] || {}) @constraints = Hash.new { |h,k| h[k] = k.to_s } @constraints.merge!([:constraints] || {}) @defaults = Hash.new { |h,k| h[k] = k.to_s } @defaults.merge!([:defaults] || {}) @sign_out_via = [:sign_out_via] || Devise.sign_out_via @format = [:format] singularizer = lambda { |s| s.to_s.singularize.to_sym } if .has_key?(:only) @used_routes = self.routes & Array([:only]).map(&singularizer) elsif [:skip] == :all @used_routes = [] else @used_routes = self.routes - Array([:skip]).map(&singularizer) end if [:skip_helpers] == true @used_helpers = @used_routes elsif skip = [:skip_helpers] @used_helpers = self.routes - Array(skip).map(&singularizer) else @used_helpers = self.routes end end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def class_name @class_name end |
#controllers ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def controllers @controllers end |
#format ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def format @format end |
#path ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def path @path end |
#path_names ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def path_names @path_names end |
#scoped_path ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def scoped_path @scoped_path end |
#sign_out_via ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def sign_out_via @sign_out_via end |
#singular ⇒ Object (readonly) Also known as: name
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def singular @singular end |
#used_helpers ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def used_helpers @used_helpers end |
#used_routes ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/devise/mapping.rb', line 25 def used_routes @used_routes end |
Class Method Details
.add_module(m) ⇒ Object
Create magic predicates for verifying what module is activated by this map. Example:
def confirmable?
self.modules.include?(:confirmable)
end
141 142 143 144 145 146 147 |
# File 'lib/devise/mapping.rb', line 141 def self.add_module(m) class_eval <<-METHOD, __FILE__, __LINE__ + 1 def #{m}? self.modules.include?(:#{m}) end METHOD end |
.find_by_path!(path, path_type = :fullpath) ⇒ Object
44 45 46 47 |
# File 'lib/devise/mapping.rb', line 44 def self.find_by_path!(path, path_type=:fullpath) Devise.mappings.each_value { |m| return m if path.include?(m.send(path_type)) } raise "Could not find a valid mapping for path #{path.inspect}" 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.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/devise/mapping.rb', line 31 def self.find_scope!(duck) case duck when String, Symbol return duck when Class Devise.mappings.each_value { |m| return m.name if duck <= m.to } else Devise.mappings.each_value { |m| return m.name if duck.is_a?(m.to) } end raise "Could not find a valid mapping for #{duck.inspect}" end |
Instance Method Details
#authenticatable? ⇒ Boolean
118 119 120 |
# File 'lib/devise/mapping.rb', line 118 def authenticatable? @authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ } end |
#constraints ⇒ Object
126 127 128 |
# File 'lib/devise/mapping.rb', line 126 def constraints @constraints end |
#defaults ⇒ Object
130 131 132 |
# File 'lib/devise/mapping.rb', line 130 def defaults @defaults end |
#fullpath ⇒ Object
122 123 124 |
# File 'lib/devise/mapping.rb', line 122 def fullpath "/#{@path_prefix}/#{@path}".squeeze("/") end |
#modules ⇒ Object
Return modules for the mapping.
97 98 99 |
# File 'lib/devise/mapping.rb', line 97 def modules @modules ||= to.respond_to?(:devise_modules) ? to.devise_modules : [] end |
#no_input_strategies ⇒ Object
110 111 112 |
# File 'lib/devise/mapping.rb', line 110 def no_input_strategies self.strategies & Devise::NO_INPUT end |
#routes ⇒ Object
114 115 116 |
# File 'lib/devise/mapping.rb', line 114 def routes @routes ||= ROUTES.values_at(*self.modules).compact.uniq end |
#strategies ⇒ Object
106 107 108 |
# File 'lib/devise/mapping.rb', line 106 def strategies @strategies ||= STRATEGIES.values_at(*self.modules).compact.uniq.reverse end |
#to ⇒ Object
Gives the class the mapping points to.
102 103 104 |
# File 'lib/devise/mapping.rb', line 102 def to @ref.get end |