Merge pull request #1781 from betagouv/improve_individual_date
Improve individual date
This commit is contained in:
commit
eacb9d1c62
5 changed files with 34 additions and 1 deletions
|
@ -8,6 +8,7 @@ class Individual < ApplicationRecord
|
||||||
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
|
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_validation :set_iso_date, if: -> { birthdate_changed? }
|
||||||
|
before_save :save_birthdate_in_datetime_format
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
@ -17,4 +18,13 @@ class Individual < ApplicationRecord
|
||||||
self.birthdate = Date.parse(birthdate).iso8601
|
self.birthdate = Date.parse(birthdate).iso8601
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def save_birthdate_in_datetime_format
|
||||||
|
if birthdate.present?
|
||||||
|
begin
|
||||||
|
self.second_birthdate = Date.parse(birthdate)
|
||||||
|
rescue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddSecondBirthdateColumnToIndividual < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_column :individuals, :second_birthdate, :date
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2018_03_23_101837) do
|
ActiveRecord::Schema.define(version: 2018_04_03_094135) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -366,6 +366,7 @@ ActiveRecord::Schema.define(version: 2018_03_23_101837) do
|
||||||
t.string "gender"
|
t.string "gender"
|
||||||
t.datetime "created_at"
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at"
|
t.datetime "updated_at"
|
||||||
|
t.date "second_birthdate"
|
||||||
t.index ["dossier_id"], name: "index_individuals_on_dossier_id"
|
t.index ["dossier_id"], name: "index_individuals_on_dossier_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
14
lib/tasks/2018_04_03_type_individual_date.rake
Normal file
14
lib/tasks/2018_04_03_type_individual_date.rake
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
namespace :'2018_04_03_type_individual_date' do
|
||||||
|
task set: :environment do
|
||||||
|
Individual.all.each { |individual| save_birthdate_in_datetime_format(individual) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def save_birthdate_in_datetime_format(individual)
|
||||||
|
if individual.birthdate.present?
|
||||||
|
begin
|
||||||
|
individual.update_column(:second_birthdate, Date.parse(individual.birthdate))
|
||||||
|
rescue
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -23,6 +23,7 @@ describe Individual do
|
||||||
|
|
||||||
it { expect(individual.valid?).to be true }
|
it { expect(individual.valid?).to be true }
|
||||||
it { expect(individual.birthdate).to eq("1980-11-12") }
|
it { expect(individual.birthdate).to eq("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
|
||||||
|
@ -30,6 +31,7 @@ describe Individual do
|
||||||
|
|
||||||
it { expect(individual.valid?).to be true }
|
it { expect(individual.valid?).to be true }
|
||||||
it { expect(individual.birthdate).to eq("1980-11-12") }
|
it { expect(individual.birthdate).to eq("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
|
||||||
|
@ -37,6 +39,7 @@ describe Individual do
|
||||||
|
|
||||||
it { expect(individual.valid?).to be false }
|
it { expect(individual.valid?).to be false }
|
||||||
it { expect(individual.birthdate).to eq("1980 1 12") }
|
it { expect(individual.birthdate).to eq("1980 1 12") }
|
||||||
|
it { expect(individual.second_birthdate).to be_nil }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue