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-11-20 15:41:26 -05:00
|
|
|
attr_reader :singular, :scoped_path, :path, :controllers, :path_names, :class_name, :sign_out_via
|
2010-04-15 02:21:13 -04:00
|
|
|
alias :name :singular
|
2009-11-06 11:27:27 -05: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
|
|
|
|
2010-11-11 07:53:52 -05:00
|
|
|
raise "Could not find a valid mapping for #{duck.inspect}"
|
2009-11-15 09:19:16 -05:00
|
|
|
end
|
|
|
|
|
2010-10-29 16:43:13 -04:00
|
|
|
def self.find_by_path!(path, path_type=:fullpath)
|
|
|
|
Devise.mappings.each_value { |m| return m if path.include?(m.send(path_type)) }
|
2010-11-11 07:53:52 -05:00
|
|
|
raise "Could not find a valid mapping for path #{path.inspect}"
|
2010-10-14 19:15:17 -04:00
|
|
|
end
|
|
|
|
|
2009-11-06 17:59:36 -05:00
|
|
|
def initialize(name, options) #:nodoc:
|
2010-11-20 15:41:26 -05:00
|
|
|
@scoped_path = options[:as] ? "#{options[:as]}/#{name}" : name.to_s
|
|
|
|
@singular = (options[:singular] || @scoped_path.tr('/', '_').singularize).to_sym
|
2010-02-08 14:25:20 -05:00
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
@class_name = (options[:class_name] || name.to_s.classify).to_s
|
2011-03-24 15:25:54 -04:00
|
|
|
@ref = Devise.ref(@class_name)
|
2010-06-12 14:48:37 -04:00
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
@path = (options[:path] || name).to_s
|
|
|
|
@path_prefix = options[:path_prefix]
|
2010-06-30 06:41:44 -04:00
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
mod = options[:module] || "devise"
|
2010-07-04 05:53:12 -04:00
|
|
|
@controllers = Hash.new { |h,k| h[k] = "#{mod}/#{k}" }
|
2010-07-05 19:27:20 -04:00
|
|
|
@controllers.merge!(options[:controllers] || {})
|
2011-04-25 04:00:28 -04:00
|
|
|
@controllers.each { |k,v| @controllers[k] = v.to_s }
|
2010-03-03 04:54:39 -05:00
|
|
|
|
2010-07-05 19:27:20 -04:00
|
|
|
@path_names = Hash.new { |h,k| h[k] = k.to_s }
|
2010-05-16 06:14:02 -04:00
|
|
|
@path_names.merge!(:registration => "")
|
2010-07-05 19:27:20 -04:00
|
|
|
@path_names.merge!(options[:path_names] || {})
|
2010-08-12 17:46:00 -04:00
|
|
|
|
2010-08-23 08:05:40 -04:00
|
|
|
@sign_out_via = options[:sign_out_via] || Devise.sign_out_via
|
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
|
|
|
def to
|
2011-03-24 18:22:05 -04:00
|
|
|
if defined?(ActiveSupport::Dependencies::ClassCache)
|
|
|
|
@ref.get @class_name
|
|
|
|
else
|
|
|
|
@ref.get
|
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
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-07-05 19:27:20 -04:00
|
|
|
def authenticatable?
|
|
|
|
@authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ }
|
2009-11-06 11:27:27 -05:00
|
|
|
end
|
|
|
|
|
2010-07-04 05:53:12 -04:00
|
|
|
def fullpath
|
2010-10-14 14:04:02 -04:00
|
|
|
"/#{@path_prefix}/#{@path}".squeeze("/")
|
2010-03-29 10:13:19 -04: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?
|
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
|