diff --git a/app/models/champ.rb b/app/models/champ.rb index 91835499f..4c091e04f 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -5,10 +5,11 @@ class Champ < ApplicationRecord has_many :commentaires 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. has_many :geo_areas, 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 diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 2173fe232..4d95e574d 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -1,6 +1,4 @@ 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 def rows @@ -21,11 +19,21 @@ class Champs::RepetitionChamp < Champ # The user cannot enter any information here so it doesn’t make much sense to search 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 property :index property :dossier_id property :champs + def read_attribute_for_serialization(attribute) + self[attribute] + end + def spreadsheet_columns [ ['Dossier ID', :dossier_id], diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 3aee61aa1..30bbe5312 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -137,12 +137,18 @@ class Dossier < ApplicationRecord champs: [ :geo_areas, :etablissement, - piece_justificative_file_attachment: :blob + piece_justificative_file_attachment: :blob, + champs: [ + piece_justificative_file_attachment: :blob + ] ], champs_private: [ :geo_areas, :etablissement, - piece_justificative_file_attachment: :blob + piece_justificative_file_attachment: :blob, + champs: [ + piece_justificative_file_attachment: :blob + ] ], justificatif_motivation_attachment: :blob, attestation: [], diff --git a/app/serializers/champ_serializer.rb b/app/serializers/champ_serializer.rb index 76d1ca76d..670979c81 100644 --- a/app/serializers/champ_serializer.rb +++ b/app/serializers/champ_serializer.rb @@ -8,6 +8,7 @@ class ChampSerializer < ActiveModel::Serializer has_many :geo_areas, if: :include_geo_areas? has_one :etablissement, if: :include_etablissement? has_one :entreprise, if: :include_etablissement? + has_many :rows, serializer: RowSerializer, if: :include_rows? def value case object @@ -35,6 +36,10 @@ class ChampSerializer < ActiveModel::Serializer object.etablissement&.entreprise end + def rows + object.rows_for_export + end + def include_etablissement? object.is_a?(Champs::SiretChamp) end @@ -43,6 +48,10 @@ class ChampSerializer < ActiveModel::Serializer object.is_a?(Champs::CarteChamp) end + def include_rows? + object.is_a?(Champs::RepetitionChamp) + end + private def legacy_type_de_champ diff --git a/app/serializers/row_serializer.rb b/app/serializers/row_serializer.rb new file mode 100644 index 000000000..b7aa2affd --- /dev/null +++ b/app/serializers/row_serializer.rb @@ -0,0 +1,5 @@ +class RowSerializer < ActiveModel::Serializer + has_many :champs, serializer: ChampSerializer + + attribute :index, key: :id +end diff --git a/app/services/procedure_export_v2_service.rb b/app/services/procedure_export_v2_service.rb index be5bf8ff0..2a14615fa 100644 --- a/app/services/procedure_export_v2_service.rb +++ b/app/services/procedure_export_v2_service.rb @@ -60,11 +60,7 @@ class ProcedureExportV2Service champs_repetables.map do |libelle, champs| [ libelle, - champs.flat_map do |champ| - 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 + champs.flat_map(&:rows_for_export) ] end end diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index 2ea28a353..955e2dac5 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -235,6 +235,22 @@ describe API::V1::DossiersController do it { expect(subject[:type_champ]).to eq('text') } 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 describe 'champs_private' do