2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: champs
|
|
|
|
#
|
2021-02-04 19:23:40 +01:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# data :jsonb
|
|
|
|
# fetch_external_data_exceptions :string is an Array
|
2022-12-01 12:00:21 +01:00
|
|
|
# prefilled :boolean default(FALSE)
|
2021-02-04 19:23:40 +01:00
|
|
|
# private :boolean default(FALSE), not null
|
2021-10-20 17:26:09 +02:00
|
|
|
# rebased_at :datetime
|
2021-02-04 19:23:40 +01:00
|
|
|
# type :string
|
|
|
|
# value :string
|
2021-10-05 15:37:13 +02:00
|
|
|
# value_json :jsonb
|
2021-02-04 19:23:40 +01:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2022-09-21 15:20:42 +02:00
|
|
|
# dossier_id :integer
|
2021-02-04 19:23:40 +01:00
|
|
|
# etablissement_id :integer
|
|
|
|
# external_id :string
|
|
|
|
# parent_id :bigint
|
2022-12-16 12:39:51 +01:00
|
|
|
# row_id :string
|
2022-09-21 15:20:42 +02:00
|
|
|
# type_de_champ_id :integer
|
2020-08-06 16:35:45 +02:00
|
|
|
#
|
2018-02-13 18:18:20 +01:00
|
|
|
class Champs::PaysChamp < Champs::TextChamp
|
2023-01-18 12:52:38 +01:00
|
|
|
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: true, allow_blank: false
|
|
|
|
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: true, allow_blank: false
|
|
|
|
|
2022-12-20 22:11:34 +01:00
|
|
|
def for_export
|
2022-12-21 17:35:05 +01:00
|
|
|
[name, code]
|
2022-12-20 22:11:34 +01:00
|
|
|
end
|
|
|
|
|
2022-12-20 21:28:42 +01:00
|
|
|
def to_s
|
2022-12-21 17:35:05 +01:00
|
|
|
name
|
2022-12-20 21:28:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def for_tag
|
2022-12-21 17:35:05 +01:00
|
|
|
name
|
2022-12-20 21:28:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def selected
|
|
|
|
code || value
|
|
|
|
end
|
|
|
|
|
|
|
|
def value=(code)
|
|
|
|
if code&.size == 2
|
|
|
|
self.external_id = code
|
|
|
|
super(APIGeoService.country_name(code, locale: 'FR'))
|
|
|
|
elsif code.blank?
|
|
|
|
self.external_id = nil
|
|
|
|
super(nil)
|
|
|
|
elsif code != value
|
|
|
|
self.external_id = APIGeoService.country_code(code)
|
|
|
|
super(code)
|
2021-08-31 12:49:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-20 21:28:42 +01:00
|
|
|
def code
|
|
|
|
external_id || APIGeoService.country_code(value)
|
2021-08-31 12:49:35 +02:00
|
|
|
end
|
|
|
|
|
2022-12-21 17:35:05 +01:00
|
|
|
def name
|
2022-12-27 16:28:34 +01:00
|
|
|
if external_id.present?
|
2022-12-20 21:28:42 +01:00
|
|
|
APIGeoService.country_name(external_id)
|
|
|
|
else
|
|
|
|
value.present? ? value.to_s : ''
|
|
|
|
end
|
2021-08-31 12:49:35 +02:00
|
|
|
end
|
2018-02-13 18:18:20 +01:00
|
|
|
end
|