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:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/devise/mapping.rb', line 43

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

  @class_name = (options[:class_name] || name.to_s.classify).to_s
  @ref = ActiveSupport::Dependencies.ref(@class_name)

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

  mod = options[:module] || "devise"
  @controllers = Hash.new { |h,k| h[k] = "#{mod}/#{k}" }
  @controllers.merge!(options[:controllers] || {})

  @path_names = Hash.new { |h,k| h[k] = k.to_s }
  @path_names.merge!(:registration => "")
  @path_names.merge!(options[:path_names] || {})
end

Instance Attribute Details

#class_nameObject (readonly)

:nodoc:



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

def class_name
  @class_name
end

#controllersObject (readonly)

:nodoc:



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

def controllers
  @controllers
end

#pathObject (readonly)

:nodoc:



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

def path
  @path
end

#path_namesObject (readonly)

:nodoc:



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

def path_names
  @path_names
end

#pluralObject (readonly)

:nodoc:



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

def plural
  @plural
end

#singularObject (readonly) Also known as: name

:nodoc:



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

def singular
  @singular
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


95
96
97
98
99
100
101
# File 'lib/devise/mapping.rb', line 95

def self.add_module(m)
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
    def #{m}?
      self.modules.include?(:#{m})
    end
  METHOD
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.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/devise/mapping.rb', line 30

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}"
end

Instance Method Details

#authenticatable?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/devise/mapping.rb', line 80

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

#fullpathObject



84
85
86
# File 'lib/devise/mapping.rb', line 84

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

#modulesObject

Return modules for the mapping.



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

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

#routesObject



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

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

#strategiesObject



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

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

#toObject

Gives the class the mapping points to.



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

def to
  @ref.get
end