2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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?
|
2019-07-11 10:28:44 +02:00
|
|
|
has_many :rows, serializer: RowSerializer, if: :include_rows?
|
2018-11-13 18:48:04 +01:00
|
|
|
|
2018-04-24 20:27:19 +02:00
|
|
|
def value
|
2018-11-06 14:14:22 +01:00
|
|
|
case object
|
2018-11-27 15:55:14 +01:00
|
|
|
when GeoArea
|
2023-02-14 18:38:01 +01:00
|
|
|
object.geometry
|
2018-11-13 18:48:04 +01:00
|
|
|
else
|
2024-10-21 11:51:34 +02:00
|
|
|
object.type_de_champ.champ_value_for_api(object, version: 1)
|
2018-11-06 14:14:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def type_de_champ
|
|
|
|
case object
|
2018-11-27 15:55:14 +01:00
|
|
|
when GeoArea
|
2018-11-06 14:14:22 +01:00
|
|
|
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
|
|
|
|
|
2023-11-28 17:30:23 +01:00
|
|
|
class Row < Hashie::Dash
|
|
|
|
property :index
|
|
|
|
property :champs
|
|
|
|
|
|
|
|
def read_attribute_for_serialization(attribute)
|
|
|
|
self[attribute]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-11 10:28:44 +02:00
|
|
|
def rows
|
2024-08-27 10:29:10 +02:00
|
|
|
object.rows.map.with_index(1) { |champs, index| Row.new(index:, champs:) }
|
2019-07-11 10:28:44 +02:00
|
|
|
end
|
|
|
|
|
2018-11-13 18:48:04 +01:00
|
|
|
def include_etablissement?
|
|
|
|
object.is_a?(Champs::SiretChamp)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_geo_areas?
|
|
|
|
object.is_a?(Champs::CarteChamp)
|
|
|
|
end
|
|
|
|
|
2019-07-11 10:28:44 +02:00
|
|
|
def include_rows?
|
|
|
|
object.is_a?(Champs::RepetitionChamp)
|
|
|
|
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,
|
2018-11-29 23:10:13 +01:00
|
|
|
description: ''
|
2018-11-06 14:14:22 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_carto_libelle
|
2018-11-27 15:55:14 +01:00
|
|
|
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
|
|
|
|
'user geometry'
|
2018-11-06 14:14:22 +01:00
|
|
|
else
|
2018-11-27 15:55:14 +01:00
|
|
|
object.source.to_s.tr('_', ' ')
|
2018-11-06 14:14:22 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def legacy_carto_type_champ
|
2018-11-27 15:55:14 +01:00
|
|
|
if object.source == GeoArea.sources.fetch(:selection_utilisateur)
|
|
|
|
'user_geometry'
|
2018-11-06 14:14:22 +01:00
|
|
|
else
|
2018-11-27 15:55:14 +01:00
|
|
|
object.source.to_s
|
2018-04-24 20:27:19 +02:00
|
|
|
end
|
|
|
|
end
|
2018-02-09 17:38:30 +01:00
|
|
|
end
|