1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise/mapping.rb

120 lines
3.7 KiB
Ruby
Raw Normal View History

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
# 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
# # 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
#
class Mapping #:nodoc:
attr_reader :singular, :scoped_path, :path, :controllers, :path_names, :class_name, :sign_out_via
alias :name :singular
# 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
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
2010-11-11 07:53:52 -05:00
raise "Could not find a valid mapping for #{duck.inspect}"
end
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
def initialize(name, options) #:nodoc:
@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
@class_name = (options[:class_name] || name.to_s.classify).to_s
2011-03-24 15:25:54 -04:00
@ref = Devise.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] || {})
@controllers.each { |k,v| @controllers[k] = v.to_s }
@path_names = Hash.new { |h,k| h[k] = k.to_s }
2010-05-16 06:14:02 -04:00
@path_names.merge!(:registration => "")
@path_names.merge!(options[:path_names] || {})
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.
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
# Gives the class the mapping points to.
2009-10-12 07:37:42 -04:00
def to
if defined?(ActiveSupport::Dependencies::ClassCache)
@ref.get @class_name
else
@ref.get
end
2009-10-12 07:37:42 -04:00
end
def strategies
@strategies ||= STRATEGIES.values_at(*self.modules).compact.uniq.reverse
end
def no_input_strategies
self.strategies & Devise::NO_INPUT
end
def routes
@routes ||= ROUTES.values_at(*self.modules).compact.uniq
end
def authenticatable?
@authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ }
end
def fullpath
2010-10-14 14:04:02 -04:00
"/#{@path_prefix}/#{@path}".squeeze("/")
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.modules.include?(:confirmable)
2009-10-17 11:10:15 -04:00
# end
#
def self.add_module(m)
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