Add repetitions to api

This commit is contained in:
Paul Chavard 2019-07-11 10:28:44 +02:00
parent 127a4d2ffb
commit b266915892
7 changed files with 51 additions and 10 deletions

View file

@ -5,10 +5,11 @@ class Champ < ApplicationRecord
has_many :commentaires has_many :commentaires
has_one_attached :piece_justificative_file has_one_attached :piece_justificative_file
# We declare champ specific relationships (Champs::CarteChamp and Champs::SiretChamp) # We declare champ specific relationships (Champs::CarteChamp, Champs::SiretChamp and Champs::RepetitionChamp)
# here because otherwise we can't easily use includes in our queries. # here because otherwise we can't easily use includes in our queries.
has_many :geo_areas, dependent: :destroy has_many :geo_areas, dependent: :destroy
belongs_to :etablissement, dependent: :destroy belongs_to :etablissement, dependent: :destroy
has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
delegate :libelle, :type_champ, :order_place, :mandatory?, :description, :drop_down_list, :exclude_from_export?, :exclude_from_view?, :repetition?, to: :type_de_champ delegate :libelle, :type_champ, :order_place, :mandatory?, :description, :drop_down_list, :exclude_from_export?, :exclude_from_view?, :repetition?, to: :type_de_champ

View file

@ -1,6 +1,4 @@
class Champs::RepetitionChamp < Champ class Champs::RepetitionChamp < Champ
has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
accepts_nested_attributes_for :champs, allow_destroy: true accepts_nested_attributes_for :champs, allow_destroy: true
def rows def rows
@ -21,11 +19,21 @@ class Champs::RepetitionChamp < Champ
# The user cannot enter any information here so it doesnt make much sense to search # The user cannot enter any information here so it doesnt make much sense to search
end end
def rows_for_export
rows.each.with_index(1).map do |champs, index|
Champs::RepetitionChamp::Row.new(index: index, dossier_id: dossier_id.to_s, champs: champs)
end
end
class Row < Hashie::Dash class Row < Hashie::Dash
property :index property :index
property :dossier_id property :dossier_id
property :champs property :champs
def read_attribute_for_serialization(attribute)
self[attribute]
end
def spreadsheet_columns def spreadsheet_columns
[ [
['Dossier ID', :dossier_id], ['Dossier ID', :dossier_id],

View file

@ -137,12 +137,18 @@ class Dossier < ApplicationRecord
champs: [ champs: [
:geo_areas, :geo_areas,
:etablissement, :etablissement,
piece_justificative_file_attachment: :blob piece_justificative_file_attachment: :blob,
champs: [
piece_justificative_file_attachment: :blob
]
], ],
champs_private: [ champs_private: [
:geo_areas, :geo_areas,
:etablissement, :etablissement,
piece_justificative_file_attachment: :blob piece_justificative_file_attachment: :blob,
champs: [
piece_justificative_file_attachment: :blob
]
], ],
justificatif_motivation_attachment: :blob, justificatif_motivation_attachment: :blob,
attestation: [], attestation: [],

View file

@ -8,6 +8,7 @@ class ChampSerializer < ActiveModel::Serializer
has_many :geo_areas, if: :include_geo_areas? has_many :geo_areas, if: :include_geo_areas?
has_one :etablissement, if: :include_etablissement? has_one :etablissement, if: :include_etablissement?
has_one :entreprise, if: :include_etablissement? has_one :entreprise, if: :include_etablissement?
has_many :rows, serializer: RowSerializer, if: :include_rows?
def value def value
case object case object
@ -35,6 +36,10 @@ class ChampSerializer < ActiveModel::Serializer
object.etablissement&.entreprise object.etablissement&.entreprise
end end
def rows
object.rows_for_export
end
def include_etablissement? def include_etablissement?
object.is_a?(Champs::SiretChamp) object.is_a?(Champs::SiretChamp)
end end
@ -43,6 +48,10 @@ class ChampSerializer < ActiveModel::Serializer
object.is_a?(Champs::CarteChamp) object.is_a?(Champs::CarteChamp)
end end
def include_rows?
object.is_a?(Champs::RepetitionChamp)
end
private private
def legacy_type_de_champ def legacy_type_de_champ

View file

@ -0,0 +1,5 @@
class RowSerializer < ActiveModel::Serializer
has_many :champs, serializer: ChampSerializer
attribute :index, key: :id
end

View file

@ -60,11 +60,7 @@ class ProcedureExportV2Service
champs_repetables.map do |libelle, champs| champs_repetables.map do |libelle, champs|
[ [
libelle, libelle,
champs.flat_map do |champ| champs.flat_map(&:rows_for_export)
champ.rows.each_with_index.map do |champs, index|
Champs::RepetitionChamp::Row.new(index: index + 1, dossier_id: champ.dossier_id.to_s, champs: champs)
end
end
] ]
end end
end end

View file

@ -235,6 +235,22 @@ describe API::V1::DossiersController do
it { expect(subject[:type_champ]).to eq('text') } it { expect(subject[:type_champ]).to eq('text') }
end end
end end
describe 'repetition' do
let(:procedure) { create(:procedure, administrateur: admin) }
let(:champ) { build(:champ_repetition) }
let(:dossier) { create(:dossier, :en_construction, champs: [champ], procedure: procedure) }
subject { super().first[:rows] }
it 'should have rows' do
expect(subject.size).to eq(2)
expect(subject[0][:id]).to eq(1)
expect(subject[0][:champs].size).to eq(2)
expect(subject[0][:champs].map { |c| c[:value] }).to eq(['text', '42'])
expect(subject[0][:champs].map { |c| c[:type_de_champ][:type_champ] }).to eq(['text', 'number'])
end
end
end end
describe 'champs_private' do describe 'champs_private' do