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-03-04 09:06:50 +01:00
|
|
|
|
def feature
|
|
|
|
|
data.to_json if full_address?
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
def to_s
|
2021-04-08 17:43:45 +02:00
|
|
|
|
address_label.presence || ''
|
2021-02-17 13:54:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
2024-04-02 17:05:44 +02:00
|
|
|
|
def for_tag(path = :value)
|
|
|
|
|
case path
|
|
|
|
|
when :value
|
|
|
|
|
address_label
|
|
|
|
|
when :departement
|
|
|
|
|
departement_code_and_name || ''
|
|
|
|
|
when :commune
|
|
|
|
|
commune_name || ''
|
|
|
|
|
end
|
2021-02-17 13:54:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
2024-04-02 17:05:44 +02:00
|
|
|
|
def for_export(path = :value)
|
|
|
|
|
case path
|
|
|
|
|
when :value
|
|
|
|
|
value.present? ? address_label : nil
|
|
|
|
|
when :departement
|
|
|
|
|
departement_code_and_name
|
|
|
|
|
when :commune
|
|
|
|
|
commune_name
|
|
|
|
|
end
|
2021-02-17 13:54:24 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def for_api
|
|
|
|
|
address_label
|
|
|
|
|
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
|