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

60 lines
1.3 KiB
Ruby
Raw Normal View History

class Champs::RepetitionChamp < Champ
accepts_nested_attributes_for :champs
delegate :libelle_for_export, to: :type_de_champ
def rows
champs.group_by(&:row_id).values
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
2021-07-21 10:45:59 +02:00
def blank?
champs.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
def for_tag
([libelle] + rows.map do |champs|
champs.map do |champ|
"#{champ.libelle} : #{champ}"
end.join("\n")
end).join("\n\n")
end
2019-07-11 10:28:44 +02:00
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
2019-04-03 14:29:30 +02:00
class Row < Hashie::Dash
property :index
property :dossier_id
property :champs
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(champs, types_de_champ)
2019-04-03 14:29:30 +02:00
end
end
end