fix(Champs::PaysChamp): validates Champs::PaysChamp respecting it's validation context. Also do not save value when external_id/code can't be found by lookup
This commit is contained in:
parent
04f3b6a844
commit
fd5faad31f
3 changed files with 23 additions and 13 deletions
|
@ -74,8 +74,8 @@ class Migrations::BatchUpdatePaysValuesJob < ApplicationJob
|
||||||
def perform(ids)
|
def perform(ids)
|
||||||
ids.each do |id|
|
ids.each do |id|
|
||||||
pays_champ = Champs::PaysChamp.find(id)
|
pays_champ = Champs::PaysChamp.find(id)
|
||||||
next if pays_champ.valid?
|
|
||||||
|
|
||||||
|
next if pays_champ.valid?(pays_champ.public? ? :champs_public_value : :champs_private_value)
|
||||||
code = APIGeoService.country_code(pays_champ.value)
|
code = APIGeoService.country_code(pays_champ.value)
|
||||||
value = if code.present?
|
value = if code.present?
|
||||||
APIGeoService.country_name(code)
|
APIGeoService.country_name(code)
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
class Champs::PaysChamp < Champs::TextChamp
|
class Champs::PaysChamp < Champs::TextChamp
|
||||||
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: true, allow_blank: false
|
with_options if: :validate_champ_value? do
|
||||||
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: true, allow_blank: false
|
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: true, allow_blank: false
|
||||||
|
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: true, allow_blank: false
|
||||||
|
end
|
||||||
|
|
||||||
|
# def value=(code) can reset champs to nil if value is empty, in case of prefill
|
||||||
|
# we do not want to try to save the champ with an nil value
|
||||||
|
with_options if: -> { validation_context == :prefill } do
|
||||||
|
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: false, allow_blank: false
|
||||||
|
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: false, allow_blank: false
|
||||||
|
end
|
||||||
|
|
||||||
def for_export
|
def for_export
|
||||||
[name, code]
|
[name, code]
|
||||||
|
@ -26,8 +35,13 @@ class Champs::PaysChamp < Champs::TextChamp
|
||||||
self.external_id = nil
|
self.external_id = nil
|
||||||
super(nil)
|
super(nil)
|
||||||
elsif code != value
|
elsif code != value
|
||||||
self.external_id = APIGeoService.country_code(code)
|
self.external_id = APIGeoService.country_code(code) # lookup by code which is a country name
|
||||||
super(code)
|
|
||||||
|
if self.external_id # if we match a country code, lookup for country name with code
|
||||||
|
super(APIGeoService.country_name(self.external_id, locale: 'FR'))
|
||||||
|
else # if we did not match any country code, external_id is nil as well as value
|
||||||
|
super(nil)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
describe Migrations::BatchUpdatePaysValuesJob, type: :job do
|
describe Migrations::BatchUpdatePaysValuesJob, type: :job do
|
||||||
before do
|
|
||||||
pays_champ.save(validate: false)
|
|
||||||
end
|
|
||||||
|
|
||||||
subject { described_class.perform_now([pays_champ.id]) }
|
subject { described_class.perform_now([pays_champ.id]) }
|
||||||
|
|
||||||
context "the value is correct" do
|
context "the value is correct" do
|
||||||
let(:pays_champ) { build(:champ_pays, value: 'France', external_id: 'FR') }
|
let(:pays_champ) { create(:champ_pays).tap { _1.update_columns(value: 'France', external_id: 'FR') } }
|
||||||
|
|
||||||
it 'does not change it' do
|
it 'does not change it' do
|
||||||
subject
|
subject
|
||||||
|
@ -16,7 +12,7 @@ describe Migrations::BatchUpdatePaysValuesJob, type: :job do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the value is incorrect" do
|
context "the value is incorrect" do
|
||||||
let(:pays_champ) { build(:champ_pays, value: 'Incorrect') }
|
let(:pays_champ) { create(:champ_pays).tap { _1.update_columns(value: 'Incorrect') } }
|
||||||
|
|
||||||
it 'updates value to nil' do
|
it 'updates value to nil' do
|
||||||
subject
|
subject
|
||||||
|
@ -26,7 +22,7 @@ describe Migrations::BatchUpdatePaysValuesJob, type: :job do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the value is easily cleanable" do
|
context "the value is easily cleanable" do
|
||||||
let(:pays_champ) { build(:champ_pays, value: 'Vietnam') }
|
let(:pays_champ) { create(:champ_pays).tap { _1.update_columns(value: 'Vietnam') } }
|
||||||
|
|
||||||
it 'cleans the value' do
|
it 'cleans the value' do
|
||||||
subject
|
subject
|
||||||
|
@ -36,7 +32,7 @@ describe Migrations::BatchUpdatePaysValuesJob, type: :job do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "the value is hard to clean" do
|
context "the value is hard to clean" do
|
||||||
let(:pays_champ) { build(:champ_pays, value: 'CHRISTMAS (ILE)') }
|
let(:pays_champ) { create(:champ_pays).tap { _1.update_columns(value: 'CHRISTMAS (ILE)') } }
|
||||||
|
|
||||||
it 'cleans the value' do
|
it 'cleans the value' do
|
||||||
subject
|
subject
|
||||||
|
|
Loading…
Add table
Reference in a new issue