2018-02-09 17:38:30 +01:00
|
|
|
class ChampSerializer < ActiveModel::Serializer
|
2018-04-24 20:27:19 +02:00
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2018-02-09 17:38:30 +01:00
|
|
|
attributes :value
|
|
|
|
|
|
|
|
has_one :type_de_champ
|
2018-04-24 20:27:19 +02:00
|
|
|
|
2018-11-13 18:48:04 +01:00
|
|
|
has_many :geo_areas, if: :include_geo_areas?
|
|
|
|
has_one :etablissement, if: :include_etablissement?
|
|
|
|
has_one :entreprise, if: :include_etablissement?
|
|
|
|
|
2018-04-24 20:27:19 +02:00
|
|
|
def value
|
2018-11-06 14:14:22 +01:00
|
|
|
case object
|
|
|
|
when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire
|
|
|
|
object.geometry
|
2018-11-13 18:48:04 +01:00
|
|
|
when Champs::CarteChamp
|
|
|
|
if object.value.present?
|
|
|
|
JSON.parse(object.value)
|
|
|
|
end
|
|
|
|
when Champs::DecimalNumberChamp
|
|
|
|
if object.value.present?
|
|
|
|
object.value.to_f
|
|
|
|
end
|
|
|
|
when Champs::IntegerNumberChamp
|
|
|
|
if object.value.present?
|
|
|
|
object.value.to_i
|
|
|
|
end
|
|
|
|
when Champs::LinkedDropDownListChamp
|
|
|
|
if object.value.present?
|
|
|
|
{ primary: object.primary_value, secondary: object.secondary_value }
|
|
|
|
end
|
|
|
|
when Champs::PieceJustificativeChamp
|
2018-11-06 14:14:22 +01:00
|
|
|
if object.piece_justificative_file.attached?
|
|
|
|
url_for(object.piece_justificative_file)
|
|
|
|
end
|
2018-11-13 18:48:04 +01:00
|
|
|
else
|
|
|
|
object.value
|
2018-11-06 14:14:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_de_champ
|
|
|
|
case object
|
|
|
|
when GeoArea, UserGeometry, Cadastre, QuartierPrioritaire
|
|
|
|
legacy_type_de_champ
|
|
|
|
else
|
|
|
|
object.type_de_champ
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-13 18:48:04 +01:00
|
|
|
def etablissement
|
|
|
|
object.etablissement
|
|
|
|
end
|
|
|
|
|
|
|
|
def entreprise
|
|
|
|
object.etablissement&.entreprise
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_etablissement?
|
|
|
|
object.is_a?(Champs::SiretChamp)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_geo_areas?
|
|
|
|
object.is_a?(Champs::CarteChamp)
|
|
|
|
end
|
|
|
|
|
2018-11-06 14:14:22 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def legacy_type_de_champ
|
|
|
|
{
|
|
|
|
id: -1,
|
|
|
|
libelle: legacy_carto_libelle,
|
|
|
|
type_champ: legacy_carto_type_champ,
|
|
|
|
order_place: -1,
|
|
|
|
descripton: ''
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_carto_libelle
|
|
|
|
case object
|
|
|
|
when UserGeometry, Cadastre, QuartierPrioritaire
|
|
|
|
object.class.name.underscore.tr('_', ' ')
|
|
|
|
else
|
|
|
|
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
|
|
|
|
'user geometry'
|
|
|
|
else
|
|
|
|
object.source.to_s.tr('_', ' ')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_carto_type_champ
|
|
|
|
case object
|
|
|
|
when UserGeometry, Cadastre, QuartierPrioritaire
|
|
|
|
object.class.name.underscore
|
|
|
|
else
|
|
|
|
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
|
|
|
|
'user_geometry'
|
|
|
|
else
|
|
|
|
object.source.to_s
|
|
|
|
end
|
2018-04-24 20:27:19 +02:00
|
|
|
end
|
|
|
|
end
|
2018-02-09 17:38:30 +01:00
|
|
|
end
|