Module: Devise::Models::Validatable

Defined in:
lib/devise/models/validatable.rb

Overview

Validatable creates all needed validations for a user email and password. It’s optional, given you may want to create the validations by yourself. Automatically validate if the email is present, unique and it’s format is valid. Also tests presence of password, confirmation and length

Constant Summary collapse

VALIDATIONS =

All validations used by this module.

[ :validates_presence_of, :validates_uniqueness_of, :validates_format_of,
:validates_confirmation_of, :validates_length_of ].freeze

Class Method Summary collapse

Class Method Details

.assert_validations_api!(base) ⇒ Object

:nodoc:



29
30
31
32
33
34
35
36
# File 'lib/devise/models/validatable.rb', line 29

def self.assert_validations_api!(base) #:nodoc:
  unavailable_validations = VALIDATIONS.select { |v| !base.respond_to?(v) }

  unless unavailable_validations.empty?
    raise "Could not use :validatable module since #{base} does not respond " <<
          "to the following methods: #{unavailable_validations.to_sentence}."
  end
end

.included(base) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/devise/models/validatable.rb', line 13

def self.included(base)
  assert_validations_api!(base)

  base.class_eval do
    validates_presence_of   :email
    validates_uniqueness_of :email, :scope => authentication_keys[1..-1], :case_sensitive => false, :allow_blank => true
    validates_format_of     :email, :with  => EMAIL_REGEX, :allow_blank => true

    with_options :if => :password_required? do |v|
      v.validates_presence_of     :password
      v.validates_confirmation_of :password
      v.validates_length_of       :password, :within => 6..20, :allow_blank => true
    end
  end
end