2023-01-19 09:43:19 +01:00
|
|
|
|
class Champs::EpciChamp < Champs::TextChamp
|
2023-12-06 17:12:02 +01:00
|
|
|
|
store_accessor :value_json, :code_departement, :code_region
|
2023-01-19 09:43:19 +01:00
|
|
|
|
before_validation :on_departement_change
|
2023-12-26 16:36:53 +01:00
|
|
|
|
before_validation :on_epci_name_changes
|
2023-01-19 09:43:19 +01:00
|
|
|
|
|
2023-02-06 14:24:51 +01:00
|
|
|
|
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
|
|
|
|
|
validate :external_id_in_departement_epci_codes, unless: -> { code_departement.nil? || external_id.nil? }
|
|
|
|
|
validate :value_in_departement_epci_names, unless: -> { code_departement.nil? || external_id.nil? || value.nil? }
|
|
|
|
|
|
2023-01-19 09:43:19 +01:00
|
|
|
|
def for_export
|
|
|
|
|
[value, code, "#{code_departement} – #{departement_name}"]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def departement_name
|
|
|
|
|
APIGeoService.departement_name(code_departement)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def departement
|
|
|
|
|
{ code: code_departement, name: departement_name }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def departement?
|
|
|
|
|
code_departement.present?
|
|
|
|
|
end
|
|
|
|
|
|
2023-08-17 16:43:39 +02:00
|
|
|
|
def html_label?
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def legend_label?
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-19 09:43:19 +01:00
|
|
|
|
def code?
|
|
|
|
|
code.present?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def name
|
|
|
|
|
value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def code
|
|
|
|
|
external_id
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-06 17:12:02 +01:00
|
|
|
|
def code_region
|
|
|
|
|
APIGeoService.region_code_by_departement(departement_name)
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-19 09:43:19 +01:00
|
|
|
|
def selected
|
|
|
|
|
code
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def value=(code)
|
|
|
|
|
if code.blank? || !departement?
|
|
|
|
|
self.external_id = nil
|
|
|
|
|
super(nil)
|
|
|
|
|
else
|
|
|
|
|
self.external_id = code
|
|
|
|
|
super(APIGeoService.epci_name(code_departement, code))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-02-27 15:33:25 +01:00
|
|
|
|
def code_departement_input_id
|
|
|
|
|
"#{input_id}-code_departement"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def epci_input_id
|
|
|
|
|
"#{input_id}-epci"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def focusable_input_id
|
|
|
|
|
code_departement_input_id
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-19 09:43:19 +01:00
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def on_departement_change
|
|
|
|
|
if code_departement_changed?
|
|
|
|
|
self.external_id = nil
|
|
|
|
|
self.value = nil
|
2023-12-06 17:12:02 +01:00
|
|
|
|
self.code_region = code_region
|
2023-01-19 09:43:19 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2023-02-06 14:24:51 +01:00
|
|
|
|
|
|
|
|
|
def code_departement_in_departement_codes
|
|
|
|
|
return if code_departement.in?(APIGeoService.departements.pluck(:code))
|
|
|
|
|
|
|
|
|
|
errors.add(:code_departement, :not_in_departement_codes)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def external_id_in_departement_epci_codes
|
|
|
|
|
return if external_id.in?(APIGeoService.epcis(code_departement).pluck(:code))
|
|
|
|
|
|
|
|
|
|
errors.add(:external_id, :not_in_departement_epci_codes)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def value_in_departement_epci_names
|
|
|
|
|
return if value.in?(APIGeoService.epcis(code_departement).pluck(:name))
|
|
|
|
|
|
|
|
|
|
errors.add(:value, :not_in_departement_epci_names)
|
|
|
|
|
end
|
2023-12-26 16:36:53 +01:00
|
|
|
|
|
|
|
|
|
def on_epci_name_changes
|
|
|
|
|
return if external_id.nil? || code_departement.nil?
|
|
|
|
|
return if value.in?(APIGeoService.epcis(code_departement).pluck(:name))
|
|
|
|
|
|
|
|
|
|
if external_id.in?(APIGeoService.epcis(code_departement).pluck(:code))
|
|
|
|
|
self.value = (external_id)
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-01-19 09:43:19 +01:00
|
|
|
|
end
|