Module: Devise::Models
- Defined in:
- lib/devise/models.rb,
lib/devise/models/confirmable.rb,
lib/devise/models/recoverable.rb,
lib/devise/models/validatable.rb,
lib/devise/models/rememberable.rb,
lib/devise/models/authenticatable.rb
Defined Under Namespace
Modules: Authenticatable, Confirmable, Recoverable, Rememberable, Validatable
Class Method Summary collapse
-
.config(mod, accessor, default = nil) ⇒ Object
Creates configuration values for Devise and for the given module.
Instance Method Summary collapse
-
#devise(*modules) ⇒ Object
Shortcut method for including all devise modules inside your model.
-
#devise_modules ⇒ Object
Stores all modules included inside the model, so we are able to verify which routes are needed.
Class Method Details
.config(mod, accessor, default = nil) ⇒ Object
Creates configuration values for Devise and for the given module.
Devise::Models.config(Devise::Authenticable, :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.
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 |
# File 'lib/devise/models.rb', line 19 def self.config(mod, accessor, default=nil) #:nodoc: Devise.send :"#{accessor}=", default mod.class_eval <<-METHOD, __FILE__, __LINE__ def #{accessor} self.class.#{accessor} end METHOD mod.const_get(:ClassMethods).class_eval <<-METHOD, __FILE__, __LINE__ 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 |
Instance Method Details
#devise(*modules) ⇒ Object
Shortcut method for including all devise modules inside your model. You can give some extra options while declaring devise in your model:
-
except: convenient option that allows you to add all devise modules, removing only the modules you setup here:
devise :all, :except => :rememberable
You can also give the following configuration values in a hash: :pepper, :stretches, :confirm_within and :remember_for. Please check your Devise initialiazer for a complete description on those values.
Examples:
# include only authenticatable module (default)
devise
# include authenticatable + confirmable modules
devise :confirmable
# include authenticatable + recoverable modules
devise :recoverable
# include authenticatable + rememberable modules
devise :rememberable
# include authenticatable + validatable modules
devise :validatable
# include authenticatable + confirmable + recoverable + rememberable + validatable
devise :confirmable, :recoverable, :rememberable, :validatable
# shortcut to include all modules (same as above)
devise :all
# include all except recoverable
devise :all, :except => :recoverable
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/devise/models.rb', line 83 def devise(*modules) = modules. modules = Devise::ALL if modules.include?(:all) modules -= Array(.delete(:except)) modules = [:authenticatable] | modules modules.each do |m| devise_modules << m.to_sym include Devise::Models.const_get(m.to_s.classify) end # Convert new keys to methods which overwrites Devise defaults .each { |key, value| send(:"#{key}=", value) } end |
#devise_modules ⇒ Object
Stores all modules included inside the model, so we are able to verify which routes are needed.
101 102 103 |
# File 'lib/devise/models.rb', line 101 def devise_modules @devise_modules ||= [] end |