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

83 lines
2.3 KiB
Ruby
Raw Normal View History

2009-10-12 07:37:42 -04:00
module Devise
2009-10-17 11:10:15 -04:00
# Maps controller names to devise modules
2009-10-12 07:37:42 -04:00
CONTROLLERS = {
:sessions => :authenticable,
:passwords => :recoverable,
:confirmations => :confirmable
}.freeze
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. Please refer to devise_for in routes for more info.
2009-10-12 07:37:42 -04:00
class Mapping
attr_reader :name, :as, :path_names
2009-10-12 07:37:42 -04:00
def initialize(name, options)
2009-10-12 20:49:51 -04:00
@as = (options[:as] || name).to_sym
@klass = (options[:class_name] || name.to_s.classify).to_s
@name = (options[:singular] || name.to_s.singularize).to_sym
@path_names = options[:path_names] || {}
2009-10-17 11:10:15 -04:00
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)
self.for.include?(CONTROLLERS[controller.to_sym])
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-12 20:06:39 -04:00
CONTROLLERS.values.each do |m|
class_eval <<-METHOD, __FILE__, __LINE__
def #{m}?
2009-10-12 20:49:51 -04:00
self.for.include?(:#{m})
2009-10-12 20:06:39 -04:00
end
METHOD
end
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
[:sign_in, :sign_out, :password, :confirmation].each do |path_name|
@path_names[path_name] ||= path_name.to_s
end
end
2009-10-12 07:37:42 -04:00
end
mattr_accessor :mappings
self.mappings = {}
2009-10-17 11:10:15 -04:00
# Loop through all mappings looking for a map that matches with the requested
# path (ie /users/sign_in). The important part here is the key :users. If no
# map is found just returns nil.
2009-10-12 07:37:42 -04:00
def self.find_mapping_by_path(path)
route = path.split("/")[1]
return nil unless route
route = route.to_sym
mappings.each do |key, map|
return map if map.as == route.to_sym
end
nil
end
end