1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00
heartcombo--devise/lib/devise/models.rb

122 lines
3.5 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
module Devise
module Models
2012-02-17 11:37:44 -05:00
class MissingAttribute < StandardError
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
# 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)
#
# The line above creates:
#
# 1) An accessor called Devise.stretches, which value is used by default;
#
# 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:
class << mod; attr_accessor :available_configs; end
mod.available_configs = accessors
2009-11-15 00:31:13 -05:00
accessors.each do |accessor|
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
end
2009-11-15 00:31:13 -05:00
def #{accessor}=(value)
@#{accessor} = value
end
METHOD
end
end
2012-02-17 11:37:44 -05:00
def self.check_fields!(klass)
2012-02-21 07:31:38 -05:00
failed_attributes = []
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?
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:
#
# devise :database_authenticatable, :confirmable, :recoverable
#
# 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
#
def devise(*modules)
options = modules.extract_options!.dup
selected_modules = modules.map(&:to_sym).uniq.sort_by do |s|
Devise::ALL.index(s) || -1 # follow Devise::ALL order
end
devise_modules_hook! do
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|
mod = Devise::Models.const_get(m.to_s.classify)
if mod.const_defined?("ClassMethods")
class_mod = mod.const_get("ClassMethods")
extend class_mod
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)
send(:"#{config}=", options.delete(config))
end
end
end
include mod
end
self.devise_modules |= selected_modules
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.
def devise_modules_hook!
yield
end
end
end
require 'devise/models/authenticatable'