Ошибка ветви назначения Rubocop и ошибки защитного предложения - PullRequest
0 голосов
/ 24 мая 2019

Есть ли хороший способ реорганизовать это, чтобы избежать ошибок rubocop?

Нарушения:

app/controllers/confirmations_controller.rb:6:3: C: Metrics/AbcSize: Assignment Branch Condition size for update is too high. [17.12/15]
  def update ...
  ^^^^^^^^^^
app/controllers/confirmations_controller.rb:22:5: C: Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.
    unless @confirmable.errors.empty?
    ^^^^^^
app/controllers/confirmations_controller.rb:36:5: C: Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression.
    unless @confirmable.errors.empty?
    ^^^^^^

Код:

class ConfirmationsController < Devise::ConfirmationsController
  skip_before_action :authenticate_user!

  def update
    with_unconfirmed_confirmable do
      if @confirmable.no_password?
        @confirmable.attempt_set_password(params[:user])
        if @confirmable.valid? && @confirmable.password_match?
          do_confirm
        else
          do_show
          @confirmable.errors.clear
          return
        end
      else
        @confirmable.errors.add(:email, :password_already_set)
      end
    end

    unless @confirmable.errors.empty?
      self.resource = @confirmable
      render "devise/confirmations/show"
    end
  end

  def show
    with_unconfirmed_confirmable do
      if @confirmable.no_password?
        do_show
      else
        do_confirm
      end
    end
    unless @confirmable.errors.empty?
      self.resource = @confirmable
      render "devise/confirmations/new"
    end
  end

  protected

  def with_unconfirmed_confirmable
    @confirmable = User.find_or_initialize_with_error_by(:confirmation_token, params[:confirmation_token])
    @confirmable.only_if_unconfirmed { yield } unless @confirmable.new_record?
  end

  def do_show
    @confirmation_token = params[:confirmation_token]
    @requires_password = true
    self.resource = @confirmable
    render "devise/confirmations/show"
  end

  def do_confirm
    @confirmable.confirm
    set_flash_message :notice, :confirmed
    sign_in_and_redirect(resource_name, @confirmable)
  end

  def after_confirmation_path_for(_resource_name, resource)
    token = resource.send(:set_reset_password_token)
    edit_password_url(resource, reset_password_token: token)
  end
end
...