I have a custom validator that checks the min and max score of different sporting leagues -- e.g., MLB, NBA, NFL, NCAAB, etc.
Here's short version of what it looks like:
class ScoreValidator < ActiveModel::Validator
def validate(event)
if event.league_is_mlb?
if event.total_score < 1
event.errors.add :base, 'min total score for an MLB event is 1'
elsif event.total_score > 49
event.errors.add :base, 'max total score for an MLB event is 49'
end
elsif event.league_is_nba?
if event.total_score < 119
event.errors.add :base, 'min total score for an NBA event is 119'
elsif event.total_score > 370
event.errors.add :base, 'max total score for an NBA event is 370'
end
end
end
end
Seems like this code could be much cleaner by making smarter use of ruby data structures and methods. Any creative suggestions you could make would be educational and much appreciated.