Skip to content

Commit

Permalink
refactor: remove champ factories
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Jul 12, 2024
1 parent fe37f1a commit c2c4c2b
Show file tree
Hide file tree
Showing 118 changed files with 1,137 additions and 1,533 deletions.
2 changes: 1 addition & 1 deletion app/controllers/administrateurs/procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def filter_procedures(filter)
procedures_result = procedures_result.where(aasm_state: filter.statuses) if filter.statuses.present?
procedures_result = procedures_result.where("tags @> ARRAY[?]::text[]", filter.tags) if filter.tags.present?
procedures_result = procedures_result.where(template: true) if filter.template?
procedures_result = procedures_result.where('published_at >= ?', filter.from_publication_date) if filter.from_publication_date.present?
procedures_result = procedures_result.where(published_at: filter.from_publication_date..) if filter.from_publication_date.present?
procedures_result = procedures_result.where(service: service) if filter.service_siret.present?
procedures_result = procedures_result.where(service: services) if services
procedures_result = procedures_result.where(for_individual: filter.for_individual) if filter.for_individual.present?
Expand Down
4 changes: 1 addition & 3 deletions app/graphql/loaders/champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def perform(keys)
private

def query(keys)
::Champ.where(@where)
.includes(:type_de_champ)
.where(types_de_champ: { stable_id: keys })
::Champ.where(@where).where(stable_id: keys)

Check warning on line 22 in app/graphql/loaders/champ.rb

View check run for this annotation

Codecov / codecov/patch

app/graphql/loaders/champ.rb#L22

Added line #L22 was not covered by tests
end
end
end
4 changes: 2 additions & 2 deletions app/jobs/migrations/backfill_dossier_repetition_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ def perform(dossier_ids)
.revision
.types_de_champ
.filter do |type_de_champ|
type_de_champ.type_champ == 'repetition' && dossier.champs.none? { _1.type_de_champ_id == type_de_champ.id }
type_de_champ.type_champ == 'repetition' && dossier.champs.none? { _1.stable_id == type_de_champ.stable_id }

Check warning on line 10 in app/jobs/migrations/backfill_dossier_repetition_job.rb

View check run for this annotation

Codecov / codecov/patch

app/jobs/migrations/backfill_dossier_repetition_job.rb#L10

Added line #L10 was not covered by tests
end
.each do |type_de_champ|
dossier.champs << type_de_champ.champ.build
dossier.champs << type_de_champ.build_champ

Check warning on line 13 in app/jobs/migrations/backfill_dossier_repetition_job.rb

View check run for this annotation

Codecov / codecov/patch

app/jobs/migrations/backfill_dossier_repetition_job.rb#L13

Added line #L13 was not covered by tests
end
end
end
Expand Down
69 changes: 0 additions & 69 deletions app/lib/recovery/align_champ_with_dossier_revision.rb

This file was deleted.

11 changes: 9 additions & 2 deletions app/models/champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ class Champ < ApplicationRecord
include ChampConditionalConcern
include ChampsValidateConcern

self.ignored_columns += [:type_de_champ_id]

belongs_to :dossier, inverse_of: false, touch: true, optional: false
belongs_to :type_de_champ, inverse_of: :champ, optional: false
belongs_to :parent, class_name: 'Champ', optional: true
has_many_attached :piece_justificative_file

Expand All @@ -15,6 +16,12 @@ class Champ < ApplicationRecord

delegate :procedure, to: :dossier

def type_de_champ
@type_de_champ ||= dossier.revision
.types_de_champ
.find(-> { raise "Type De Champ #{stable_id} not found in Revision #{dossier.revision_id}" }) { _1.stable_id == stable_id }
end

delegate :libelle,
:type_champ,
:description,
Expand Down Expand Up @@ -222,7 +229,7 @@ def update_with_external_data!(data:)
end

def clone(fork = false)
champ_attributes = [:parent_id, :private, :row_id, :type, :type_de_champ_id, :stable_id, :stream]
champ_attributes = [:parent_id, :private, :row_id, :type, :stable_id, :stream]
value_attributes = fork || !private? ? [:value, :value_json, :data, :external_id] : []
relationships = fork || !private? ? [:etablissement, :geo_areas] : []

Expand Down
6 changes: 2 additions & 4 deletions app/models/concerns/champs_validate_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ def valid_champ_value?
private

def validate_champ_value?
return false unless visible?

case validation_context
when :champs_public_value
public?
public? && visible?
when :champs_private_value
private?
private? && visible?
else
false
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/dossier_champs_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def champ_with_attributes_for_update(type_de_champ, row_id, updated_by:)
attributes = type_de_champ.params_for_champ
# TODO: Once we have the right index in place, we should change this to use `create_or_find_by` instead of `find_or_create_by`
champ = champs
.create_with(type_de_champ:, **attributes)
.create_with(**attributes)
.find_or_create_by!(stable_id: type_de_champ.stable_id, row_id:)

attributes[:id] = champ.id
Expand All @@ -113,7 +113,7 @@ def champ_with_attributes_for_update(type_de_champ, row_id, updated_by:)

parent = revision.parent_of(type_de_champ)
if parent.present?
attributes[:parent] = champs.find { _1.type_de_champ_id == parent.id }
attributes[:parent] = champs.find { _1.stable_id == parent.stable_id }
else
attributes[:parent] = nil
end
Expand Down
13 changes: 2 additions & 11 deletions app/models/concerns/dossier_rebase_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ def rebase
# index published types de champ coordinates by stable_id
target_coordinates_by_stable_id = target_revision
.revision_types_de_champ
.includes(:type_de_champ, :parent)
.includes(:parent)
.index_by(&:stable_id)

changes_by_op = pending_changes
.group_by(&:op)
.tap { _1.default = [] }

champs_by_stable_id = champs
.includes(:type_de_champ)
.group_by(&:stable_id)
.transform_values { Champ.where(id: _1) }
.tap { _1.default = Champ.none }
Expand All @@ -78,14 +77,6 @@ def rebase
# update champ
changes_by_op[:update].each { apply(_1, champs_by_stable_id[_1.stable_id]) }

# due to repetition tdc clone on update or erase
# we must reassign tdc to the latest version
champs_by_stable_id.each do |stable_id, champs|
if target_coordinates_by_stable_id[stable_id].present? && champs.present?
champs.update_all(type_de_champ_id: target_coordinates_by_stable_id[stable_id].type_de_champ_id)
end
end

# update dossier revision
update_column(:revision_id, target_revision.id)
end
Expand Down Expand Up @@ -134,7 +125,7 @@ def add_new_champs_for_revision(target_coordinate)
champ_repetition.champs.map(&:row_id).uniq.each do |row_id|
champs << create_champ(target_coordinate, champ_repetition, row_id:)
end
elsif champ_repetition.mandatory?
elsif target_coordinate.parent.mandatory?
champs << create_champ(target_coordinate, champ_repetition, row_id: ULID.generate)
end
end
Expand Down
15 changes: 5 additions & 10 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,7 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon
scope :hidden_by_administration_since, -> (since) { where('dossiers.hidden_by_administration_at IS NOT NULL AND dossiers.hidden_by_administration_at >= ?', since) }
scope :hidden_since, -> (since) { hidden_by_user_since(since).or(hidden_by_administration_since(since)) }

scope :with_type_de_champ, -> (stable_id) {
joins('INNER JOIN champs ON champs.dossier_id = dossiers.id INNER JOIN types_de_champ ON types_de_champ.id = champs.type_de_champ_id')
.where(types_de_champ: { stable_id: })
}
scope :with_type_de_champ, -> (stable_id) { joins(:champs).where(champs: { stream: 'main', stable_id: }) }

scope :all_state, -> { not_archived.state_not_brouillon }
scope :en_construction, -> { not_archived.state_en_construction }
Expand All @@ -271,20 +268,18 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon
scope :with_followers, -> { left_outer_joins(:follows).where.not(follows: { id: nil }) }
scope :with_champs, -> {
includes(champs_public: [
:type_de_champ,
:geo_areas,
piece_justificative_file_attachments: :blob,
champs: [:type_de_champ, piece_justificative_file_attachments: :blob]
champs: [piece_justificative_file_attachments: :blob]
])
}

