Add repetitions to api
This commit is contained in:
parent
127a4d2ffb
commit
b266915892
7 changed files with 51 additions and 10 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -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: [],
|
||||
|
|
|
@ -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
|
||||
|
|
5
app/serializers/row_serializer.rb
Normal file
5
app/serializers/row_serializer.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class RowSerializer < ActiveModel::Serializer
|
||||
has_many :champs, serializer: ChampSerializer
|
||||
|
||||
attribute :index, key: :id
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue