Skip to content

Instantly share code, notes, and snippets.

@webgago
Created April 16, 2013 14:49
Show Gist options
  • Save webgago/5396531 to your computer and use it in GitHub Desktop.
Save webgago/5396531 to your computer and use it in GitHub Desktop.
Сложная валидация больших объектов
class PersonValidator
def validate(record)
record.validates_presence_of :reject_reason
end
end
class Form10Validator
def validate(record)
case record.applicant_type
when 'husband'
validate_for_husband
when 'wife'
validate_for_wife
else
default_validation
end
end
def validate_for_husband
record.husband.validates_with(PersonValidator)
end
def validate_for_wife
record.wife.validates_with(PersonValidator)
end
def default_validation
end
end
class Form9Validator
def validate(record)
record.husband.validates_with(PersonValidator)
record.wife.validates_with(PersonValidator)
end
end
s = ServiceStatementDivorce.last
s.validates_with(Form10Validator)
s.valid? #=> true
s = ServiceStatementDivorce.last
s.validates_with(Form9Validator)
s.valid? #=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment