chore(conditional): update champs to use in_region operators

This commit is contained in:
Eric Leroy-Terquem 2023-12-06 17:12:02 +01:00
parent c9676020b4
commit 3db21b633c
3 changed files with 13 additions and 2 deletions

View file

@ -1,5 +1,5 @@
class Champs::CommuneChamp < Champs::TextChamp
store_accessor :value_json, :code_departement, :code_postal
store_accessor :value_json, :code_departement, :code_postal, :code_region
before_save :on_codes_change, if: :should_refresh_after_code_change?
def for_export
@ -82,6 +82,7 @@ class Champs::CommuneChamp < Champs::TextChamp
if commune.present?
self.code_departement = commune[:departement_code]
self.code_region = commune[:region_code]
self.value = commune[:name]
else
self.code_departement = nil

View file

@ -1,4 +1,5 @@
class Champs::DepartementChamp < Champs::TextChamp
store_accessor :value_json, :code_region
validate :value_in_departement_names, unless: -> { value.nil? }
validate :external_id_in_departement_codes, unless: -> { external_id.nil? }
@ -39,6 +40,10 @@ class Champs::DepartementChamp < Champs::TextChamp
end
end
def code_region(departement_name)
APIGeoService.region_code_by_departement(name)
end
def value=(code)
if [2, 3].include?(code&.size)
self.external_id = code

View file

@ -1,5 +1,5 @@
class Champs::EpciChamp < Champs::TextChamp
store_accessor :value_json, :code_departement
store_accessor :value_json, :code_departement, :code_region
before_validation :on_departement_change
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
@ -42,6 +42,10 @@ class Champs::EpciChamp < Champs::TextChamp
external_id
end
def code_region
APIGeoService.region_code_by_departement(departement_name)
end
def selected
code
end
@ -74,6 +78,7 @@ class Champs::EpciChamp < Champs::TextChamp
if code_departement_changed?
self.external_id = nil
self.value = nil
self.code_region = code_region
end
end