demarches-normaliennes/app/models/individual.rb

31 lines
939 B
Ruby
Raw Normal View History

2018-03-06 13:44:29 +01:00
class Individual < ApplicationRecord
2016-08-30 11:18:43 +02:00
belongs_to :dossier
2018-03-06 13:44:29 +01:00
validates :dossier_id, uniqueness: true
2018-02-08 17:13:15 +01:00
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
2018-02-08 17:13:15 +01:00
before_validation :set_iso_date, if: -> { birthdate_changed? }
2018-04-03 11:57:16 +02:00
before_save :save_birthdate_in_datetime_format
2018-02-08 17:13:15 +01:00
private
def set_iso_date
if birthdate.present? &&
birthdate =~ /\A\d{2}\/\d{2}\/\d{4}\z/
2018-02-08 17:13:15 +01:00
self.birthdate = Date.parse(birthdate).iso8601
end
end
2018-04-03 11:57:16 +02:00
def save_birthdate_in_datetime_format
if birthdate.present?
begin
self.second_birthdate = Date.parse(birthdate)
rescue
end
end
end
2016-08-30 11:18:43 +02:00
end