diff --git a/app/models/user.rb b/app/models/user.rb index 394f946..64f73f4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,9 +19,11 @@ class User < ApplicationRecord enum :role, %i[contributor reviewer admin] - validates :email, presence: true, uniqueness: true + validates :email, presence: true, uniqueness: { case_sensitive: false } validates :password, length: { minimum: 12 }, if: -> { password.present? } + before_validation :normalize_email + scope :by_role, ->(role) { where(role: role) if role.present? } scope :search_email, ->(q) { where("email LIKE ?", "%#{sanitize_sql_like(q)}%") if q.present? } @@ -78,4 +80,10 @@ class User < ApplicationRecord return nil if user.nil? || user.remember_token_expired? user end + + private + + def normalize_email + self.email = email.downcase.strip if email.present? + end end