demarches-normaliennes/app/models/champs/cojo_champ.rb
2023-07-10 14:57:34 +02:00

64 lines
1.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class Champs::COJOChamp < Champ
store_accessor :value_json, :accreditation_number, :accreditation_birthdate
store_accessor :data, :accreditation_success, :accreditation_first_name, :accreditation_last_name
after_validation :update_external_id
def accreditation_birthdate
Date.parse(super)
rescue ArgumentError, TypeError
nil
end
def accreditation_success?
accreditation_success == true
end
def accreditation_error?
accreditation_success == false
end
def blank?
accreditation_success.nil?
end
def fetch_external_data?
true
end
def poll_external_data?
true
end
def fetch_external_data
COJOService.new.(accreditation_number:, accreditation_birthdate:)
end
def to_s
"#{accreditation_number} #{accreditation_birthdate}"
end
def accreditation_number_input_id
"#{input_id}-accreditation_number"
end
def accreditation_birthdate_input_id
"#{input_id}-accreditation_birthdate"
end
def focusable_input_id
accreditation_number_input_id
end
private
def update_external_id
if accreditation_number_changed? || accreditation_birthdate_changed?
if accreditation_number.present? && accreditation_birthdate.present? && /\A\d+\z/.match?(accreditation_number)
self.external_id = { accreditation_number:, accreditation_birthdate: }.to_json
else
self.external_id = nil
end
end
end
end