Ruby on rails 重构代码以减少分配分支条件的大小
我正在开发一个Rails web应用程序。但是当我运行rubocop检查代码时。它说下面方法的ABC(分配分支条件)规模太大。虽然我是RubyonRails的新手,但有人能给我一些重构这段代码的建议吗?关于更多细节,我正在实施第三方身份验证,允许用户通过facebook或google等登录 多谢各位Ruby on rails 重构代码以减少分配分支条件的大小,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我正在开发一个Rails web应用程序。但是当我运行rubocop检查代码时。它说下面方法的ABC(分配分支条件)规模太大。虽然我是RubyonRails的新手,但有人能给我一些重构这段代码的建议吗?关于更多细节,我正在实施第三方身份验证,允许用户通过facebook或google等登录 多谢各位 def self.from_omniauth auth, current_user identity = Identity.find_by(provider: auth.provider
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
你可以试试。但是,如果确实需要复杂性,您可以设置一条注释来忽略cop#rubocop:disable ABC(Assignment Branch Condition)
,或者不管cop的实际名称是什么。如果您觉得设置的尺寸太小,也可以配置ABC尺寸
你可以试试。但是,如果确实需要复杂性,您可以设置一条注释来忽略cop
#rubocop:disable ABC(Assignment Branch Condition)
,或者不管cop的实际名称是什么。此外,如果您觉得设置的大小太小,您可以配置ABC大小我没有收到您所说的任何错误,因此您可能应该尝试#rubocp:disable ABC
当我保存这个时,它在参数中添加了括号
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
我没有像你说的那样得到任何错误,所以你可能应该试试#rubocop:disable ABC 当我保存这个时,它在参数中添加了括号
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
谢谢你的评论,我已经按照上面的评论对代码进行了重构。谢谢你的评论,我已经按照上面的评论对代码进行了重构。这非常有效,一个小小的修正就是create_user method need self。从_omniauth调用。谢谢,我已经修好了。这很好用,一个小小的修正就是创建用户需要的自我。从_omniauth调用。谢谢,我已经修好了。