2017-12-21 17:36:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2009-09-18 15:03:41 +00:00
|
|
|
module Devise
|
2009-10-09 11:30:25 +00:00
|
|
|
module Models
|
2009-10-09 12:27:44 +00:00
|
|
|
# Validatable creates all needed validations for a user email and password.
|
|
|
|
# It's optional, given you may want to create the validations by yourself.
|
2011-08-16 20:06:13 +00:00
|
|
|
# Automatically validate if the email is present, unique and its format is
|
2010-07-15 11:01:31 +00:00
|
|
|
# valid. Also tests presence of password, confirmation and length.
|
|
|
|
#
|
|
|
|
# == Options
|
|
|
|
#
|
2021-10-06 22:20:03 +00:00
|
|
|
# Validatable adds the following options to +devise+:
|
2010-07-15 11:01:31 +00:00
|
|
|
#
|
|
|
|
# * +email_regexp+: the regular expression used to validate e-mails;
|
2017-12-19 10:34:45 +00:00
|
|
|
# * +password_length+: a range expressing password length. Defaults to 6..128.
|
2010-07-15 11:01:31 +00:00
|
|
|
#
|
2009-10-09 11:30:25 +00:00
|
|
|
module Validatable
|
2009-11-24 15:56:54 +00:00
|
|
|
# All validations used by this module.
|
2015-03-13 16:48:14 +00:00
|
|
|
VALIDATIONS = [:validates_presence_of, :validates_uniqueness_of, :validates_format_of,
|
|
|
|
:validates_confirmation_of, :validates_length_of].freeze
|
2009-11-24 15:56:54 +00:00
|
|
|
|
2012-02-23 16:50:57 +00:00
|
|
|
def self.required_fields(klass)
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2009-10-09 11:30:25 +00:00
|
|
|
def self.included(base)
|
2010-03-28 05:19:23 +00:00
|
|
|
base.extend ClassMethods
|
2009-11-24 15:56:54 +00:00
|
|
|
assert_validations_api!(base)
|
|
|
|
|
2009-10-09 11:30:25 +00:00
|
|
|
base.class_eval do
|
2014-02-25 16:42:55 +00:00
|
|
|
validates_presence_of :email, if: :email_required?
|
2017-05-10 20:09:38 +00:00
|
|
|
if Devise.activerecord51?
|
2019-04-26 11:20:30 +00:00
|
|
|
validates_uniqueness_of :email, allow_blank: true, case_sensitive: true, if: :will_save_change_to_email?
|
2017-04-28 23:05:39 +00:00
|
|
|
validates_format_of :email, with: email_regexp, allow_blank: true, if: :will_save_change_to_email?
|
|
|
|
else
|
|
|
|
validates_uniqueness_of :email, allow_blank: true, if: :email_changed?
|
|
|
|
validates_format_of :email, with: email_regexp, allow_blank: true, if: :email_changed?
|
|
|
|
end
|
2009-09-18 15:03:41 +00:00
|
|
|
|
2014-02-25 16:42:55 +00:00
|
|
|
validates_presence_of :password, if: :password_required?
|
|
|
|
validates_confirmation_of :password, if: :password_required?
|
|
|
|
validates_length_of :password, within: password_length, allow_blank: true
|
2009-10-09 11:30:25 +00:00
|
|
|
end
|
2009-09-18 15:03:41 +00:00
|
|
|
end
|
|
|
|
|
2009-11-24 15:56:54 +00:00
|
|
|
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
|
2010-03-27 11:31:38 +00:00
|
|
|
|
2010-03-28 05:19:23 +00:00
|
|
|
protected
|
2010-03-27 11:31:38 +00:00
|
|
|
|
2010-03-28 05:19:23 +00:00
|
|
|
# Checks whether a password is needed or not. For validations only.
|
|
|
|
# Passwords are always required if it's a new record, or if the password
|
|
|
|
# or confirmation are being set somewhere.
|
|
|
|
def password_required?
|
2010-03-28 20:26:07 +00:00
|
|
|
!persisted? || !password.nil? || !password_confirmation.nil?
|
2010-03-28 05:19:23 +00:00
|
|
|
end
|
|
|
|
|
2010-10-03 22:36:15 +00:00
|
|
|
def email_required?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2010-03-28 05:19:23 +00:00
|
|
|
module ClassMethods
|
|
|
|
Devise::Models.config(self, :email_regexp, :password_length)
|
|
|
|
end
|
2009-10-09 11:30:25 +00:00
|
|
|
end
|
2009-09-18 15:03:41 +00:00
|
|
|
end
|
|
|
|
end
|