Created
January 29, 2019 18:19
-
-
Save mskyle/64da1e2fa3137bd5ada85b44786fb2c1 to your computer and use it in GitHub Desktop.
Cash Validation for review
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The following is a Rails validation method on a model object | |
# Validations are pieces of code that ensure the data passed in to a model matches a defined set of rules | |
# ex. presence, uniqueness, within a certain range | |
# The validation should ensure the following constraints are met: | |
# 1. | |
# min_cash_pct: | |
# Allow null: no | |
# Value range: 0 to 100 | |
# min_cash_dollars: | |
# Allow null: no | |
# Value range: 0 to 1e+17 | |
# max_cash_pct: | |
# Allow null: no | |
# Value range: 0 to 100 | |
# max_cash_dollars: | |
# Allow null: no | |
# Value range: 0 to 1e+17 | |
# refill_cash_pct: | |
# Allow null: yes | |
# Value range: 0 to 100 | |
# refill_cash_dollars: | |
# Allow null: yes | |
# Value range: 0 to 1e+17 | |
# 2. | |
# effective min cash <= effective refill cash <= effective max cash | |
# Effective min cash is the larger of min_cash_dollars and min_cash_pct converted to a percentage of account value | |
# Effective max cash is the smaller of max_cash_dollars and max_cash_pct converted to a percentage of account value | |
# Effective refill cash is the larger of refill_cash_dollars and refill_cash_pct converted to a percentage of account value | |
# total_value can be assumed to be a method on the account model that returns the account value | |
validate do | |
validates_in_range :min_cash_pct, min: 0, max: 100 | |
validates_in_range :max_cash_pct, min: 0, max: 100 | |
validates_in_range :refill_cash_pct, min: 0, max: 100 | |
validates_numericality_of :min_cash_dollars, greater_than_or_equal_to: 0, less_than_or_equal_to: 1e+17 | |
validates_numericality_of :max_cash_dollars, greater_than_or_equal_to: 0, less_than_or_equal_to: 1e+17 | |
validates_numericality_of :refill_cash_dollars, greater_than_or_equal_to: 0, less_than_or_equal_to: 1e+17, allow_nil: true | |
min_cash_pct_converted = (!min_cash_pct.blank? ? (min_cash_pct * total_value)/100 : -1) | |
min_cash_dollars_temp = (min_cash_dollars.blank? ? -1 : min_cash_dollars) | |
max_cash_pct_converted = (!max_cash_pct.blank? ? (max_cash_pct * total_value)/100 : -1) | |
effective_min_cash = [min_cash_pct_converted, min_cash_dollars_temp].max | |
if max_cash_pct_converted < effective_min_cash | |
errors.add(:max_cash_pct, "value must be greater than or equal to Min Cash value") | |
end | |
# convert all % values to cash | |
refill_cash_pct_converted = (!refill_cash_pct.blank? ? (refill_cash_pct * total_value)/100 : -1) | |
refill_cash_dollars_temp = (refill_cash_dollars.blank? ? -1 : refill_cash_dollars) | |
max_cash_dollars_temp = (max_cash_dollars.blank? ? -1 : max_cash_dollars) | |
# pick the appropriate value | |
effective_max_cash = [max_cash_pct_converted, max_cash_dollars_temp].min | |
effective_refill_cash = [refill_cash_pct_converted, refill_cash_dollars_temp].max | |
if !max_cash_dollars.blank? && effective_max_cash < effective_min_cash | |
errors.add(:max_cash_dollars, "value must be greater than Min Cash. ") | |
elsif max_cash_pct_converted < effective_min_cash | |
errors.add(:max_cash_dollars, "value must be greater than Min Cash. ") | |
end | |
# Refill Cash Level validations ------------- | |
# 3) Refill Cash Pct can be 0 and pass | |
# 4) Refill Cash Dollars can be null or 0 and pass | |
# then validate refill vs min | |
if (!(refill_cash_dollars.blank?) && !(refill_cash_dollars == 0)) || (!(refill_cash_pct.blank?) && !(refill_cash_pct == 0)) | |
if effective_refill_cash < effective_min_cash | |
errors.add(:refill_cash_dollars, "value must be greater than or equal to Min Cash value.") | |
end | |
if (!min_cash_pct.nil? || !min_cash_dollars.nil?) && !(effective_refill_cash == 0) && effective_refill_cash < effective_min_cash | |
errors.add(:refill_cash_dollars, "value must be greater than or equal to Min Cash value.") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment