簡體   English   中英

我知道如何進一步重構它

[英]Any idea how I can refactor this any further

我是Rails的新手,我已經盡了最大的努力來繼續重構此方法,但這是我能做到的最小方法。 關於我可以提取或重構的任何想法? 等等

def create
 if verify_recaptcha
  super do |resource|
    if resource.save
      pin = resource.create_pin(otp: new_otp)
      begin
        SendMessage.new({pin: pin.otp, contact_number: resource.contact_number, first_name: resource.first_name}).send_otp
      rescue Exceptions::ServiceDownError
        sign_in(resource)
        return redirect_to users_otp_verification_path(user: @user.uuid, service_status: true)
      end
      sign_in(resource)
      return redirect_to users_otp_verification_path(user: @user.uuid)
    else
      return render :new, resource: resource
    end
  end
  else
     flash.now[:alert] = 'There was an error with the recaptcha verification. Please verify that you are human.'
     flash.delete :recaptcha_error
     return render :new, resource: build_resource(hash = nil)
  end
end

要開始,您可以執行以下操作:

def create
  if verify_recaptcha
    super do |resource|
      return render :new, resource: resource unless resource.save
      begin
        SendMessage.new({pin: resource.create_pin(otp: new_otp).otp, contact_number: resource.contact_number, first_name: resource.first_name}).send_otp
      rescue Exceptions::ServiceDownError
        sign_in(resource)
        return redirect_to users_otp_verification_path(user: @user.uuid, service_status: true)
      end
      sign_in(resource)
      return redirect_to users_otp_verification_path(user: @user.uuid)
    end
  else
    flash.now[:alert] = 'There was an error with the recaptcha verification. Please verify that you are human.'
    flash.delete :recaptcha_error
    return render :new, resource: build_resource(hash = nil)
  end
end

我不知道'service_status:true'在ServiceDownError中意味着什么...但是我想您可以做到:

def create
  if verify_recaptcha
    super do |resource|
      return render :new, resource: resource unless resource.save
      begin
        SendMessage.new({pin: resource.create_pin(otp: new_otp).otp, contact_number: resource.contact_number, first_name: resource.first_name}).send_otp
      ensure
        sign_in(resource)
        return redirect_to users_otp_verification_path(user: @user.uuid, service_status: true)
      end
    end
  else
    flash.now[:alert] = 'There was an error with the recaptcha verification. Please verify that you are human.'
    flash.delete :recaptcha_error
    return render :new, resource: build_resource(hash = nil)
  end
end

或者可能:

def create
  if verify_recaptcha
    super do |resource|
      return render :new, resource: resource unless resource.save
      status = nil
      begin
        SendMessage.new({pin: resource.create_pin(otp: new_otp).otp, contact_number: resource.contact_number, first_name: resource.first_name}).send_otp
      rescue Exceptions::ServiceDownError
        status = true
      ensure
        sign_in(resource)
        return redirect_to users_otp_verification_path(user: @user.uuid, status: status)
      end
    end
  else
    flash.now[:alert] = 'There was an error with the recaptcha verification. Please verify that you are human.'
    flash.delete :recaptcha_error
    return render :new, resource: build_resource(hash = nil)
  end
end

為了更進一步,您可以將其提取為單獨的方法:

{pin: resource.create_pin(otp: new_otp).otp, contact_number: resource.contact_number, first_name: resource.first_name}

喜歡:

private

def message_params(resource)
  {
    pin: resource.create_pin(otp: new_otp).otp,
    contact_number: resource.contact_number,
    first_name: resource.first_name
  }
end

請記住,重構不僅是使代碼變小,而且還使代碼更整潔和易於閱讀,因此將內容提取到較小的方法中可能是個好主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM