以下のコードをどのように DRY できますか? たくさんの ELSE をセットアップする必要がありますか? 私は通常、ネストされたifの束ではなく、「これが満たされた場合は停止する」、「これが満たされた場合は停止する」を見つけます。
redirect_to と render がアクションの実行を停止しないことを発見しました...
def payment_confirmed
confirm_payment do |confirmation|
@purchase = Purchase.find(confirmation.order_id)
unless @purchase.products_match_order_products?(confirmation.products)
# TODO notify the buyer of problems
return
end
if confirmation.status == :completed
@purchase.paid!
# TODO notify the user of completed purchase
redirect_to purchase_path(@purchase)
else
# TODO notify the user somehow that thigns are pending
end
return
end
unless session[:last_purchase_id]
flash[:notice] = 'Unable to identify purchase from session data.'
redirect_to user_path(current_user)
return
end
@purchase = Purchase.find(session[:last_purchase_id])
if @purchase.paid?
redirect_to purchase_path(@purchase)
return
end
# going to show message about pending payment
end