scope :brouillons_recently_updated, -> { updated_since(2.days.ago).state_brouillon.order_by_updated_at }
scope :with_annotations, -> {
includes(champs_private: [
:type_de_champ,
:geo_areas,
piece_justificative_file_attachments: :blob,
champs: [:type_de_champ, piece_justificative_file_attachments: :blob]
champs: [piece_justificative_file_attachments: :blob]
])
}
scope :for_api, -> {
Expand Down Expand Up @@ -493,10 +488,10 @@ def motivation
end

def build_default_champs_for_new_dossier
revision.build_champs_public.each do |champ|
revision.build_champs_public(self).each do |champ|
champs_public << champ
end
revision.build_champs_private.each do |champ|
revision.build_champs_private(self).each do |champ|
champs_private << champ
end
champs_public.filter { _1.repetition? && _1.mandatory? }.each do |champ|
Expand Down
40 changes: 20 additions & 20 deletions app/models/dossier_preloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def in_batches(size = DEFAULT_BATCH_SIZE)

def in_batches_with_block(size = DEFAULT_BATCH_SIZE, &block)
@dossiers.in_batches(of: size) do |batch|
data = Dossier.where(id: batch.ids).includes(:individual, :traitement, :etablissement, user: :france_connect_informations, avis: :expert, commentaires: [:instructeur, :expert], revision: :revision_types_de_champ)
data = Dossier.where(id: batch.ids).includes(:individual, :traitement, :etablissement, user: :france_connect_informations, avis: :expert, commentaires: [:instructeur, :expert])

dossiers = data.to_a
load_dossiers(dossiers)
Expand All @@ -30,39 +30,32 @@ def all(pj_template: false)
end

def self.load_one(dossier, pj_template: false)
DossierPreloader.new([dossier]).all(pj_template: pj_template).first
DossierPreloader.new([dossier]).all(pj_template:).first
end

private

# returns: { revision_id : { type_de_champ_id : position } }
def revisions
@revisions ||= ProcedureRevision.where(id: @dossiers.pluck(:revision_id).uniq)
.includes(types_de_champ: { piece_justificative_template_attachment: :blob })
.index_by(&:id)
end

# returns: { revision_id : { stable_id : position } }
def positions
@positions ||= ProcedureRevisionTypeDeChamp
.where(revision_id: @dossiers.pluck(:revision_id).uniq)
.select(:revision_id, :type_de_champ_id, :position)
.group_by(&:revision_id)
.transform_values do |coordinates|
coordinates.index_by(&:type_de_champ_id).transform_values(&:position)
end
@positions ||= revisions
.transform_values { |revision| revision.revision_types_de_champ.map { [_1.stable_id, _1.position] }.to_h }
end

def load_dossiers(dossiers, pj_template: false)
to_include = @includes_for_champ.dup
to_include << [piece_justificative_file_attachments: :blob]

if pj_template
to_include << { type_de_champ: { piece_justificative_template_attachment: :blob } }
else
to_include << :type_de_champ
end

all_champs = Champ
.includes(to_include)
.where(dossier_id: dossiers)
.to_a

load_etablissements(all_champs)

children_champs, root_champs = all_champs.partition(&:child?)
champs_by_dossier = root_champs.group_by(&:dossier_id)
champs_by_dossier_by_parent = children_champs
Expand All @@ -74,6 +67,8 @@ def load_dossiers(dossiers, pj_template: false)
dossiers.each do |dossier|
load_dossier(dossier, champs_by_dossier[dossier.id] || [], champs_by_dossier_by_parent[dossier.id] || {})
end

load_etablissements(all_champs)
end

def load_etablissements(champs)
Expand All @@ -90,6 +85,11 @@ def load_etablissements(champs)
end

def load_dossier(dossier, champs, children_by_parent = {})
revision = revisions[dossier.revision_id]
if revision.present?
dossier.association(:revision).target = revision
end

champs_public, champs_private = champs.partition(&:public?)

dossier.association(:champs).target = []
Expand Down Expand Up @@ -121,8 +121,8 @@ def load_champs(parent, name, champs, dossier, children_by_parent)
dossier.association(:champs).target += champs

parent.association(name).target = champs
.filter { positions[dossier.revision_id][_1.type_de_champ_id].present? }
.sort_by { [_1.row_id, positions[dossier.revision_id][_1.type_de_champ_id]] }
.filter { positions[dossier.revision_id][_1.stable_id].present? }
.sort_by { [_1.row_id, positions[dossier.revision_id][_1.stable_id]] }

# Load children champs
champs.filter(&:block?).each do |parent_champ|
Expand Down
1 change: 0 additions & 1 deletion app/models/logic/champ_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def compute(champs)
return nil if !targeted_champ.visible?
return nil if targeted_champ.blank? & !targeted_champ.drop_down_other?

# on dépense 22ms ici, à cause du map, mais on doit pouvoir passer par un champ type
case targeted_champ.type
when "Champs::YesNoChamp",
"Champs::CheckboxChamp"
Expand Down
8 changes: 4 additions & 4 deletions app/models/procedure_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class ProcedureRevision < ApplicationRecord

serialize :ineligibilite_rules, LogicSerializer

def build_champs_public
def build_champs_public(dossier)
# reload: it can be out of sync in test if some tdcs are added wihtout using add_tdc
types_de_champ_public.reload.map(&:build_champ)
types_de_champ_public.reload.map { _1.build_champ(dossier:) }
end

def build_champs_private
def build_champs_private(dossier)
# reload: it can be out of sync in test if some tdcs are added wihtout using add_tdc
types_de_champ_private.reload.map(&:build_champ)
types_de_champ_private.reload.map { _1.build_champ(dossier:) }
end

def add_type_de_champ(params)
Expand Down
Loading

0 comments on commit c2c4c2b

Please sign in to comment.