2018-02-13 18:18:20 +01:00
|
|
|
|
class Champs::AddressChamp < Champs::TextChamp
|
2021-02-17 13:54:24 +01:00
|
|
|
|
def full_address?
|
|
|
|
|
data.present?
|
|
|
|
|
end
|
|
|
|
|
|
2024-02-13 13:40:33 +01:00
|
|
|
|
def feature=(value)
|
2024-03-04 09:06:50 +01:00
|
|
|
|
if value.blank?
|
|
|
|
|
self.data = nil
|
|
|
|
|
else
|
|
|
|
|
feature = JSON.parse(value)
|
|
|
|
|
if feature.key?('properties')
|
|
|
|
|
self.data = APIGeoService.parse_ban_address(feature)
|
|
|
|
|
else
|
|
|
|
|
self.data = feature
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-02-13 13:40:33 +01:00
|
|
|
|
rescue JSON::ParserError
|
|
|
|
|
self.data = nil
|
|
|
|
|
end
|
|
|
|
|
|
2024-05-06 18:09:10 +02:00
|
|
|
|
def selected_items
|
|
|
|
|
if value.present?
|
|
|
|
|
[{ value:, label: value, data: full_address? ? data : nil }]
|
|
|
|
|
else
|
|
|
|
|
[]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-17 13:54:24 +01:00
|
|
|
|
def address
|
|
|
|
|
full_address? ? data : nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def address_label
|
|
|
|
|
full_address? ? data['label'] : value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def search_terms
|
|
|
|
|
if full_address?
|
2024-02-13 13:40:33 +01:00
|
|
|
|
[data['label'], data['department_name'], data['region_name'], data['city_name']]
|
2021-02-17 13:54:24 +01:00
|
|
|
|
else
|
|
|
|
|
[address_label]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-02-13 13:40:33 +01:00
|
|
|
|
def code_departement
|
|
|
|
|
if full_address?
|
|
|
|
|
address.fetch('department_code')
|
|
|
|
|
end
|
2021-02-17 13:54:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
2024-02-13 13:40:33 +01:00
|
|
|
|
def code_region
|
|
|
|
|
if full_address?
|
|
|
|
|
address.fetch('region_code')
|
|
|
|
|
end
|
2021-02-17 13:54:24 +01:00
|
|
|
|
end
|
2023-04-05 18:02:42 +02:00
|
|
|
|
|
|
|
|
|
def departement_name
|
2024-02-13 13:40:33 +01:00
|
|
|
|
APIGeoService.departement_name(code_departement)
|
2023-04-05 18:02:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
2023-10-20 11:22:51 +02:00
|
|
|
|
def departement_code_and_name
|
2023-11-17 12:48:25 +01:00
|
|
|
|
if full_address?
|
2024-02-13 13:40:33 +01:00
|
|
|
|
"#{code_departement} – #{departement_name}"
|
2023-11-17 12:48:25 +01:00
|
|
|
|
end
|
2023-10-20 11:22:51 +02:00
|
|
|
|
end
|
|
|
|
|
|
2023-04-05 18:02:42 +02:00
|
|
|
|
def departement
|
|
|
|
|
if full_address?
|
2024-02-13 13:40:33 +01:00
|
|
|
|
{ code: code_departement, name: departement_name }
|
2023-04-05 18:02:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-11-17 12:48:25 +01:00
|
|
|
|
def commune_name
|
2024-04-16 14:54:53 +02:00
|
|
|
|
return if !full_address?
|
|
|
|
|
|
|
|
|
|
commune = APIGeoService.commune_name(code_departement, address['city_code'])
|
|
|
|
|
|
|
|
|
|
return commune if address['postal_code'].blank?
|
|
|
|
|
|
|
|
|
|
"#{commune} (#{address['postal_code']})"
|
2023-11-17 12:48:25 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-04-05 18:02:42 +02:00
|
|
|
|
def commune
|
|
|
|
|
if full_address?
|
2023-06-23 15:46:37 +02:00
|
|
|
|
city_code = address.fetch('city_code')
|
|
|
|
|
city_name = address.fetch('city_name')
|
|
|
|
|
postal_code = address.fetch('postal_code')
|
|
|
|
|
|
2024-02-13 13:40:33 +01:00
|
|
|
|
commune_name = APIGeoService.commune_name(code_departement, city_code)
|
|
|
|
|
commune_code = APIGeoService.commune_code(code_departement, city_name)
|
2023-06-23 15:46:37 +02:00
|
|
|
|
|
|
|
|
|
if commune_name.present?
|
|
|
|
|
{
|
|
|
|
|
code: city_code,
|
|
|
|
|
name: commune_name
|
|
|
|
|
}
|
|
|
|
|
elsif commune_code.present?
|
|
|
|
|
{
|
|
|
|
|
code: commune_code,
|
|
|
|
|
name: city_name
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
code: city_code,
|
|
|
|
|
name: city_name
|
|
|
|
|
}
|
|
|
|
|
end.merge(postal_code:)
|
2023-04-05 18:02:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-02-13 18:18:20 +01:00
|
|
|
|
end
|