2009-10-09 09:46:24 -04:00
|
|
|
module Devise
|
2009-10-30 19:51:50 -04:00
|
|
|
module Models
|
|
|
|
# Creates configuration values for Devise and for the given module.
|
|
|
|
#
|
2011-01-11 13:53:17 -05:00
|
|
|
# Devise::Models.config(Devise::Authenticatable, :stretches, 10)
|
2009-10-30 19:51:50 -04:00
|
|
|
#
|
|
|
|
# The line above creates:
|
|
|
|
#
|
2009-11-03 06:35:11 -05:00
|
|
|
# 1) An accessor called Devise.stretches, which value is used by default;
|
2009-10-30 19:51:50 -04:00
|
|
|
#
|
|
|
|
# 2) Some class methods for your model Model.stretches and Model.stretches=
|
|
|
|
# which have higher priority than Devise.stretches;
|
|
|
|
#
|
|
|
|
# 3) And an instance method stretches.
|
|
|
|
#
|
|
|
|
# To add the class methods you need to have a module ClassMethods defined
|
|
|
|
# inside the given class.
|
|
|
|
#
|
2009-11-15 00:31:13 -05:00
|
|
|
def self.config(mod, *accessors) #:nodoc:
|
2011-03-30 08:33:56 -04:00
|
|
|
(class << mod; self; end).send :attr_accessor, :available_configs
|
|
|
|
mod.available_configs = accessors
|
|
|
|
|
2009-11-15 00:31:13 -05:00
|
|
|
accessors.each do |accessor|
|
2010-01-23 19:47:33 -05:00
|
|
|
mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
2009-11-15 00:31:13 -05:00
|
|
|
def #{accessor}
|
|
|
|
if defined?(@#{accessor})
|
|
|
|
@#{accessor}
|
|
|
|
elsif superclass.respond_to?(:#{accessor})
|
|
|
|
superclass.#{accessor}
|
|
|
|
else
|
|
|
|
Devise.#{accessor}
|
|
|
|
end
|
2009-10-30 19:51:50 -04:00
|
|
|
end
|
|
|
|
|
2009-11-15 00:31:13 -05:00
|
|
|
def #{accessor}=(value)
|
|
|
|
@#{accessor} = value
|
|
|
|
end
|
|
|
|
METHOD
|
|
|
|
end
|
2009-10-30 19:51:50 -04:00
|
|
|
end
|
|
|
|
|
2010-01-13 13:45:24 -05:00
|
|
|
# Include the chosen devise modules in your model:
|
2009-10-20 09:08:40 -04:00
|
|
|
#
|
2010-03-29 10:13:19 -04:00
|
|
|
# devise :database_authenticatable, :confirmable, :recoverable
|
2009-10-20 09:08:40 -04:00
|
|
|
#
|
2010-01-23 20:40:32 -05:00
|
|
|
# You can also give any of the devise configuration values in form of a hash,
|
|
|
|
# with specific values for this model. Please check your Devise initializer
|
|
|
|
# for a complete description on those values.
|
2009-10-12 07:37:28 -04:00
|
|
|
#
|
2009-10-18 11:30:32 -04:00
|
|
|
def devise(*modules)
|
2010-04-06 10:34:22 -04:00
|
|
|
include Devise::Models::Authenticatable
|
2011-03-30 08:33:56 -04:00
|
|
|
options = modules.extract_options!.dup
|
|
|
|
|
2011-04-16 07:15:31 -04:00
|
|
|
selected_modules = modules.map(&:to_sym).uniq.sort_by do |s|
|
2011-01-14 18:10:46 -05:00
|
|
|
Devise::ALL.index(s) || -1 # follow Devise::ALL order
|
2011-04-16 07:15:31 -04:00
|
|
|
end
|
2009-11-22 20:14:45 -05:00
|
|
|
|
2010-02-19 03:26:17 -05:00
|
|
|
devise_modules_hook! do
|
2011-04-16 07:15:31 -04:00
|
|
|
selected_modules.each do |m|
|
2011-03-30 08:33:56 -04:00
|
|
|
mod = Devise::Models.const_get(m.to_s.classify)
|
|
|
|
|
|
|
|
if mod.const_defined?("ClassMethods")
|
|
|
|
class_mod = mod.const_get("ClassMethods")
|
2011-04-09 16:47:06 -04:00
|
|
|
extend class_mod
|
|
|
|
|
2011-03-30 08:33:56 -04:00
|
|
|
if class_mod.respond_to?(:available_configs)
|
|
|
|
available_configs = class_mod.available_configs
|
|
|
|
available_configs.each do |config|
|
|
|
|
next unless options.key?(config)
|
|
|
|
send(:"#{config}=", options.delete(config))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-09 16:47:06 -04:00
|
|
|
|
|
|
|
include mod
|
2011-03-30 08:33:56 -04:00
|
|
|
end
|
|
|
|
|
2011-04-16 07:15:31 -04:00
|
|
|
self.devise_modules |= selected_modules
|
2009-11-22 15:17:38 -05:00
|
|
|
options.each { |key, value| send(:"#{key}=", value) }
|
|
|
|
end
|
2009-10-12 20:06:39 -04:00
|
|
|
end
|
|
|
|
|
2010-02-19 03:26:17 -05:00
|
|
|
# The hook which is called inside devise. So your ORM can include devise
|
|
|
|
# compatibility stuff.
|
|
|
|
def devise_modules_hook!
|
|
|
|
yield
|
|
|
|
end
|
2009-10-09 09:46:24 -04:00
|
|
|
end
|
2010-01-23 19:47:33 -05:00
|
|
|
end
|
2010-04-06 11:27:49 -04:00
|
|
|
|
|
|
|
require 'devise/models/authenticatable'
|