docbrown/app/models/identity.rb
Anna Buianova 4b02fe87a4 Optimize sign in: Identity #2061 (#2231)
* Add unique index to identity

* Conditional uniqueness validations on identities

* Remove redundant Identity specs
2019-03-28 16:10:23 -04:00

13 lines
515 B
Ruby

class Identity < ApplicationRecord
belongs_to :user
validates :uid, :provider, presence: true
validates :uid, uniqueness: { scope: :provider }, if: proc { |i| i.uid_changed? || i.provider_changed? }
validates :user_id, uniqueness: { scope: :provider }, if: proc { |i| i.user_id_changed? || i.provider_changed? }
validates :provider, inclusion: { in: %w[github twitter] }
serialize :auth_data_dump
def self.find_for_oauth(auth)
find_or_create_by(uid: auth.uid, provider: auth.provider)
end
end