From c019cb623a4a67f66e820e7ffe578154c1cb0ed3 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 3 Apr 2018 16:38:54 +0200 Subject: [PATCH] Individual: use birthdate as a ... date :) --- app/controllers/users/dossiers_controller.rb | 4 +++- app/models/individual.rb | 22 ++++--------------- app/views/dossiers/_infos_dossier.html.haml | 2 +- .../dossiers/_identite_individual.html.haml | 2 +- spec/factories/individual.rb | 2 +- spec/features/users/dossier_creation_spec.rb | 4 ++-- spec/models/individual_spec.rb | 10 +++------ .../dossiers/show.html.haml_spec.rb | 2 +- 8 files changed, 16 insertions(+), 32 deletions(-) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 815b00ede..12fd0573a 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -92,7 +92,9 @@ class Users::DossiersController < UsersController individual.update_column :gender, @facade.dossier.france_connect_information.gender 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 :birthdate, @facade.dossier.france_connect_information.birthdate.iso8601 + + individual.birthdate = @facade.dossier.france_connect_information.birthdate + individual.save end rescue ActiveRecord::RecordNotFound diff --git a/app/models/individual.rb b/app/models/individual.rb index b5ac55067..5e3fd2ea6 100644 --- a/app/models/individual.rb +++ b/app/models/individual.rb @@ -5,26 +5,12 @@ class Individual < ApplicationRecord validates :gender, presence: true, 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 :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? } - before_save :save_birthdate_in_datetime_format - - private - - def set_iso_date - if birthdate.present? && - birthdate =~ /\A\d{2}\/\d{2}\/\d{4}\z/ - self.birthdate = Date.parse(birthdate).iso8601 - end + def birthdate + second_birthdate end - def save_birthdate_in_datetime_format - if birthdate.present? - begin - self.second_birthdate = Date.parse(birthdate) - rescue - end - end + def birthdate=(date) + self.second_birthdate = date end end diff --git a/app/views/dossiers/_infos_dossier.html.haml b/app/views/dossiers/_infos_dossier.html.haml index c6c3cbde6..d5e0f1ec1 100644 --- a/app/views/dossiers/_infos_dossier.html.haml +++ b/app/views/dossiers/_infos_dossier.html.haml @@ -20,7 +20,7 @@ .row .col-xs-6.depositaire-label Date de naissance .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 - if @facade.champs.present? diff --git a/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml b/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml index 27d8e44a5..c1d08eb5d 100644 --- a/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml +++ b/app/views/new_gestionnaire/dossiers/_identite_individual.html.haml @@ -12,4 +12,4 @@ - if individual.birthdate.present? %tr %th.libelle Date de naissance : - %td= Date.parse(individual.birthdate).strftime("%d/%m/%Y") + %td= individual.birthdate&.strftime("%d/%m/%Y") diff --git a/spec/factories/individual.rb b/spec/factories/individual.rb index 05128e117..75e0c4b19 100644 --- a/spec/factories/individual.rb +++ b/spec/factories/individual.rb @@ -3,6 +3,6 @@ FactoryBot.define do gender 'M.' nom 'Julien' prenom 'Xavier' - birthdate '1991-11-01' + birthdate Date.new(1991, 11, 01) end end diff --git a/spec/features/users/dossier_creation_spec.rb b/spec/features/users/dossier_creation_spec.rb index aed39137e..84572b3d5 100644 --- a/spec/features/users/dossier_creation_spec.rb +++ b/spec/features/users/dossier_creation_spec.rb @@ -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(user.dossiers.first.individual.birthdate).to eq("1987-10-14") + expect(user.dossiers.first.individual.birthdate).to eq(Date.new(1987, 10, 14)) end 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(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 diff --git a/spec/models/individual_spec.rb b/spec/models/individual_spec.rb index 43d627139..6b11324f1 100644 --- a/spec/models/individual_spec.rb +++ b/spec/models/individual_spec.rb @@ -4,7 +4,6 @@ describe Individual do it { is_expected.to have_db_column(:gender) } it { is_expected.to have_db_column(:nom) } it { is_expected.to have_db_column(:prenom) } - it { is_expected.to have_db_column(:birthdate) } it { is_expected.to belong_to(:dossier) } describe "#save" do @@ -21,24 +20,21 @@ describe Individual do 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") } + it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } it { expect(individual.second_birthdate).to eq(Date.new(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") } + it { expect(individual.birthdate).to eq(Date.new(1980, 11, 12)) } it { expect(individual.second_birthdate).to eq(Date.new(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") } + it { expect(individual.birthdate).to be_nil } it { expect(individual.second_birthdate).to be_nil } end end diff --git a/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb b/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb index b45e7123f..7ac72030c 100644 --- a/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb +++ b/spec/views/new_gestionnaire/dossiers/show.html.haml_spec.rb @@ -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.nom) } 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