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.modules  #=> [: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:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/devise/mapping.rb', line 54

def initialize(name, options) #:nodoc:
  @scoped_path = options[:as] ? "#{options[:as]}/#{name}" : name.to_s
  @singular = (options[:singular] || @scoped_path.tr('/', '_').singularize).to_sym

  @class_name = (options[:class_name] || name.to_s.classify).to_s
  @klass = Devise.ref(@class_name)

  @path = (options[:path] || name).to_s
  @path_prefix = options[:path_prefix]

  @sign_out_via = options[:sign_out_via] || Devise.sign_out_via
  @format = options[:format]

  @router_name = options[:router_name]

  default_failure_app(options)
  default_controllers(options)
  default_path_names(options)
  default_used_route(options)
  default_used_helpers(options)
end

Instance Attribute Details

#class_nameObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def class_name
  @class_name
end

#controllersObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def controllers
  @controllers
end

#failure_appObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def failure_app
  @failure_app
end

#formatObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def format
  @format
end

#pathObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def path
  @path
end

#path_namesObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def path_names
  @path_names
end

#router_nameObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def router_name
  @router_name
end

#scoped_pathObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def scoped_path
  @scoped_path
end

#sign_out_viaObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def sign_out_via
  @sign_out_via
end

#singularObject (readonly) Also known as: name

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def singular
  @singular
end

#used_helpersObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

def used_helpers
  @used_helpers
end

#used_routesObject (readonly)

:nodoc:



27
28
29
# File 'lib/devise/mapping.rb', line 27

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


113
114
115
116
117
118
119
# File 'lib/devise/mapping.rb', line 113

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



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

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!(obj) ⇒ 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.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/devise/mapping.rb', line 35

def self.find_scope!(obj)
  obj = obj.devise_scope if obj.respond_to?(:devise_scope)
  case obj
  when String, Symbol
    return obj.to_sym
  when Class
    Devise.mappings.each_value { |m| return m.name if obj <= m.to }
  else
    Devise.mappings.each_value { |m| return m.name if obj.is_a?(m.to) }
  end

  raise "Could not find a valid mapping for #{obj.inspect}"
end

Instance Method Details

#authenticatable?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/devise/mapping.rb', line 98

def authenticatable?
  @authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ }
end

#fullpathObject



102
103
104
# File 'lib/devise/mapping.rb', line 102

def fullpath
  "/#{@path_prefix}/#{@path}".squeeze("/")
end

#modulesObject

Return modules for the mapping.



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

def modules
  @modules ||= to.respond_to?(:devise_modules) ? to.devise_modules : []
end

#no_input_strategiesObject



90
91
92
# File 'lib/devise/mapping.rb', line 90

def no_input_strategies
  self.strategies & Devise::NO_INPUT
end

#routesObject



94
95
96
# File 'lib/devise/mapping.rb', line 94

def routes
  @routes ||= ROUTES.values_at(*self.modules).compact.uniq
end

#strategiesObject



86
87
88
# File 'lib/devise/mapping.rb', line 86

def strategies
  @strategies ||= STRATEGIES.values_at(*self.modules).compact.uniq.reverse
end

#toObject

Gives the class the mapping points to.



82
83
84
# File 'lib/devise/mapping.rb', line 82

def to
  @klass.get
end