refactor: do not query for already load dossier table

This commit is contained in:
simon lehericey 2024-11-21 12:05:47 +01:00
parent bb198000fe
commit 18a4ee450f
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5

View file

@ -30,11 +30,13 @@ class DossierProjectionService
# - the order of the intermediary query results are unknown # - the order of the intermediary query results are unknown
# - some values can be missing (if a revision added or removed them) # - some values can be missing (if a revision added or removed them)
def self.project(dossiers_ids, columns) def self.project(dossiers_ids, columns)
dossiers = Dossier.includes(:corrections, :pending_corrections).find(dossiers_ids)
fields = columns.map do |c| fields = columns.map do |c|
if c.is_a?(Columns::ChampColumn) if c.is_a?(Columns::ChampColumn)
{ TABLE => c.table, STABLE_ID => c.stable_id, original_column: c } { TABLE => c.table, STABLE_ID => c.stable_id, original_column: c }
else else
{ TABLE => c.table, COLUMN => c.column } { TABLE => c.table, COLUMN => c.column, original_column: c }
end end
end end
champ_value = champ_value_formatter(dossiers_ids, fields) champ_value = champ_value_formatter(dossiers_ids, fields)
@ -61,13 +63,11 @@ class DossierProjectionService
end end
end end
when 'self' when 'self'
Dossier dossiers.each do |dossier|
.where(id: dossiers_ids) fields.each do |field|
.pluck(:id, *fields.map { |f| f[COLUMN].to_sym }) column = field[:original_column]
.each do |id, *columns| value = column.value(dossier)
fields.zip(columns).each do |field, value| field[:id_value_h][dossier.id] = if value.respond_to?(:strftime)
# SVA must remain a date: in other column we compute remaining delay with it
field[:id_value_h][id] = if value.respond_to?(:strftime)
I18n.l(value.to_date) I18n.l(value.to_date)
else else
value value
@ -149,8 +149,6 @@ class DossierProjectionService
end end
end end
dossiers = Dossier.includes(:corrections, :pending_corrections).find(dossiers_ids)
dossiers_ids.map do |dossier_id| dossiers_ids.map do |dossier_id|
DossierProjection.new( DossierProjection.new(
dossiers.find { _1.id == dossier_id }, dossiers.find { _1.id == dossier_id },