perf(dossier): memoize champ.sections on dossier

This commit is contained in:
Paul Chavard 2022-03-10 19:22:07 +01:00
parent 51af70639b
commit c60a8970f1
3 changed files with 36 additions and 19 deletions

View file

@ -81,18 +81,8 @@ class Champ < ApplicationRecord
!private?
end
def siblings
if parent
parent&.champs
elsif public?
dossier&.champs
else
dossier&.champs_private
end
end
def sections
siblings&.filter(&:header_section?)
@sections ||= dossier.sections_for(self)
end
def mandatory_and_blank?

View file

@ -1122,6 +1122,20 @@ class Dossier < ApplicationRecord
termine_expired_to_delete.find_each(&:purge_discarded)
end
def sections_for(champ)
@sections = Hash.new do |hash, parent|
case parent
when :public
hash[parent] = champs.filter(&:header_section?)
when :private
hash[parent] = champs_private.filter(&:header_section?)
else
hash[parent] = parent.champs.filter(&:header_section?)
end
end
@sections[champ.parent || (champ.public? ? :public : :private)]
end
private
def create_missing_traitemets