diff --git a/app/models/user.rb b/app/models/user.rb index 6a9d60e6d..b012a2223 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,10 +24,9 @@ class User < ApplicationRecord phony_normalize :phone, as: :normalized_phone, default_country_code: "RU" - validates :phone, presence: true, if: :sms_reminders? - validates :email, presence: true, uniqueness: { case_sensitive: false } + validates :phone, presence: true, phony_plausible: { country_code: "RU" }, if: :sms_reminders? + validates :email, uniqueness: { allow_blank: true, case_sensitive: false } validates :role, presence: true - validates_plausible_phone :phone, country_code: "RU" before_save :nullify_empty_email before_save :downcase_email @@ -45,26 +44,24 @@ class User < ApplicationRecord scope :developers, -> { joins(:groups).where("groups.kind = 1") } def self.from_omniauth!(auth) - social = SocialAccount.includes(:user).find_or_initialize_by(uid: auth.uid, provider: auth.provider) + auth_info = auth.info + user = User.find_or_initialize_by(email: auth_info.email) + + if user.new_record? + user.email = auth_info.email + user.name = auth_info.name + user.first_name = auth_info.first_name + user.last_name = auth_info.last_name + user.remote_avatar_url = avatar_from_auth(auth) + end + social = SocialAccount.includes(:user).find_or_initialize_by(uid: auth.uid, provider: auth.provider) if social.new_record? social.link = SocialAccount.link_for(auth) - social.save + user.social_accounts << social end - auth_info = auth.info - user = User.find_by(email: auth_info.email) - - unless user.present? - user = social.build_user do |user| - user.email = auth_info.email - user.name = auth_info.name - user.first_name = auth_info.first_name - user.last_name = auth_info.last_name - user.remote_avatar_url = avatar_from_auth(auth) - end - user.save - end + user.save user end