Add Champ#for_api

This commit is contained in:
gregoirenovel 2018-12-28 17:59:14 +01:00
parent 083dfb4eaa
commit 866c74ce63
7 changed files with 25 additions and 21 deletions

View file

@ -47,6 +47,10 @@ class Champ < ApplicationRecord
value.presence
end
def for_api
value
end
def main_value_name
:value
end

View file

@ -88,4 +88,8 @@ class Champs::CarteChamp < Champ
)
end
end
def for_api
geo_json.present? ? geo_json.to_json : nil
end
end

View file

@ -4,4 +4,8 @@ class Champs::DecimalNumberChamp < Champ
def for_export
value.present? ? value.to_f : nil
end
def for_api
value.present? ? value.to_f : nil
end
end

View file

@ -4,4 +4,8 @@ class Champs::IntegerNumberChamp < Champ
def for_export
value.present? ? value.to_i : nil
end
def for_api
value.present? ? value.to_i : nil
end
end

View file

@ -37,6 +37,10 @@ class Champs::LinkedDropDownListChamp < Champ
value.present? ? "#{primary_value || ''};#{secondary_value || ''}" : nil
end
def for_api
value.present? ? { primary: primary_value, secondary: secondary_value } : nil
end
def mandatory_and_blank?
mandatory? && (primary_value.blank? || secondary_value.blank?)
end

View file

@ -48,6 +48,10 @@ class Champs::PieceJustificativeChamp < Champ
errors
end
def for_api
Rails.application.routes.url_helpers.url_for(piece_justificative_file)
end
private
def create_virus_scan

View file

@ -13,28 +13,8 @@ class ChampSerializer < ActiveModel::Serializer
case object
when GeoArea
object.geometry
when Champs::CarteChamp
if object.geo_json.present?
object.geo_json.to_json
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
if object.piece_justificative_file.attached?
url_for(object.piece_justificative_file)
end
else
object.value
object.for_api
end
end