From 866c74ce6321667818ed7ac45cfcf01f658b199d Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Fri, 28 Dec 2018 17:59:14 +0100 Subject: [PATCH] Add Champ#for_api --- app/models/champ.rb | 4 ++++ app/models/champs/carte_champ.rb | 4 ++++ app/models/champs/decimal_number_champ.rb | 4 ++++ app/models/champs/integer_number_champ.rb | 4 ++++ .../champs/linked_drop_down_list_champ.rb | 4 ++++ .../champs/piece_justificative_champ.rb | 4 ++++ app/serializers/champ_serializer.rb | 22 +------------------ 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 05f197970..8a7a9b91c 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -47,6 +47,10 @@ class Champ < ApplicationRecord value.presence end + def for_api + value + end + def main_value_name :value end diff --git a/app/models/champs/carte_champ.rb b/app/models/champs/carte_champ.rb index c7631b1ad..e03f9e2e7 100644 --- a/app/models/champs/carte_champ.rb +++ b/app/models/champs/carte_champ.rb @@ -88,4 +88,8 @@ class Champs::CarteChamp < Champ ) end end + + def for_api + geo_json.present? ? geo_json.to_json : nil + end end diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index 465d4ec5c..ec8000541 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -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 diff --git a/app/models/champs/integer_number_champ.rb b/app/models/champs/integer_number_champ.rb index a483ab80c..ce04b1904 100644 --- a/app/models/champs/integer_number_champ.rb +++ b/app/models/champs/integer_number_champ.rb @@ -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 diff --git a/app/models/champs/linked_drop_down_list_champ.rb b/app/models/champs/linked_drop_down_list_champ.rb index d0fade636..cb4ff4ae8 100644 --- a/app/models/champs/linked_drop_down_list_champ.rb +++ b/app/models/champs/linked_drop_down_list_champ.rb @@ -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 diff --git a/app/models/champs/piece_justificative_champ.rb b/app/models/champs/piece_justificative_champ.rb index 315cc1988..208aca715 100644 --- a/app/models/champs/piece_justificative_champ.rb +++ b/app/models/champs/piece_justificative_champ.rb @@ -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 diff --git a/app/serializers/champ_serializer.rb b/app/serializers/champ_serializer.rb index db0f8434f..76d1ca76d 100644 --- a/app/serializers/champ_serializer.rb +++ b/app/serializers/champ_serializer.rb @@ -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