2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2018-12-18 11:17:52 +01:00
|
|
|
|
class Champs::RepetitionChamp < Champ
|
2022-11-15 14:31:29 +01:00
|
|
|
|
accepts_nested_attributes_for :champs
|
2018-12-18 11:17:52 +01:00
|
|
|
|
|
|
|
|
|
def rows
|
2023-11-28 17:33:29 +01:00
|
|
|
|
dossier
|
2024-02-08 18:28:15 +01:00
|
|
|
|
.champs_for_revision(scope: type_de_champ)
|
2024-03-18 17:21:07 +01:00
|
|
|
|
.group_by(&:row_id)
|
|
|
|
|
.sort
|
|
|
|
|
.map(&:second)
|
2023-11-28 17:33:29 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def row_ids
|
|
|
|
|
rows.map { _1.first.row_id }
|
2018-12-18 11:17:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
2022-05-18 14:49:49 +02:00
|
|
|
|
def add_row(revision)
|
2022-04-28 15:06:40 +02:00
|
|
|
|
added_champs = []
|
2021-10-27 16:27:17 +02:00
|
|
|
|
transaction do
|
2022-12-16 12:39:51 +01:00
|
|
|
|
row_id = ULID.generate
|
2022-05-18 14:49:49 +02:00
|
|
|
|
revision.children_of(type_de_champ).each do |type_de_champ|
|
2023-02-02 09:59:03 +01:00
|
|
|
|
added_champs << type_de_champ.build_champ(row_id:)
|
2021-10-27 16:27:17 +02:00
|
|
|
|
end
|
2022-04-28 15:06:40 +02:00
|
|
|
|
self.champs << added_champs
|
2019-01-30 16:14:15 +01:00
|
|
|
|
end
|
2022-04-28 15:06:40 +02:00
|
|
|
|
added_champs
|
2019-01-30 16:14:15 +01:00
|
|
|
|
end
|
|
|
|
|
|
2024-04-15 15:06:05 +02:00
|
|
|
|
def remove_row(row_id)
|
|
|
|
|
dossier.champs.where(row_id:).destroy_all
|
|
|
|
|
dossier.champs.reload
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def focusable_input_id
|
|
|
|
|
rows.last&.first&.focusable_input_id
|
|
|
|
|
end
|
|
|
|
|
|
2021-07-21 10:45:59 +02:00
|
|
|
|
def blank?
|
2024-09-20 14:54:26 +02:00
|
|
|
|
row_ids.empty?
|
2019-01-30 16:14:15 +01:00
|
|
|
|
end
|
|
|
|
|
|
2018-12-18 11:17:52 +01:00
|
|
|
|
def search_terms
|
|
|
|
|
# The user cannot enter any information here so it doesn’t make much sense to search
|
|
|
|
|
end
|
2019-04-03 14:29:30 +02:00
|
|
|
|
|
2019-07-11 10:28:44 +02:00
|
|
|
|
def rows_for_export
|
2024-03-14 15:13:13 +01:00
|
|
|
|
row_ids.map.with_index(1) do |row_id, index|
|
2024-03-15 14:10:12 +01:00
|
|
|
|
Champs::RepetitionChamp::Row.new(index:, row_id:, dossier:)
|
2019-07-11 10:28:44 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-03 14:29:30 +02:00
|
|
|
|
class Row < Hashie::Dash
|
|
|
|
|
property :index
|
2023-11-28 17:33:29 +01:00
|
|
|
|
property :row_id
|
2024-03-15 14:10:12 +01:00
|
|
|
|
property :dossier
|
|
|
|
|
|
|
|
|
|
def dossier_id
|
|
|
|
|
dossier.id.to_s
|
|
|
|
|
end
|
2019-04-03 14:29:30 +02:00
|
|
|
|
|
2019-07-11 10:28:44 +02:00
|
|
|
|
def read_attribute_for_serialization(attribute)
|
|
|
|
|
self[attribute]
|
|
|
|
|
end
|
|
|
|
|
|
2021-10-22 12:18:43 +02:00
|
|
|
|
def spreadsheet_columns(types_de_champ)
|
2019-04-03 14:29:30 +02:00
|
|
|
|
[
|
|
|
|
|
['Dossier ID', :dossier_id],
|
|
|
|
|
['Ligne', :index]
|
2024-03-15 14:10:12 +01:00
|
|
|
|
] + dossier.champs_for_export(types_de_champ, row_id)
|
2019-04-03 14:29:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2018-12-18 11:17:52 +01:00
|
|
|
|
end
|