2017-04-25 5 views
1

以下のような良い方法がありますか?Rubyの異なるif条件で異なるエラーメッセージのベースを印刷する方が良い

if promo_code.present? 
    if promo_code.active?(self) 
     if !promo_code.expired?(self) 
     if promo_code.applicable?(self) 
      current_customer.promo_transactions.create!(promo_code: promo_code, order_id: id) 
     else 
      return false 
     end 
     else 
     errors.add(:promo_code, message: 'Sorry! This code has expired') 
     return false 
     end 
    else 
     errors.add(:promo_code, message: 'Sorry! This code cannot be used at this time.') 
     return false 
    end 
    else 
    errors.add(:promo_code, message: 'Sorry! The code does not exist. Check for typos') 
    return false 
    end 

いずれかの条件が満たされない場合は、falseと特定のエラーメッセージを返す必要があります。私が持っているやり方はかわいそうだと思うし、これを行う良い方法があるのだろうか?

答えて

4

あなたはこの

​​

それとも、

errors.add(:promo_code, message: 'Sorry! The code does not exist. Check for typos') unless promo_code.present?(self) 
errors.add(:promo_code, message: 'Sorry! This code cannot be used at this time.') unless promo_code.active?(self) 
errors.add(:promo_code, message: 'Sorry! This code has expired') if promo_code.expired?(self) 
current_customer.promo_transactions.create!(promo_code: promo_code, order_id: id) if promo_code.applicable?(self) 
false 

はヤー

を選ぶ取るインライン条件を使用することができますような何かを行うことができます
関連する問題