adds validation for allowed age

This commit is contained in:
Juanjo Bazán
2015-09-16 18:00:49 +02:00
parent 46612bd01e
commit 1f42fba8ec
4 changed files with 17 additions and 2 deletions

View File

@@ -12,8 +12,9 @@ class Verification::Residence
validates :postal_code, length: { is: 5 }
validate :residence_in_madrid
validate :allowed_age
validate :document_number_uniqueness
validate :residence_in_madrid
def initialize(attrs={})
self.date_of_birth = parse_date('date_of_birth', attrs)
@@ -47,6 +48,11 @@ class Verification::Residence
self.date_of_birth = string_to_date(date_of_birth)
end
def allowed_age
return if errors[:date_of_birth].any?
errors.add(:date_of_birth, I18n.t('verification.residence.new.error_not_allowed_age')) unless self.date_of_birth <= 16.years.ago
end
def store_failed_attempt
FailedCensusCall.create({
user: user,

View File

@@ -22,6 +22,7 @@ en:
verify_residence: "Verify residence"
form_errors: "prevented your residence verification"
error_verifying_census: "The census of the city of Madrid could not verify your information. Pero revise de information and try again or get in touch with us."
error_not_allowed_age: "You need yo be at least 16 years old"
create:
flash:
success: "Residence verified"

View File

@@ -22,6 +22,7 @@ es:
verify_residence: "Verificar residencia"
form_errors: "evitaron verificar tu residencia"
error_verifying_census: "El Padrón de Madrid no pudo verificar tu información. Revisa la información ó ponte en contacto con nosotros."
error_not_allowed_age: "Hay que tener al menos 16 años"
create:
flash:
success: "Residencia verificada"

View File

@@ -18,11 +18,17 @@ describe Verification::Residence do
it "should not be valid without a date of birth" do
residence = Verification::Residence.new({"date_of_birth(3i)"=>"", "date_of_birth(2i)"=>"", "date_of_birth(1i)"=>""})
residence.valid?
expect(residence).to_not be_valid
expect(residence.errors[:date_of_birth]).to include("can't be blank")
end
end
it "should validate user has allowed age" do
residence = Verification::Residence.new({"date_of_birth(3i)"=>"1", "date_of_birth(2i)"=>"1", "date_of_birth(1i)"=>"#{5.year.ago.year}"})
expect(residence).to_not be_valid
expect(residence.errors[:date_of_birth]).to include("You need yo be at least 16 years old")
end
it "should validate uniquness of document_number" do
user = create(:user)
residence.user = user
@@ -38,6 +44,7 @@ describe Verification::Residence do
residence.terms_of_service = nil
expect(residence).to_not be_valid
end
end
describe "new" do