Merge pull request #1792 from betagouv/improve_individual_date_second_part

Individual: use birthdate as a  ... date :)
This commit is contained in:
LeSim 2018-04-04 13:52:18 +02:00 committed by GitHub
commit c670c0fbea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 16 additions and 32 deletions

View file

@ -92,7 +92,9 @@ class Users::DossiersController < UsersController
individual.update_column :gender, @facade.dossier.france_connect_information.gender individual.update_column :gender, @facade.dossier.france_connect_information.gender
individual.update_column :nom, @facade.dossier.france_connect_information.family_name individual.update_column :nom, @facade.dossier.france_connect_information.family_name
individual.update_column :prenom, @facade.dossier.france_connect_information.given_name individual.update_column :prenom, @facade.dossier.france_connect_information.given_name
individual.update_column :birthdate, @facade.dossier.france_connect_information.birthdate.iso8601
individual.birthdate = @facade.dossier.france_connect_information.birthdate
individual.save
end end
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound

View file

@ -5,26 +5,12 @@ class Individual < ApplicationRecord
validates :gender, presence: true, allow_nil: false, on: :update validates :gender, presence: true, allow_nil: false, on: :update
validates :nom, presence: true, allow_blank: false, allow_nil: false, on: :update validates :nom, presence: true, allow_blank: false, allow_nil: false, on: :update
validates :prenom, presence: true, allow_blank: false, allow_nil: false, on: :update validates :prenom, presence: true, allow_blank: false, allow_nil: false, on: :update
validates :birthdate, format: { with: /\A\d{4}\-\d{2}\-\d{2}\z/, message: "La date n'est pas au format AAAA-MM-JJ" }, allow_nil: true
before_validation :set_iso_date, if: -> { birthdate_changed? } def birthdate
before_save :save_birthdate_in_datetime_format second_birthdate
private
def set_iso_date
if birthdate.present? &&
birthdate =~ /\A\d{2}\/\d{2}\/\d{4}\z/
self.birthdate = Date.parse(birthdate).iso8601
end
end end
def save_birthdate_in_datetime_format def birthdate=(date)
if birthdate.present? self.second_birthdate = date
begin
self.second_birthdate = Date.parse(birthdate)
rescue
end
end
end end
end end

View file

@ -20,7 +20,7 @@
.row .row
.col-xs-6.depositaire-label Date de naissance .col-xs-6.depositaire-label Date de naissance
.col-xs-1.comments-off= "-" .col-xs-1.comments-off= "-"
.col-xs-5.depositaire-info= @facade.individual.birthdate .col-xs-4.depositaire-info= @facade.individual.birthdate&.strftime("%d/%m/%Y")
.row.margin-top-20 .row.margin-top-20
- if @facade.champs.present? - if @facade.champs.present?

View file

@ -12,4 +12,4 @@
- if individual.birthdate.present? - if individual.birthdate.present?
%tr %tr
%th.libelle Date de naissance : %th.libelle Date de naissance :
%td= Date.parse(individual.birthdate).strftime("%d/%m/%Y") %td= individual.birthdate&.strftime("%d/%m/%Y")

View file

@ -3,6 +3,6 @@ FactoryBot.define do
gender 'M.' gender 'M.'
nom 'Julien' nom 'Julien'
prenom 'Xavier' prenom 'Xavier'
birthdate '1991-11-01' birthdate Date.new(1991, 11, 01)
end end
end end

View file

@ -30,7 +30,7 @@ feature 'As a User I wanna create a dossier' do
expect(page).to have_current_path(modifier_dossier_path(procedure_for_individual.dossiers.last)) expect(page).to have_current_path(modifier_dossier_path(procedure_for_individual.dossiers.last))
expect(user.dossiers.first.individual.birthdate).to eq("1987-10-14") expect(user.dossiers.first.individual.birthdate).to eq(Date.new(1987, 10, 14))
end end
scenario "with a basic text input field for birthdate (type='date' unsupported)" do scenario "with a basic text input field for birthdate (type='date' unsupported)" do
@ -42,7 +42,7 @@ feature 'As a User I wanna create a dossier' do
expect(page).to have_current_path(modifier_dossier_path(procedure_for_individual.dossiers.last)) expect(page).to have_current_path(modifier_dossier_path(procedure_for_individual.dossiers.last))
expect(user.dossiers.first.individual.birthdate).to eq("1987-10-14") expect(user.dossiers.first.individual.birthdate).to eq(Date.new(1987, 10, 14))
end end
end end

View file

@ -4,7 +4,6 @@ describe Individual do
it { is_expected.to have_db_column(:gender) } it { is_expected.to have_db_column(:gender) }
it { is_expected.to have_db_column(:nom) } it { is_expected.to have_db_column(:nom) }
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 belong_to(:dossier) } it { is_expected.to belong_to(:dossier) }
describe "#save" do describe "#save" do
@ -21,24 +20,21 @@ describe Individual do
context "and the format is dd/mm/yyy " do context "and the format is dd/mm/yyy " do
let(:birthdate_from_user) { "12/11/1980" } let(:birthdate_from_user) { "12/11/1980" }
it { expect(individual.valid?).to be true } it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) }
it { expect(individual.birthdate).to eq("1980-11-12") }
it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) } it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) }
end end
context "and the format is ISO" do context "and the format is ISO" do
let(:birthdate_from_user) { "1980-11-12" } let(:birthdate_from_user) { "1980-11-12" }
it { expect(individual.valid?).to be true } it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) }
it { expect(individual.birthdate).to eq("1980-11-12") }
it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) } it { expect(individual.second_birthdate).to eq(Date.new(1980, 11, 12)) }
end end
context "and the format is WTF" do context "and the format is WTF" do
let(:birthdate_from_user) { "1980 1 12" } let(:birthdate_from_user) { "1980 1 12" }
it { expect(individual.valid?).to be false } it { expect(individual.birthdate).to be_nil }
it { expect(individual.birthdate).to eq("1980 1 12") }
it { expect(individual.second_birthdate).to be_nil } it { expect(individual.second_birthdate).to be_nil }
end end
end end

View file

@ -34,6 +34,6 @@ describe 'new_gestionnaire/dossiers/show.html.haml', type: :view do
it { expect(rendered).to include(individual.gender) } it { expect(rendered).to include(individual.gender) }
it { expect(rendered).to include(individual.nom) } it { expect(rendered).to include(individual.nom) }
it { expect(rendered).to include(individual.prenom) } it { expect(rendered).to include(individual.prenom) }
it { expect(rendered).to include(Date.parse(individual.birthdate).strftime("%d/%m/%Y")) } it { expect(rendered).to include(individual.birthdate.strftime("%d/%m/%Y")) }
end end
end end