我怎样才能干燥下面的代码?我必须设置一堆 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