demarches-normaliennes/app/models/champs/repetition_champ.rb

74 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class Champs::RepetitionChamp < Champ
accepts_nested_attributes_for :champs
def rows
dossier
.champs_for_revision(scope: type_de_champ)
2024-03-18 17:21:07 +01:00
.group_by(&:row_id)
.sort
.map(&:second)
end
def row_ids
rows.map { _1.first.row_id }
end
def add_row(revision)
added_champs = []
transaction do
row_id = ULID.generate
revision.children_of(type_de_champ).each do |type_de_champ|
added_champs << type_de_champ.build_champ(row_id:)
end
self.champs << added_champs
2019-01-30 16:14:15 +01:00
end
added_champs
2019-01-30 16:14:15 +01:00
end
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?
row_ids.empty?
2019-01-30 16:14:15 +01:00
end
def search_terms
# The user cannot enter any information here so it doesnt 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
row_ids.map.with_index(1) do |row_id, index|
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
property :row_id
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
def spreadsheet_columns(types_de_champ)
2019-04-03 14:29:30 +02:00
[
['Dossier ID', :dossier_id],
['Ligne', :index]
] + dossier.champs_for_export(types_de_champ, row_id)
2019-04-03 14:29:30 +02:00
end
end
end