2017-12-21 12:36:29 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-10-09 09:46:24 -04:00
|
|
|
module Devise
|
2009-10-30 19:51:50 -04:00
|
|
|
module Models
|
2012-02-17 11:37:44 -05:00
|
|
|
class MissingAttribute < StandardError
|
2012-02-22 10:48:41 -05: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 11:37:44 -05:00
|
|
|
end
|
|
|
|
|
2009-10-30 19:51:50 -04:00
|
|
|
# Creates configuration values for Devise and for the given module.
|
|
|
|
#
|
2017-01-19 07:03:10 -05:00
|
|
|
# Devise::Models.config(Devise::Models::DatabaseAuthenticatable, :stretches)
|
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:
|
2012-05-15 14:05:21 -04:00
|
|
|
class << mod; attr_accessor :available_configs; end
|
2011-03-30 08:33:56 -04:00
|
|
|
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
|
|
|
|
|
2012-02-17 11:37:44 -05:00
|
|
|
def self.check_fields!(klass)
|
2012-02-21 07:31:38 -05:00
|
|
|
failed_attributes = []
|
2012-05-15 14:06:14 -04:00
|
|
|
instance = klass.new
|
2012-02-21 07:31:38 -05:00
|
|
|
|
2012-02-17 11:37:44 -05:00
|
|
|
klass.devise_modules.each do |mod|
|
2012-05-15 14:18:53 -04:00
|
|
|
constant = const_get(mod.to_s.classify)
|
|
|
|
|
2013-08-05 05:24:04 -04:00
|
|
|
constant.required_fields(klass).each do |field|
|
|
|
|
failed_attributes << field unless instance.respond_to?(field)
|
2012-02-17 11:37:44 -05:00
|
|
|
end
|
|
|
|
end
|
2012-02-21 07:31:38 -05:00
|
|
|
|
|
|
|
if failed_attributes.any?
|
2012-02-22 10:48:41 -05:00
|
|
|
fail Devise::Models::MissingAttribute.new(failed_attributes)
|
2012-02-21 07:31:38 -05:00
|
|
|
end
|
2012-02-17 11:37:44 -05: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)
|
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-12-20 16:52:26 -05:00
|
|
|
include Devise::Models::Authenticatable
|
2012-05-07 15:21:35 -04:00
|
|
|
|
2013-08-05 12:56:07 -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|
|
2012-02-17 11:53:16 -05:00
|
|
|
next unless options.key?(config)
|
2011-03-30 08:33:56 -04:00
|
|
|
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
|
|
|
|
|
2012-05-06 07:13:53 -04:00
|
|
|
# The hook which is called inside devise.
|
|
|
|
# So your ORM can include devise compatibility stuff.
|
2010-02-19 03:26:17 -05:00
|
|
|
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
|
|
|
|
2012-03-03 12:30:53 -05:00
|
|
|
require 'devise/models/authenticatable'
|