Always serialize type_de_champ stable_id as id

This commit is contained in:
Paul Chavard 2020-09-02 11:46:12 +02:00
parent 0d9ac9ffd9
commit d1de1587d3
2 changed files with 3 additions and 8 deletions

View file

@ -44,7 +44,6 @@ module NewAdministrateur
{
type_de_champ: type_de_champ.as_json(
except: [
:id,
:created_at,
:options,
:order_place,
@ -64,7 +63,7 @@ module NewAdministrateur
:piece_justificative_template_url,
:quartiers_prioritaires
]
).merge(id: TypeDeChamp.format_stable_id(type_de_champ.stable_id))
)
}
end

View file

@ -304,7 +304,7 @@ class TypeDeChamp < ApplicationRecord
def read_attribute_for_serialization(name)
if name == 'id'
self.class.format_stable_id(stable_id)
stable_id
else
super
end
@ -317,15 +317,11 @@ class TypeDeChamp < ApplicationRecord
# This is only needed for a clean migration without downtime. We want to ensure
# that if editor send a simple id because it was loaded before deployment
# we would still do the right thing.
def self.format_stable_id(stable_id)
"stable:#{stable_id}"
end
def self.to_stable_id(id_or_stable_id)
if id_or_stable_id.to_s =~ /^stable:/
id_or_stable_id.to_s.gsub(/^stable:/, '')
else
find(id_or_stable_id).stable_id
id_or_stable_id
end
end