lib/devise/multi_email/models/validatable.rb in devise-multi_email-2.0.1 vs lib/devise/multi_email/models/validatable.rb in devise-multi_email-3.0.0
- old
+ new
@@ -5,12 +5,17 @@
module EmailValidatable
extend ActiveSupport::Concern
included do
validates_presence_of :email, if: :email_required?
- validates_uniqueness_of :email, allow_blank: true, if: :email_changed?
- validates_format_of :email, with: email_regexp, allow_blank: true, if: :email_changed?
+ if Devise.activerecord51?
+ validates_uniqueness_of :email, allow_blank: true, case_sensitive: true, if: :will_save_change_to_email?
+ 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
end
def email_required?
true
end
@@ -58,15 +63,27 @@
end
private
def propagate_email_errors
- email_error_key = self.class.multi_email_association.name
- email_errors = errors.delete(email_error_key) ||
- errors.delete("#{email_error_key}.email".to_sym) || []
+ association_name = self.class.multi_email_association.name
+ email_error_key = errors.keys.detect do |key|
+ [association_name.to_s, "#{association_name}.email"].include?(key.to_s)
+ end
+ return unless email_error_key.present?
- email_errors.each do |error|
- errors.add(:email, error)
+ email_errors =
+ if errors.respond_to?(:details)
+ errors
+ .details[email_error_key]
+ .map { |e| e[:error] }
+ .zip(errors.delete(email_error_key) || [])
+ else
+ errors.delete(email_error_key)
+ end
+
+ email_errors.each do |type, message|
+ errors.add(:email, type, message: message)
end
end
module ClassMethods