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.
|
|
|
|
#
|
2010-03-03 04:54:39 -05:00
|
|
|
# mapping.modules #=> [:authenticatable]
|
2009-10-18 10:54:21 -04:00
|
|
|
# # is the modules included in the class
|
|
|
|
#
|
|
|
|
class Mapping #:nodoc:
|
2010-04-15 02:21:13 -04:00
|
|
|
attr_reader :singular, :plural, :path, :controllers, :path_names, :path_prefix
|
|
|
|
alias :name :singular
|
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|
|
2010-04-15 02:21:13 -04:00
|
|
|
route = path.split("/")[mapping.segment_position]
|
|
|
|
return mapping if route && mapping.path == route.to_sym
|
2009-11-06 11:27:27 -05:00
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
|
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
|
2010-03-26 05:19:31 -04:00
|
|
|
return duck
|
|
|
|
when Class
|
|
|
|
Devise.mappings.each_value { |m| return m.name if duck <= m.to }
|
2009-11-16 12:07:01 -05:00
|
|
|
else
|
2010-03-26 05:19:31 -04:00
|
|
|
Devise.mappings.each_value { |m| return m.name if duck.is_a?(m.to) }
|
2009-11-16 12:07:01 -05:00
|
|
|
end
|
2010-03-26 05:19:31 -04:00
|
|
|
|
|
|
|
raise "Could not find a valid mapping for #{duck}"
|
2009-11-15 09:19:16 -05:00
|
|
|
end
|
|
|
|
|
2009-11-06 17:59:36 -05:00
|
|
|
def initialize(name, options) #:nodoc:
|
2010-04-15 02:21:13 -04:00
|
|
|
if as = options.delete(:as)
|
|
|
|
ActiveSupport::Deprecation.warn ":as is deprecated, please use :path instead."
|
|
|
|
options[:path] ||= as
|
|
|
|
end
|
|
|
|
|
|
|
|
if scope = options.delete(:scope)
|
|
|
|
ActiveSupport::Deprecation.warn ":scope is deprecated, please use :singular instead."
|
|
|
|
options[:singular] ||= scope
|
|
|
|
end
|
|
|
|
|
|
|
|
@plural = name.to_sym
|
|
|
|
@path = (options.delete(:path) || name).to_sym
|
|
|
|
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
|
|
|
|
@singular = (options.delete(:singular) || name.to_s.singularize).to_sym
|
2010-02-08 14:25:20 -05:00
|
|
|
|
2010-02-16 08:31:49 -05:00
|
|
|
@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")
|
2010-03-03 04:54:39 -05:00
|
|
|
|
|
|
|
@controllers = Hash.new { |h,k| h[k] = "devise/#{k}" }
|
|
|
|
@controllers.merge!(options.delete(:controllers) || {})
|
|
|
|
|
2010-02-16 08:31:49 -05:00
|
|
|
@path_names = Hash.new { |h,k| h[k] = k.to_s }
|
2010-02-08 14:25:20 -05:00
|
|
|
@path_names.merge!(options.delete(:path_names) || {})
|
2009-10-12 20:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return modules for the mapping.
|
2010-03-03 04:54:39 -05:00
|
|
|
def modules
|
2010-04-16 16:00:06 -04:00
|
|
|
@modules ||= to.respond_to?(:devise_modules) ? to.devise_modules : []
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
|
2010-03-03 04:54:39 -05:00
|
|
|
# Gives the class the mapping points to.
|
2009-10-12 07:37:42 -04:00
|
|
|
# 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
|
|
|
|
|
2010-03-28 08:51:03 -04:00
|
|
|
def strategies
|
2010-03-29 10:13:19 -04:00
|
|
|
@strategies ||= STRATEGIES.values_at(*self.modules).compact.uniq.reverse
|
|
|
|
end
|
|
|
|
|
|
|
|
def routes
|
|
|
|
@routes ||= ROUTES.values_at(*self.modules).compact.uniq
|
2010-03-28 08:51:03 -04:00
|
|
|
end
|
|
|
|
|
2010-03-03 05:06:32 -05:00
|
|
|
# Keep a list of allowed controllers for this mapping. It's useful to ensure
|
|
|
|
# that an Admin cannot access the registrations controller unless it has
|
|
|
|
# :registerable in the model.
|
|
|
|
def allowed_controllers
|
|
|
|
@allowed_controllers ||= begin
|
|
|
|
canonical = CONTROLLERS.values_at(*self.modules).compact
|
|
|
|
@controllers.values_at(*canonical)
|
|
|
|
end
|
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.
|
2010-04-15 02:21:13 -04:00
|
|
|
def segment_position
|
2009-11-06 11:27:27 -05:00
|
|
|
self.path_prefix.count("/")
|
|
|
|
end
|
|
|
|
|
2010-01-25 14:19:47 -05:00
|
|
|
# Returns the raw path using path_prefix and as.
|
2010-04-15 02:21:13 -04:00
|
|
|
def full_path
|
|
|
|
path_prefix + path.to_s
|
2009-11-06 11:27:27 -05:00
|
|
|
end
|
|
|
|
|
2010-03-29 10:13:19 -04:00
|
|
|
def authenticatable?
|
|
|
|
@authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ }
|
|
|
|
end
|
|
|
|
|
2009-10-17 11:10:15 -04:00
|
|
|
# Create magic predicates for verifying what module is activated by this map.
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# def confirmable?
|
2010-03-03 04:54:39 -05:00
|
|
|
# self.modules.include?(:confirmable)
|
2009-10-17 11:10:15 -04:00
|
|
|
# end
|
2009-10-18 10:54:21 -04:00
|
|
|
#
|
2010-03-28 08:51:03 -04:00
|
|
|
def self.add_module(m)
|
2010-03-03 05:57:23 -05:00
|
|
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
|
|
|
def #{m}?
|
|
|
|
self.modules.include?(:#{m})
|
|
|
|
end
|
|
|
|
METHOD
|
2009-10-12 20:06:39 -04:00
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
end
|