2009-10-12 07:37:42 -04:00
|
|
|
module Devise
|
2009-10-17 11:10:15 -04:00
|
|
|
# Responsible for handling devise mappings and routes configuration. Each
|
|
|
|
# resource configured by devise_for in routes is actually creating a mapping
|
2009-10-18 10:54:21 -04:00
|
|
|
# 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]
|
|
|
|
#
|
2009-10-19 22:31:33 -04:00
|
|
|
# mapping.name #=> :user
|
2009-10-18 10:54:21 -04:00
|
|
|
# # 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.
|
|
|
|
#
|
2009-10-30 06:29:10 -04:00
|
|
|
# mapping.for #=> [:authenticatable]
|
2009-10-18 10:54:21 -04:00
|
|
|
# # is the modules included in the class
|
|
|
|
#
|
|
|
|
class Mapping #:nodoc:
|
2009-11-13 05:49:22 -05:00
|
|
|
attr_reader :name, :as, :path_names, :path_prefix, :route_options
|
2009-11-06 11:27:27 -05:00
|
|
|
|
|
|
|
# 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.
|
|
|
|
def self.find_by_path(path)
|
|
|
|
Devise.mappings.each_value do |mapping|
|
|
|
|
route = path.split("/")[mapping.as_position]
|
2010-01-06 08:31:00 -05:00
|
|
|
return mapping if route && mapping.as == route.to_sym
|
2009-11-06 11:27:27 -05:00
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-11-15 09:19:16 -05:00
|
|
|
# Find a mapping by a given class. It takes into account single table inheritance as well.
|
|
|
|
def self.find_by_class(klass)
|
2010-02-06 04:06:22 -05:00
|
|
|
Devise.mappings.each_value do |mapping|
|
|
|
|
return mapping if klass <= mapping.to
|
|
|
|
end
|
|
|
|
nil
|
2009-11-15 09:19:16 -05:00
|
|
|
end
|
|
|
|
|
2009-11-16 12:07:01 -05:00
|
|
|
# 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.
|
|
|
|
def self.find_scope!(duck)
|
2010-01-19 11:19:40 -05:00
|
|
|
case duck
|
|
|
|
when String, Symbol
|
2009-11-16 12:07:01 -05:00
|
|
|
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
|
2009-11-15 09:19:16 -05:00
|
|
|
end
|
|
|
|
|
2009-11-06 17:59:36 -05:00
|
|
|
# Default url options which can be used as prefix.
|
|
|
|
def self.default_url_options
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(name, options) #:nodoc:
|
2009-11-13 05:49:22 -05:00
|
|
|
@as = (options.delete(:as) || name).to_sym
|
|
|
|
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
|
2009-11-19 08:43:31 -05:00
|
|
|
@name = (options.delete(:scope) || name.to_s.singularize).to_sym
|
2010-01-16 08:39:05 -05:00
|
|
|
@path_names = options.delete(:path_names) || {}
|
|
|
|
@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")
|
2009-11-13 05:49:22 -05:00
|
|
|
@route_options = options || {}
|
2009-11-06 11:27:27 -05:00
|
|
|
|
2009-10-17 12:06:51 -04:00
|
|
|
setup_path_names
|
2009-10-12 20:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return modules for the mapping.
|
|
|
|
def for
|
|
|
|
@for ||= to.devise_modules
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Reload mapped class each time when cache_classes is false.
|
|
|
|
def to
|
|
|
|
return @to if @to
|
|
|
|
klass = @klass.constantize
|
|
|
|
@to = klass if Rails.configuration.cache_classes
|
|
|
|
klass
|
|
|
|
end
|
|
|
|
|
2009-10-12 20:49:51 -04:00
|
|
|
# Check if the respective controller has a module in the mapping class.
|
|
|
|
def allows?(controller)
|
2009-12-08 17:39:38 -05:00
|
|
|
(self.for & CONTROLLERS[controller.to_sym]).present?
|
2009-10-12 20:49:51 -04:00
|
|
|
end
|
|
|
|
|
2009-11-06 11:27:27 -05:00
|
|
|
# Return in which position in the path prefix devise should find the as mapping.
|
|
|
|
def as_position
|
|
|
|
self.path_prefix.count("/")
|
|
|
|
end
|
|
|
|
|
2010-01-25 14:19:47 -05:00
|
|
|
# Returns the raw path using path_prefix and as.
|
2009-11-06 11:27:27 -05:00
|
|
|
def raw_path
|
2010-01-22 06:50:32 -05:00
|
|
|
path_prefix + as.to_s
|
2009-11-06 11:27:27 -05:00
|
|
|
end
|
|
|
|
|
2010-01-25 14:19:47 -05:00
|
|
|
# Returns the parsed path taking into account the relative url root and raw path.
|
2009-11-06 11:27:27 -05:00
|
|
|
def parsed_path
|
2010-01-22 06:50:32 -05:00
|
|
|
returning (ActionController::Base.relative_url_root.to_s + raw_path) do |path|
|
2009-11-06 17:59:36 -05:00
|
|
|
self.class.default_url_options.each do |key, value|
|
2010-01-19 11:19:40 -05:00
|
|
|
path.gsub!(key.inspect, value.to_param)
|
2009-11-06 17:59:36 -05:00
|
|
|
end
|
|
|
|
end
|
2009-11-06 11:27:27 -05:00
|
|
|
end
|
|
|
|
|
2009-10-17 11:10:15 -04:00
|
|
|
# Create magic predicates for verifying what module is activated by this map.
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# def confirmable?
|
|
|
|
# self.for.include?(:confirmable)
|
|
|
|
# end
|
2009-10-18 10:54:21 -04:00
|
|
|
#
|
2010-01-21 03:15:07 -05:00
|
|
|
def self.register(*modules)
|
|
|
|
modules.each do |m|
|
2010-01-23 19:47:33 -05:00
|
|
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
2010-01-21 03:15:07 -05:00
|
|
|
def #{m}?
|
|
|
|
self.for.include?(:#{m})
|
|
|
|
end
|
|
|
|
METHOD
|
|
|
|
end
|
2009-10-12 20:06:39 -04:00
|
|
|
end
|
2010-01-21 03:15:07 -05:00
|
|
|
Devise::Mapping.register *ALL
|
2009-10-17 11:10:15 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Configure default path names, allowing the user overwrite defaults by
|
|
|
|
# passing a hash in :path_names.
|
|
|
|
def setup_path_names
|
2010-01-23 19:47:33 -05:00
|
|
|
Devise::PATH_NAMES.each do |path_name|
|
2009-10-17 11:10:15 -04:00
|
|
|
@path_names[path_name] ||= path_name.to_s
|
|
|
|
end
|
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
end
|