リファクタリング


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

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