简体   繁体   English

重构代码以减少分配分支条件大小

[英]Refactor the code to reduce Assignment Branch Condition size

I am developing a Rails web application.我正在开发一个 Rails Web 应用程序。 But when I run rubocop to check the code.但是当我运行 rubocop 来检查代码时。 It said that the ABC (Assignment Branch Condition) size of the method below is too high.它说下面方法的ABC(分配分支条件)大小太高。 While I'm a newbie in Ruby on Rails, can someone give me some advice to refactor this block of code?虽然我是 Ruby on Rails 的新手,但有人能给我一些建议来重构这段代码吗? For more details, I am implementing the third party authentication which allows user to sign in by facebook or google, etc.有关更多详细信息,我正在实施第三方身份验证,允许用户通过 facebook 或 google 等登录。

Thank you谢谢

  def self.from_omniauth auth, current_user
    identity = Identity.find_by(provider: auth.provider, uid: auth.id)
                       .first_or_initialize
    if identity.user.blank?
      user = current_user || User.find_by("email = ?",
                                          auth["info"]["email"])
      if user.blank?
        user = User.new
        user.password = Devise.friendly_token[0, 10]
        user.name = auth.info.name
        user.email = auth.info.email
        user.picture = auth.info.image
        return user.save(validate: false) if auth.provider == "twitter"

        user.save
      end
      identity.user_id = user.id
      identity.save
    end
    identity.user
  end

  def self.from_omniauth auth, current_user
    identity = Identity.find_by(provider: auth.provider, uid: auth.id)
                       .first_or_initialize
    if identity.user.blank?
      user = current_user || User.find_by("email = ?",
                                          auth["info"]["email"])
      create_user(auth) if user.blank?

      identity.user_id = user.id
      identity.save
    end
    identity.user
  end

  def self.create_user(auth)
    user = User.new
    user.password = Devise.friendly_token[0, 10]
    user.name = auth.info.name
    user.email = auth.info.email
    user.picture = auth.info.image
    return user.save(validate: false) if auth.provider == "twitter"

    user.save
  end

Is something you can try.是你可以尝试的东西。 But if the complexity is actually needed you can set a comment to ignore that cop # rubocop:disable ABC (Assignment Branch Condition) , or whatever the actual name of the cop is.但是,如果确实需要复杂性,您可以设置注释以忽略该 cop # rubocop:disable ABC (Assignment Branch Condition) ,或者无论 cop 的实际名称是什么。 Also you can configure the ABC size if you feel the size set is too low如果您觉得尺寸设置太小,您也可以配置 ABC 尺寸

I don't get any error like you are saying, so probably you should try # rubocop:disable ABC我没有得到您所说的任何错误,所以您可能应该尝试 # rubocop:disable ABC

when I saved this it added bracket in parameters当我保存它时,它在参数中添加了括号

def self.from_omniauth(auth, current_user)
  identity = Identity.find_by(provider: auth.provider, uid: auth.id)
                     .first_or_initialize
  if identity.user.blank?
    user = current_user || User.find_by("email = ?",
                                        auth["info"]["email"])
    if user.blank?
      user = User.new
      user.password = Devise.friendly_token[0, 10]
      user.name = auth.info.name
      user.email = auth.info.email
      user.picture = auth.info.image
      return user.save(validate: false) if auth.provider == "twitter"

      user.save
    end
    identity.user_id = user.id
    identity.save
  end
  identity.user
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM