mardi 23 juin 2015

How to prevent too many if-else conditions

I have controllers that control whether user can rent a product. I have many conditions to check, and each have different outcomes. I have too many if else statements. I wonder what design pattern I can follow to make it more readable.

if current_user
  if rental_valid?
    if current_user.approved
      if eligible_to_use?(product_id)
        # redirect_to payment
      else 
        # redirect_to :back
        # alert not eligible
      end 
    else
      # redirect_to verify_path
    end 
  else
    # redirect_to :back
    # alert, rental not valid
  end 
else
  # redirect_to login_path
end

Aucun commentaire:

Enregistrer un commentaire