Individual: use birthdate as a ... date :)

This commit is contained in:
simon lehericey 2018-04-03 16:38:54 +02:00
parent b67b13aac8
commit c019cb623a
8 changed files with 16 additions and 32 deletions

View file

@ -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