Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move all password validations to a validation method
  • Loading branch information
leesmith committed Dec 3, 2016
1 parent 1326aca commit 9fa4c09
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions app/models/user.rb
Expand Up @@ -4,12 +4,7 @@ class User < ApplicationRecord
validates :name, :email, presence: true
validates_uniqueness_of :email, case_sensitive: false
validates_format_of :email, with: /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates_presence_of :password, unless: Proc.new { |u| u.skip_password_validation? }
validates_length_of :password, minimum: 8, too_short: 'must be at least 8 characters',
unless: Proc.new { |u| u.skip_password_validation? }
validates_format_of :password, with: /\A(?=.*[a-z])(?=.*\d).+\z/i,
message: 'must be alphanumeric',
unless: Proc.new { |u| u.skip_password_validation? }
validate :password_requirements, unless: Proc.new { |u| u.skip_password_validation? }

before_save { |user| user.email.downcase! }

Expand All @@ -21,4 +16,12 @@ def skip_password_validation?
skip_password_validation
end

private

def password_requirements
validates_presence_of :password
validates_length_of :password, minimum: 8, too_short: 'must be at least 8 characters'
validates_format_of :password, with: /\A(?=.*[a-z])(?=.*\d).+\z/i, message: 'must be alphanumeric'
end

end

0 comments on commit 9fa4c09

Please sign in to comment.