-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.rb
120 lines (102 loc) · 3.81 KB
/
model.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true
module Devise
module Models
class MissingAttribute < StandardError
def initialize(attributes)
@attributes = attributes
end
def message
"The following attribute(s) is (are) missing on your model: #{@attributes.join(", ")}"
end
end
# Creates configuration values for Devise and for the given module.
#
# 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.
#
def self.config(mod, *accessors) #:nodoc:
class << mod; attr_accessor :available_configs; end
mod.available_configs = accessors
accessors.each do |accessor|
mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{accessor}
if defined?(@#{accessor})
@#{accessor}
elsif superclass.respond_to?(:#{accessor})
superclass.#{accessor}
else
Devise.#{accessor}
end
end
def #{accessor}=(value)
@#{accessor} = value
end
METHOD
end
end
def self.check_fields!(klass)
failed_attributes = []
instance = klass.new
klass.devise_modules.each do |mod|
constant = const_get(mod.to_s.classify)
constant.required_fields(klass).each do |field|
failed_attributes << field unless instance.respond_to?(field)
end
end
if failed_attributes.any?
fail Devise::Models::MissingAttribute.new(failed_attributes)
end
end
# 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.
#
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
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|
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
end
# 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'