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

92 lines
2.4 KiB
Ruby
Raw Normal View History

2020-08-06 16:35:45 +02:00
# == Schema Information
#
# Table name: champs
#
# id :integer not null, primary key
# data :jsonb
# fetch_external_data_exceptions :string is an Array
# private :boolean default(FALSE), not null
# rebased_at :datetime
# row :integer
# type :string
# value :string
2021-10-05 15:37:13 +02:00
# value_json :jsonb
# created_at :datetime
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# type_de_champ_id :integer
2020-08-06 16:35:45 +02:00
#
class Champs::RepetitionChamp < Champ
accepts_nested_attributes_for :champs, allow_destroy: true
def rows
champs.group_by(&:row).values
end
def add_row
transaction do
row = (blank? ? -1 : champs.last.row) + 1
type_de_champ.types_de_champ.each do |type_de_champ|
self.champs << type_de_champ.champ.build(row: row)
end
2019-01-30 16:14:15 +01:00
end
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
# We have to truncate the label here as spreadsheets have a (30 char) limit on length.
def libelle_for_export
2020-04-30 15:49:31 +02:00
str = "(#{stable_id}) #{libelle}"
# /\*?[] are invalid Excel worksheet characters
ActiveStorage::Filename.new(str.delete('[]*?')).sanitized
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
2019-04-03 14:29:30 +02:00
def spreadsheet_columns
[
['Dossier ID', :dossier_id],
['Ligne', :index]
] + exported_champs
end
private
def exported_champs
champs.reject(&:exclude_from_export?).map do |champ|
[champ.libelle, champ.for_export]
end
end
end
end