Individual: save the birthdate in Y
This commit is contained in:
parent
0093db18a1
commit
8828663880
2 changed files with 13 additions and 0 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
|
||||||
|
|
|
@ -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…
Add table
Reference in a new issue