Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rails validator instead of custom one #865

Merged
merged 1 commit into from
Sep 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ module DeviseTokenAuth::Concerns::UserOmniauthCallbacks
extend ActiveSupport::Concern

included do
validates :email, presence: true, email: true, if: Proc.new { |u| u.provider == 'email' }
validates_presence_of :uid, if: Proc.new { |u| u.provider != 'email' }
validates :email, presence: true, email: true, if: :email_provider?
validates_presence_of :uid, unless: :email_provider?

# only validate unique emails among email registration users
validate :unique_email_user, on: :create
validates :email, uniqueness: { scope: :provider }, on: :create, if: :email_provider?

# keep uid in sync with email
before_save :sync_uid
Expand All @@ -15,11 +15,8 @@ module DeviseTokenAuth::Concerns::UserOmniauthCallbacks

protected

# only validate unique email among users that registered by email
def unique_email_user
if provider == 'email' && self.class.where(provider: 'email', email: email).count > 0
errors.add(:email, :taken)
end
def email_provider?
provider == 'email'
end

def sync_uid
Expand Down