Merge pull request #1476 from betagouv/fix-1475
[Fix #1475] Ensure date is parsable before doing it (to avoid errors)
This commit is contained in:
commit
eccb73f167
2 changed files with 36 additions and 1 deletions
|
@ -12,7 +12,8 @@ class Individual < ActiveRecord::Base
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_iso_date
|
def set_iso_date
|
||||||
if birthdate.present?
|
if birthdate.present? &&
|
||||||
|
birthdate =~ /\A\d{2}\/\d{2}\/\d{4}\z/
|
||||||
self.birthdate = Date.parse(birthdate).iso8601
|
self.birthdate = Date.parse(birthdate).iso8601
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,4 +6,38 @@ describe Individual do
|
||||||
it { is_expected.to have_db_column(:prenom) }
|
it { is_expected.to have_db_column(:prenom) }
|
||||||
it { is_expected.to have_db_column(:birthdate) }
|
it { is_expected.to have_db_column(:birthdate) }
|
||||||
it { is_expected.to belong_to(:dossier) }
|
it { is_expected.to belong_to(:dossier) }
|
||||||
|
|
||||||
|
describe ".save" do
|
||||||
|
let(:individual) { build(:individual) }
|
||||||
|
|
||||||
|
subject { individual.save }
|
||||||
|
|
||||||
|
context "with birthdate" do
|
||||||
|
before do
|
||||||
|
individual.birthdate = birthdate_from_user
|
||||||
|
subject
|
||||||
|
end
|
||||||
|
|
||||||
|
context "and the format is dd/mm/yyy " do
|
||||||
|
let(:birthdate_from_user) { "12/11/1980" }
|
||||||
|
|
||||||
|
it { expect(individual.valid?).to be true }
|
||||||
|
it { expect(individual.birthdate).to eq("1980-11-12") }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "and the format is ISO" do
|
||||||
|
let(:birthdate_from_user) { "1980-11-12" }
|
||||||
|
|
||||||
|
it { expect(individual.valid?).to be true }
|
||||||
|
it { expect(individual.birthdate).to eq("1980-11-12") }
|
||||||
|
end
|
||||||
|
|
||||||
|
context "and the format is WTF" do
|
||||||
|
let(:birthdate_from_user) { "1980 1 12" }
|
||||||
|
|
||||||
|
it { expect(individual.valid?).to be false }
|
||||||
|
it { expect(individual.birthdate).to eq("1980 1 12") }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue