0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

リファクタリング

Posted at

そもそもリファクタリングとは何かに関しては下記のリンクからご確認いただいた方が腹落ちしやすいかと。

chrome-error://chromewebdata

さて今回はそんなリファクタリングに関しての私自身が実際に行った修正。

class PurehasesController < ApplicationController
  before_action :authenticate_user!, only: [:index, :create]
  before_action :set_item, only: [:index, :create]
  before_action :set_purehase, only: [:index, :create]

def index
    @order = PurehaseShipping.new
    return redirect_to root_path if current_user.id == @item.user.id || @item.purehase.present?
  end

  def create
  end

  private

#一部省略


  def set_purehase
    if @item.purehase.present?
      redirect_to root_path
    end
end
return redirect_to root_path if current_user.id == @item.user.id || 

 def set_purehase
    if @item.purehase.present?
      redirect_to root_path
    end

ここの記述の内容が重複していたので

def set_purehase
    return redirect_to root_path if current_user.id == @item.user.id ||     @item.purehase.present?
  end

上記のようにbefore_actionの私の記述でいうset_purehaseの中にリファクタリング。
問題なく動作も確認できたので同じ記述を何度もするという生産性を下げてしまう行為、
デバッグの際、見つけやすくするためにも今後も心掛けていかなければ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?