Created
April 16, 2013 14:49
-
-
Save webgago/5396531 to your computer and use it in GitHub Desktop.
Сложная валидация больших объектов
This file contains hidden or 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
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