There is a constern:

module Omniauthable extend ActiveSupport::Concern included do def self.find_for_oauth(auth) authorization = Authorization.where(provider: auth.provider, uid: auth.uid.to_s).first return authorization.user if authorization if auth.info.try(:email) email = auth.info[:email] else return false end user = User.where(email: email).first if user user.create_authorization(auth) else password = Devise.friendly_token[0, 20] user = User.new(email: email, password: password, password_confirmation: password) if user.valid? user.save! user.create_authorization(auth) else return false end end user end def create_authorization(auth) authorizations.create(provider: auth.provider, uid: auth.uid) end end end 

It is necessary to split self.find_for_oauth(auth) so that the number of lines in the method is at least 15.

  • rubocop swear at you? :) - D-side
  • Yes, I swear, I increased Metrics / MethodLength in the config, but this is not a solution to the problem, I would like to divide the method somehow so that it would not be so big - Mikhail Lutsko

2 answers 2

I would rewrite it like this:

 module Omniauthable extend ActiveSupport::Concern included do def self.find_for_oauth(auth) authorization = Authorization.where(provider: auth.provider, uid: auth.uid.to_s).first if authorization authorization.user else @email = auth.info.try(:email) find_user # ищем пользователя create_user unless @user # если не находим, то создаем auth_user(auth) # пытаемся авторизовать и возвращаем либо nil либо пользователя end end def create_authorization(auth) authorizations.create(provider: auth.provider, uid: auth.uid) end private # скрываем служебные методы def self.find_user @user = User.where(email: @email).first if @email end def self.create_user password = Devise.friendly_token[0, 20] udata = { email: @email, password: password, password_confirmation: password } user = User.create(udata) # здесь происходит валидация и сохранение @user = user if user.errors.count == 0 # если после создания нет ошибок, то объявляем переменную экземпляра end def self.auth_user(auth) if @user # если в предыдущих методах был найден/создан пользователь, если @user = nil(не существует, то nil вернется из метода) @user.create_authorization(auth) @user end end end end 
  • I corrected. In this method you just need to pass the variable auth - MAXOPKA
  • aah, for sure. He is clearly called, it means not privare. I will correct now. - MAXOPKA
  • password also corrected. - MAXOPKA
  • and how to Failure/Error: authorizations.create(provider: auth.provider, uid: auth.uid) ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved with Failure/Error: authorizations.create(provider: auth.provider, uid: auth.uid) ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved Michael Lutsko
  • Try the latest version of my code. Judgment on the text of the error - the authorized user is not saved in the database - MAXOPKA

Do not abuse the cult of cargo .

If some code works without a breakdown, then let it work further.

  • It is not the answer to the question. To leave your comments or ask the author to clarify, leave a comment to the appropriate post. - From the queue of checks - Suvitruf
  • @Nakilon if you do not need to add functionality to it, then it is. - MAXOPKA
  • @MAXOPKA, while adding functionality, with a bunch of methods, songs will start with passing arguments and scopes. - Nakilon
  • @Suvitruf: I think that’s quite the answer. Just a shift of focus from task to goal. - Nick Volynkin