mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
68 lines
No EOL
2.1 KiB
Ruby
68 lines
No EOL
2.1 KiB
Ruby
module Devise
|
|
module Models
|
|
# Creates configuration values for Devise and for the given module.
|
|
#
|
|
# Devise::Models.config(Devise::Authenticatable, :stretches, 10)
|
|
#
|
|
# 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:
|
|
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
|
|
|
|
# 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)
|
|
include Devise::Models::Authenticatable
|
|
options = modules.extract_options!
|
|
self.devise_modules += modules.map(&:to_sym).uniq.sort_by { |s|
|
|
Devise::ALL.index(s) || -1 # follow Devise::ALL order
|
|
}
|
|
|
|
devise_modules_hook! do
|
|
devise_modules.each { |m| include Devise::Models.const_get(m.to_s.classify) }
|
|
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' |