2009-10-09 13:46:24 +00:00
module Devise
2009-10-30 23:51:50 +00:00
module Models
# Creates configuration values for Devise and for the given module.
#
# Devise::Models.config(Devise::Authenticable, :stretches, 10)
#
# The line above creates:
#
2009-11-03 11:35:11 +00:00
# 1) An accessor called Devise.stretches, which value is used by default;
2009-10-30 23:51:50 +00: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 05:31:13 +00:00
def self . config ( mod , * accessors ) #:nodoc:
accessors . each do | accessor |
2010-01-24 00:47:33 +00:00
mod . class_eval <<-METHOD, __FILE__, __LINE__ + 1
2009-11-15 05:31:13 +00:00
def #{accessor}
if defined? ( @ #{accessor})
@ #{accessor}
elsif superclass . respond_to? ( : #{accessor})
superclass . #{accessor}
else
Devise . #{accessor}
end
2009-10-30 23:51:50 +00:00
end
2009-11-15 05:31:13 +00:00
def #{accessor}=(value)
@ #{accessor} = value
end
METHOD
end
2009-10-30 23:51:50 +00:00
end
2010-01-13 18:45:24 +00:00
# Include the chosen devise modules in your model:
2009-10-20 13:08:40 +00:00
#
2010-03-29 14:13:19 +00:00
# devise :database_authenticatable, :confirmable, :recoverable
2009-10-20 13:08:40 +00:00
#
2010-01-24 01:40:32 +00: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 11:37:28 +00:00
#
2009-10-18 15:30:32 +00:00
def devise ( * modules )
2010-04-06 14:34:22 +00:00
include Devise :: Models :: Authenticatable
2010-02-19 08:26:17 +00:00
options = modules . extract_options!
2010-03-29 14:13:19 +00:00
if modules . delete ( :authenticatable )
ActiveSupport :: Deprecation . warn " :authenticatable as module is deprecated. Please give :database_authenticatable instead. " , caller
modules << :database_authenticatable
end
2010-04-06 14:34:22 +00:00
if modules . delete ( :activatable )
ActiveSupport :: Deprecation . warn " :activatable as module is deprecated. It's included in your model by default. " , caller
end
2010-03-29 18:52:34 +00:00
if modules . delete ( :http_authenticatable )
ActiveSupport :: Deprecation . warn " :http_authenticatable as module is deprecated and is on by default. Revert by setting :http_authenticatable => false. " , caller
end
2010-04-15 06:34:49 +00:00
self . devise_modules += Devise :: ALL & modules . map ( & :to_sym ) . uniq
2009-11-23 01:14:45 +00:00
2010-02-19 08:26:17 +00:00
devise_modules_hook! do
2010-04-15 06:34:49 +00:00
devise_modules . each { | m | include Devise :: Models . const_get ( m . to_s . classify ) }
2009-11-22 20:17:38 +00:00
options . each { | key , value | send ( :" #{ key } = " , value ) }
end
2009-10-13 00:06:39 +00:00
end
2010-02-19 08:26:17 +00: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 13:46:24 +00:00
end
2010-01-24 00:47:33 +00:00
end
2010-04-06 15:27:49 +00:00
require 'devise/models/authenticatable'