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