リファクタリング
そもそもリファクタリングとは何かに関しては下記のリンクからご確認いただいた方が腹落ちしやすいかと。
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の中にリファクタリング。
問題なく動作も確認できたので同じ記述を何度もするという生産性を下げてしまう行為、
デバッグの際、見つけやすくするためにも今後も心掛けていかなければ。
Author And Source
この問題について(リファクタリング), 我々は、より多くの情報をここで見つけました https://qiita.com/mrry1992/items/ecc1bc0d86739d1c211a著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .