2009-10-09 10:46:24 -03:00
|
|
|
module Devise
|
2009-10-30 21:51:50 -02:00
|
|
|
module Models
|
2012-02-17 14:37:44 -02:00
|
|
|
class MissingAttribute < StandardError
|
2012-02-22 13:48:41 -02:00
|
|
|
def initialize(attributes)
|
|
|
|
@attributes = attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
"The following attribute(s) is (are) missing on your model: #{@attributes.join(", ")}"
|
|
|
|
end
|
2012-02-17 14:37:44 -02:00
|
|
|
end
|
|
|
|
|
2009-10-30 21:51:50 -02:00
|
|
|
# Creates configuration values for Devise and for the given module.
|
|
|
|
#
|
2011-01-12 02:53:17 +08:00
|
|
|
# Devise::Models.config(Devise::Authenticatable, :stretches, 10)
|
2009-10-30 21:51:50 -02:00
|
|
|
#
|
|
|
|
# The line above creates:
|
|
|
|
#
|
2009-11-03 09:35:11 -02:00
|
|
|
# 1) An accessor called Devise.stretches, which value is used by default;
|
2009-10-30 21:51:50 -02: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 03:31:13 -02:00
|
|
|
def self.config(mod, *accessors) #:nodoc:
|
2012-05-15 15:05:21 -03:00
|
|
|
class << mod; attr_accessor :available_configs; end
|
2011-03-30 14:33:56 +02:00
|
|
|
mod.available_configs = accessors
|
|
|
|
|
2009-11-15 03:31:13 -02:00
|
|
|
accessors.each do |accessor|
|
2010-01-23 22:47:33 -02:00
|
|
|
mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
2009-11-15 03:31:13 -02:00
|
|
|
def #{accessor}
|
|
|
|
if defined?(@#{accessor})
|
|
|
|
@#{accessor}
|
|
|
|
elsif superclass.respond_to?(:#{accessor})
|
|
|
|
superclass.#{accessor}
|
|
|
|
else
|
|
|
|
Devise.#{accessor}
|
|
|
|
end
|
2009-10-30 21:51:50 -02:00
|
|
|
end
|
|
|
|
|
2009-11-15 03:31:13 -02:00
|
|
|
def #{accessor}=(value)
|
|
|
|
@#{accessor} = value
|
|
|
|
end
|
|
|
|
METHOD
|
|
|
|
end
|
2009-10-30 21:51:50 -02:00
|
|
|
end
|
|
|
|
|
2012-02-17 14:37:44 -02:00
|
|
|
def self.check_fields!(klass)
|
2012-02-21 10:31:38 -02:00
|
|
|
failed_attributes = []
|
2012-05-15 15:06:14 -03:00
|
|
|
instance = klass.new
|
2012-02-21 10:31:38 -02:00
|
|
|
|
2012-02-17 14:37:44 -02:00
|
|
|
klass.devise_modules.each do |mod|
|
2012-05-15 15:18:53 -03:00
|
|
|
constant = const_get(mod.to_s.classify)
|
|
|
|
|
2013-08-05 11:24:04 +02:00
|
|
|
constant.required_fields(klass).each do |field|
|
|
|
|
failed_attributes << field unless instance.respond_to?(field)
|
2012-02-17 14:37:44 -02:00
|
|
|
end
|
|
|
|
end
|
2012-02-21 10:31:38 -02:00
|
|
|
|
|
|
|
if failed_attributes.any?
|
2012-02-22 13:48:41 -02:00
|
|
|
fail Devise::Models::MissingAttribute.new(failed_attributes)
|
2012-02-21 10:31:38 -02:00
|
|
|
end
|
2012-02-17 14:37:44 -02:00
|
|
|
end
|
|
|
|
|
2010-01-13 19:45:24 +01:00
|
|
|
# Include the chosen devise modules in your model:
|
2009-10-20 11:08:40 -02:00
|
|
|
#
|
2010-03-29 16:13:19 +02:00
|
|
|
# devise :database_authenticatable, :confirmable, :recoverable
|
2009-10-20 11:08:40 -02:00
|
|
|
#
|
2010-01-23 23:40:32 -02: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 08:37:28 -03:00
|
|
|
#
|
2009-10-18 13:30:32 -02:00
|
|
|
def devise(*modules)
|
2011-03-30 14:33:56 +02:00
|
|
|
options = modules.extract_options!.dup
|
|
|
|
|
2011-04-16 13:15:31 +02:00
|
|
|
selected_modules = modules.map(&:to_sym).uniq.sort_by do |s|
|
2011-01-15 07:10:46 +08:00
|
|
|
Devise::ALL.index(s) || -1 # follow Devise::ALL order
|
2011-04-16 13:15:31 +02:00
|
|
|
end
|
2009-11-22 23:14:45 -02:00
|
|
|
|
2010-02-19 09:26:17 +01:00
|
|
|
devise_modules_hook! do
|
2011-12-20 15:52:26 -06:00
|
|
|
include Devise::Models::Authenticatable
|
2012-05-07 16:21:35 -03:00
|
|
|
|
2013-08-05 18:56:07 +02:00
|
|
|
selected_modules.each do |m|
|
2011-03-30 14:33:56 +02:00
|
|
|
mod = Devise::Models.const_get(m.to_s.classify)
|
|
|
|
|
|
|
|
if mod.const_defined?("ClassMethods")
|
|
|
|
class_mod = mod.const_get("ClassMethods")
|
2011-04-09 22:47:06 +02:00
|
|
|
extend class_mod
|
|
|
|
|
2011-03-30 14:33:56 +02:00
|
|
|
if class_mod.respond_to?(:available_configs)
|
|
|
|
available_configs = class_mod.available_configs
|
|
|
|
available_configs.each do |config|
|
2012-02-17 14:53:16 -02:00
|
|
|
next unless options.key?(config)
|
2011-03-30 14:33:56 +02:00
|
|
|
send(:"#{config}=", options.delete(config))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-04-09 22:47:06 +02:00
|
|
|
|
|
|
|
include mod
|
2011-03-30 14:33:56 +02:00
|
|
|
end
|
|
|
|
|
2011-04-16 13:15:31 +02:00
|
|
|
self.devise_modules |= selected_modules
|
2009-11-22 18:17:38 -02:00
|
|
|
options.each { |key, value| send(:"#{key}=", value) }
|
|
|
|
end
|
2009-10-12 21:06:39 -03:00
|
|
|
end
|
|
|
|
|
2012-05-06 13:13:53 +02:00
|
|
|
# The hook which is called inside devise.
|
|
|
|
# So your ORM can include devise compatibility stuff.
|
2010-02-19 09:26:17 +01:00
|
|
|
def devise_modules_hook!
|
|
|
|
yield
|
|
|
|
end
|
2009-10-09 10:46:24 -03:00
|
|
|
end
|
2010-01-23 22:47:33 -02:00
|
|
|
end
|
2010-04-06 17:27:49 +02:00
|
|
|
|
2012-03-03 14:30:53 -03:00
|
|
|
require 'devise/models/authenticatable'
|