refactor(champ): make row_id a named param

This commit is contained in:
Paul Chavard 2024-12-09 09:36:27 +01:00
parent 3556fcc11e
commit 10333908c3
No known key found for this signature in database
21 changed files with 51 additions and 51 deletions

View file

@ -17,7 +17,7 @@ class EditableChamp::SectionComponent < ApplicationComponent
def header_section def header_section
node = @nodes.first node = @nodes.first
@dossier.project_champ(node, @row_id) if node.is_a?(TypeDeChamp) && node.header_section? @dossier.project_champ(node, row_id: @row_id) if node.is_a?(TypeDeChamp) && node.header_section?
end end
def splitted_tail def splitted_tail
@ -40,7 +40,7 @@ class EditableChamp::SectionComponent < ApplicationComponent
when EditableChamp::SectionComponent when EditableChamp::SectionComponent
[node, nil] [node, nil]
else else
[nil, @dossier.project_champ(node, @row_id)] [nil, @dossier.project_champ(node, row_id: @row_id)]
end end
end end

View file

@ -18,11 +18,11 @@ class ViewableChamp::SectionComponent < ApplicationComponent
def header_section def header_section
node = @nodes.first node = @nodes.first
@dossier.project_champ(node, @row_id) if node.is_a?(TypeDeChamp) && node.header_section? @dossier.project_champ(node, row_id: @row_id) if node.is_a?(TypeDeChamp) && node.header_section?
end end
def champs def champs
tail.filter_map { _1.is_a?(TypeDeChamp) ? @dossier.project_champ(_1, @row_id) : nil } tail.filter_map { _1.is_a?(TypeDeChamp) ? @dossier.project_champ(_1, row_id: @row_id) : nil }
end end
def sections def sections

View file

@ -10,9 +10,9 @@ class Champs::ChampController < ApplicationController
dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id]) dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id])
type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id]) type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id])
if type_de_champ.repetition? if type_de_champ.repetition?
dossier.project_champ(type_de_champ, nil) dossier.project_champ(type_de_champ)
else else
dossier.champ_for_update(type_de_champ, params_row_id, updated_by: current_user.email) dossier.champ_for_update(type_de_champ, row_id: params_row_id, updated_by: current_user.email)
end end
end end

View file

@ -321,7 +321,7 @@ module Instructeurs
def annotation def annotation
@dossier = dossier_with_champs(pj_template: false) @dossier = dossier_with_champs(pj_template: false)
type_de_champ = @dossier.find_type_de_champ_by_stable_id(params[:stable_id], :private) type_de_champ = @dossier.find_type_de_champ_by_stable_id(params[:stable_id], :private)
annotation = @dossier.project_champ(type_de_champ, params[:row_id]) annotation = @dossier.project_champ(type_de_champ, row_id: params[:row_id])
respond_to do |format| respond_to do |format|
format.turbo_stream do format.turbo_stream do

View file

@ -299,7 +299,7 @@ module Users
def champ def champ
@dossier = dossier_with_champs(pj_template: false) @dossier = dossier_with_champs(pj_template: false)
type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id], :public) type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id], :public)
champ = dossier.project_champ(type_de_champ, params[:row_id]) champ = dossier.project_champ(type_de_champ, row_id: params[:row_id])
respond_to do |format| respond_to do |format|
format.turbo_stream do format.turbo_stream do

View file

@ -46,7 +46,7 @@ module Mutations
.find_by(type_champ: annotation_type_champ, stable_id:) .find_by(type_champ: annotation_type_champ, stable_id:)
return nil if type_de_champ.nil? return nil if type_de_champ.nil?
dossier.champ_for_update(type_de_champ, row_id, updated_by: current_administrateur.email) dossier.champ_for_update(type_de_champ, row_id:, updated_by: current_administrateur.email)
end end
def annotation_type_champ def annotation_type_champ

View file

@ -34,7 +34,7 @@ module Mutations
.find_by(type_champ: TypeDeChamp.type_champs.fetch(:repetition), stable_id:) .find_by(type_champ: TypeDeChamp.type_champs.fetch(:repetition), stable_id:)
return nil if type_de_champ.nil? return nil if type_de_champ.nil?
dossier.project_champ(type_de_champ, nil) dossier.project_champ(type_de_champ)
end end
end end
end end

View file

@ -92,7 +92,7 @@ class AttestationTemplate < ApplicationRecord
used_tags.filter_map do |used_tag| used_tags.filter_map do |used_tag|
corresponding_type_de_champ = types_de_champ_by_tag_id[used_tag] corresponding_type_de_champ = types_de_champ_by_tag_id[used_tag]
if corresponding_type_de_champ && dossier.project_champ(corresponding_type_de_champ, nil).blank? if corresponding_type_de_champ && dossier.project_champ(corresponding_type_de_champ).blank?
corresponding_type_de_champ corresponding_type_de_champ
end end
end end

View file

@ -3,7 +3,7 @@
module DossierChampsConcern module DossierChampsConcern
extend ActiveSupport::Concern extend ActiveSupport::Concern
def project_champ(type_de_champ, row_id) def project_champ(type_de_champ, row_id: nil)
check_valid_row_id_on_read?(type_de_champ, row_id) check_valid_row_id_on_read?(type_de_champ, row_id)
champ = champs_by_public_id[type_de_champ.public_id(row_id)] champ = champs_by_public_id[type_de_champ.public_id(row_id)]
if champ.nil? || !champ.is_type?(type_de_champ.type_champ) if champ.nil? || !champ.is_type?(type_de_champ.type_champ)
@ -17,11 +17,11 @@ module DossierChampsConcern
end end
def project_champs_public def project_champs_public
@project_champs_public ||= revision.types_de_champ_public.map { project_champ(_1, nil) } @project_champs_public ||= revision.types_de_champ_public.map { project_champ(_1) }
end end
def project_champs_private def project_champs_private
@project_champs_private ||= revision.types_de_champ_private.map { project_champ(_1, nil) } @project_champs_private ||= revision.types_de_champ_private.map { project_champ(_1) }
end end
def filled_champs_public def filled_champs_public
@ -54,7 +54,7 @@ module DossierChampsConcern
def project_champs_public_all def project_champs_public_all
revision.types_de_champ_public.flat_map do |type_de_champ| revision.types_de_champ_public.flat_map do |type_de_champ|
champ = project_champ(type_de_champ, nil) champ = project_champ(type_de_champ)
if type_de_champ.repetition? if type_de_champ.repetition?
[champ] + project_rows_for(type_de_champ).flatten [champ] + project_rows_for(type_de_champ).flatten
else else
@ -65,7 +65,7 @@ module DossierChampsConcern
def project_champs_private_all def project_champs_private_all
revision.types_de_champ_private.flat_map do |type_de_champ| revision.types_de_champ_private.flat_map do |type_de_champ|
champ = project_champ(type_de_champ, nil) champ = project_champ(type_de_champ)
if type_de_champ.repetition? if type_de_champ.repetition?
[champ] + project_rows_for(type_de_champ).flatten [champ] + project_rows_for(type_de_champ).flatten
else else
@ -81,7 +81,7 @@ module DossierChampsConcern
row_ids = repetition_row_ids(type_de_champ) row_ids = repetition_row_ids(type_de_champ)
row_ids.map do |row_id| row_ids.map do |row_id|
children.map { project_champ(_1, row_id) } children.map { project_champ(_1, row_id:) }
end end
end end
@ -101,19 +101,19 @@ module DossierChampsConcern
.types_de_champ .types_de_champ
.filter { _1.stable_id.in?(stable_ids) } .filter { _1.stable_id.in?(stable_ids) }
.filter { !_1.child?(revision) } .filter { !_1.child?(revision) }
.map { _1.repetition? ? project_champ(_1, nil) : champ_for_update(_1, nil, updated_by: nil) } .map { _1.repetition? ? project_champ(_1) : champ_for_update(_1, updated_by: nil) }
end end
def champ_value_for_tag(type_de_champ, path = :value) def champ_value_for_tag(type_de_champ, path = :value)
champ = if type_de_champ.repetition? champ = if type_de_champ.repetition?
project_champ(type_de_champ, nil) project_champ(type_de_champ)
else else
filled_champ(type_de_champ, nil) filled_champ(type_de_champ)
end end
type_de_champ.champ_value_for_tag(champ, path) type_de_champ.champ_value_for_tag(champ, path)
end end
def champ_for_update(type_de_champ, row_id, updated_by:) def champ_for_update(type_de_champ, row_id: nil, updated_by:)
champ = champ_upsert_by!(type_de_champ, row_id) champ = champ_upsert_by!(type_de_champ, row_id)
champ.updated_by = updated_by champ.updated_by = updated_by
champ champ
@ -155,7 +155,7 @@ module DossierChampsConcern
raise "Can't add row to non-repetition type de champ" if !type_de_champ.repetition? raise "Can't add row to non-repetition type de champ" if !type_de_champ.repetition?
row_id = ULID.generate row_id = ULID.generate
champ = champ_for_update(type_de_champ, row_id, updated_by:) champ = champ_for_update(type_de_champ, row_id:, updated_by:)
champ.save! champ.save!
reset_champ_cache(champ) reset_champ_cache(champ)
row_id row_id
@ -164,7 +164,7 @@ module DossierChampsConcern
def repetition_remove_row(type_de_champ, row_id, updated_by:) def repetition_remove_row(type_de_champ, row_id, updated_by:)
raise "Can't remove row from non-repetition type de champ" if !type_de_champ.repetition? raise "Can't remove row from non-repetition type de champ" if !type_de_champ.repetition?
champ = champ_for_update(type_de_champ, row_id, updated_by:) champ = champ_for_update(type_de_champ, row_id:, updated_by:)
champ.discard! champ.discard!
reset_champ_cache(champ) reset_champ_cache(champ)
end end
@ -191,7 +191,7 @@ module DossierChampsConcern
champs.filter { stable_id_in_revision?(_1.stable_id) } champs.filter { stable_id_in_revision?(_1.stable_id) }
end end
def filled_champ(type_de_champ, row_id) def filled_champ(type_de_champ, row_id: nil)
champ = champs_by_public_id[type_de_champ.public_id(row_id)] champ = champs_by_public_id[type_de_champ.public_id(row_id)]
if type_de_champ.champ_blank?(champ) || !champ.visible? if type_de_champ.champ_blank?(champ) || !champ.visible?
nil nil

View file

@ -175,10 +175,10 @@ module DossierCloneConcern
end end
added_row_ids.each do |row_id, repetition_type_de_champ| added_row_ids.each do |row_id, repetition_type_de_champ|
champ_for_update(repetition_type_de_champ, row_id, updated_by: user.email).save! champ_for_update(repetition_type_de_champ, row_id:, updated_by: user.email).save!
end end
removed_row_ids.each do |row_id, repetition_type_de_champ| removed_row_ids.each do |row_id, repetition_type_de_champ|
champ_for_update(repetition_type_de_champ, row_id, updated_by: user.email).discard! champ_for_update(repetition_type_de_champ, row_id:, updated_by: user.email).discard!
end end
end end
end end

View file

@ -17,7 +17,7 @@ module DossierExportConcern
def champ_values_for_export(types_de_champ, row_id: nil, export_template: nil, format:) def champ_values_for_export(types_de_champ, row_id: nil, export_template: nil, format:)
types_de_champ.flat_map do |type_de_champ| types_de_champ.flat_map do |type_de_champ|
champ = filled_champ(type_de_champ, row_id) champ = filled_champ(type_de_champ, row_id:)
if export_template.present? if export_template.present?
export_template export_template
.columns_for_stable_id(type_de_champ.stable_id) .columns_for_stable_id(type_de_champ.stable_id)

View file

@ -33,7 +33,7 @@ module DossierSectionsConcern
return "#{index}.#{index_in_repetition + 1}" if index_in_repetition return "#{index}.#{index_in_repetition + 1}" if index_in_repetition
else else
return index if tdc.stable_id == type_de_champ.stable_id return index if tdc.stable_id == type_de_champ.stable_id
next unless project_champ(tdc, nil).visible? next unless project_champ(tdc).visible?
index += 1 if tdc.header_section? index += 1 if tdc.header_section?
end end

View file

@ -65,7 +65,7 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
type_de_champ = revision.types_de_champ.find { _1.stable_id == stable_id } type_de_champ = revision.types_de_champ.find { _1.stable_id == stable_id }
next unless type_de_champ next unless type_de_champ
subchamp = champ.dossier.champ_for_update(type_de_champ, row_id, updated_by: nil) subchamp = champ.dossier.champ_for_update(type_de_champ, row_id:, updated_by: nil)
TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ, revision).to_assignable_attributes(subchamp, value) TypesDeChamp::PrefillTypeDeChamp.build(subchamp.type_de_champ, revision).to_assignable_attributes(subchamp, value)
end.compact end.compact
end end

View file

@ -155,13 +155,13 @@ describe Columns::ChampColumn do
def expect_type_de_champ_values(type, assertion) def expect_type_de_champ_values(type, assertion)
type_de_champ = types_de_champ.find { _1.type_champ == type } type_de_champ = types_de_champ.find { _1.type_champ == type }
champ = dossier.send(:filled_champ, type_de_champ, nil) champ = dossier.send(:filled_champ, type_de_champ)
columns = type_de_champ.columns(procedure:) columns = type_de_champ.columns(procedure:)
expect(columns.map { _1.value(champ) }).to assertion expect(columns.map { _1.value(champ) }).to assertion
end end
def retrieve_champ(type) def retrieve_champ(type)
type_de_champ = types_de_champ.find { _1.type_champ == type } type_de_champ = types_de_champ.find { _1.type_champ == type }
dossier.send(:filled_champ, type_de_champ, nil) dossier.send(:filled_champ, type_de_champ)
end end
end end

View file

@ -36,13 +36,13 @@ RSpec.describe DossierChampsConcern do
context "public champ" do context "public champ" do
let(:row_id) { nil } let(:row_id) { nil }
subject { dossier.project_champ(type_de_champ_public, row_id) } subject { dossier.project_champ(type_de_champ_public, row_id:) }
it { expect(subject.persisted?).to be_truthy } it { expect(subject.persisted?).to be_truthy }
context "in repetition" do context "in repetition" do
let(:type_de_champ_public) { dossier.find_type_de_champ_by_stable_id(994) } let(:type_de_champ_public) { dossier.find_type_de_champ_by_stable_id(994) }
let(:row_id) { dossier.project_champ(type_de_champ_repetition, nil).row_ids.first } let(:row_id) { dossier.project_champ(type_de_champ_repetition).row_ids.first }
it { it {
expect(subject.new_record?).to be_truthy expect(subject.new_record?).to be_truthy
@ -88,7 +88,7 @@ RSpec.describe DossierChampsConcern do
end end
context "private champ" do context "private champ" do
subject { dossier.project_champ(type_de_champ_private, nil) } subject { dossier.project_champ(type_de_champ_private) }
it { expect(subject.persisted?).to be_truthy } it { expect(subject.persisted?).to be_truthy }
@ -225,7 +225,7 @@ RSpec.describe DossierChampsConcern do
let(:row_id) { nil } let(:row_id) { nil }
context "public champ" do context "public champ" do
subject { dossier.champ_for_update(type_de_champ_public, row_id, updated_by: dossier.user.email) } subject { dossier.champ_for_update(type_de_champ_public, row_id:, updated_by: dossier.user.email) }
it { it {
expect(subject.persisted?).to be_truthy expect(subject.persisted?).to be_truthy
@ -283,7 +283,7 @@ RSpec.describe DossierChampsConcern do
end end
context "private champ" do context "private champ" do
subject { dossier.champ_for_update(type_de_champ_private, row_id, updated_by: dossier.user.email) } subject { dossier.champ_for_update(type_de_champ_private, row_id:, updated_by: dossier.user.email) }
it { it {
expect(subject.persisted?).to be_truthy expect(subject.persisted?).to be_truthy
@ -304,9 +304,9 @@ RSpec.describe DossierChampsConcern do
} }
end end
let(:champ_99) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(99), nil) } let(:champ_99) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(99)) }
let(:champ_991) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(991), nil) } let(:champ_991) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(991)) }
let(:champ_994) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(994), row_id) } let(:champ_994) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(994), row_id:) }
subject { dossier.update_champs_attributes(attributes, :public, updated_by: dossier.user.email) } subject { dossier.update_champs_attributes(attributes, :public, updated_by: dossier.user.email) }
@ -365,7 +365,7 @@ RSpec.describe DossierChampsConcern do
} }
end end
let(:annotation_995) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(995), nil) } let(:annotation_995) { dossier.project_champ(dossier.find_type_de_champ_by_stable_id(995)) }
subject { dossier.update_champs_attributes(attributes, :private, updated_by: dossier.user.email) } subject { dossier.update_champs_attributes(attributes, :private, updated_by: dossier.user.email) }

View file

@ -368,13 +368,13 @@ RSpec.describe DossierCloneConcern do
context 'with new revision' do context 'with new revision' do
let(:added_champ) { let(:added_champ) {
tdc = forked_dossier.revision.types_de_champ.find { _1.libelle == "Un nouveau champ text" } tdc = forked_dossier.revision.types_de_champ.find { _1.libelle == "Un nouveau champ text" }
forked_dossier.champ_for_update(tdc, nil, updated_by: 'test') forked_dossier.champ_for_update(tdc, updated_by: 'test')
} }
let(:added_repetition_champ) { let(:added_repetition_champ) {
tdc_repetition = forked_dossier.revision.types_de_champ.find { _1.stable_id == 993 } tdc_repetition = forked_dossier.revision.types_de_champ.find { _1.stable_id == 993 }
tdc = forked_dossier.revision.types_de_champ.find { _1.libelle == "Texte en répétition" } tdc = forked_dossier.revision.types_de_champ.find { _1.libelle == "Texte en répétition" }
row_id = forked_dossier.repetition_row_ids(tdc_repetition).first row_id = forked_dossier.repetition_row_ids(tdc_repetition).first
forked_dossier.champ_for_update(tdc, row_id, updated_by: 'test') forked_dossier.champ_for_update(tdc, row_id:, updated_by: 'test')
} }
let(:removed_champ) { dossier.champs.find { _1.stable_id == 99 } } let(:removed_champ) { dossier.champs.find { _1.stable_id == 99 } }
let(:updated_champ) { dossier.champs.find { _1.stable_id == 991 } } let(:updated_champ) { dossier.champs.find { _1.stable_id == 991 } }

View file

@ -72,7 +72,7 @@ describe DossierSectionsConcern do
context "when there are invisible sections" do context "when there are invisible sections" do
it "index accordingly header sections" do it "index accordingly header sections" do
expect(dossier.index_for_section_header(headers[0])).to eq(1) expect(dossier.index_for_section_header(headers[0])).to eq(1)
expect(dossier.project_champ(headers[1], nil)).not_to be_visible expect(dossier.project_champ(headers[1])).not_to be_visible
expect(dossier.index_for_section_header(headers[2])).to eq(2) expect(dossier.index_for_section_header(headers[2])).to eq(2)
end end
end end
@ -81,7 +81,7 @@ describe DossierSectionsConcern do
let(:number_value) { 5 } let(:number_value) { 5 }
it "index accordingly header sections" do it "index accordingly header sections" do
expect(dossier.index_for_section_header(headers[0])).to eq(1) expect(dossier.index_for_section_header(headers[0])).to eq(1)
expect(dossier.project_champ(headers[1], nil)).to be_visible expect(dossier.project_champ(headers[1])).to be_visible
expect(dossier.index_for_section_header(headers[1])).to eq(2) expect(dossier.index_for_section_header(headers[1])).to eq(2)
expect(dossier.index_for_section_header(headers[2])).to eq(3) expect(dossier.index_for_section_header(headers[2])).to eq(3)
end end

View file

@ -258,7 +258,7 @@ describe TagsSubstitutionConcern, type: :model do
context 'and the champ has a primary value' do context 'and the champ has a primary value' do
before do before do
dossier.champ_for_update(type_de_champ, nil, updated_by: 'test').update(primary_value: 'primo') dossier.champ_for_update(type_de_champ, updated_by: 'test').update(primary_value: 'primo')
dossier.reload dossier.reload
end end
@ -266,7 +266,7 @@ describe TagsSubstitutionConcern, type: :model do
context 'and the champ has a secondary value' do context 'and the champ has a secondary value' do
before do before do
dossier.champ_for_update(type_de_champ, nil, updated_by: 'test').update(secondary_value: 'secundo') dossier.champ_for_update(type_de_champ, updated_by: 'test').update(secondary_value: 'secundo')
dossier.reload dossier.reload
end end

View file

@ -17,7 +17,7 @@ describe TypeDeChamp do
it do it do
dossier.revision.types_de_champ_public.each do |type_de_champ| dossier.revision.types_de_champ_public.each do |type_de_champ|
champ = dossier.project_champ(type_de_champ, nil) champ = dossier.project_champ(type_de_champ)
expect(type_de_champ.dynamic_type.class.name).to match(/^TypesDeChamp::/) expect(type_de_champ.dynamic_type.class.name).to match(/^TypesDeChamp::/)
expect(champ.class.name).to match(/^Champs::/) expect(champ.class.name).to match(/^Champs::/)
end end

View file

@ -127,7 +127,7 @@ RSpec.configure do |config|
module SpecHelpers module SpecHelpers
def champ_for_update(champ) def champ_for_update(champ)
champ.dossier.champ_for_update(champ.type_de_champ, champ.row_id, updated_by: 'test') champ.dossier.champ_for_update(champ.type_de_champ, row_id: champ.row_id, updated_by: 'test')
end end
end end

View file

@ -19,9 +19,9 @@ describe 'shared/dossiers/edit', type: :view do
let(:type_de_champ_checkbox) { procedure.draft_types_de_champ_public.find(&:checkbox?) } let(:type_de_champ_checkbox) { procedure.draft_types_de_champ_public.find(&:checkbox?) }
let(:type_de_champ_textarea) { procedure.draft_types_de_champ_public.find(&:textarea?) } let(:type_de_champ_textarea) { procedure.draft_types_de_champ_public.find(&:textarea?) }
let(:champ_checkbox) { dossier.project_champ(type_de_champ_checkbox, nil) } let(:champ_checkbox) { dossier.project_champ(type_de_champ_checkbox) }
let(:champ_dossier_link) { dossier.project_champ(type_de_champ_dossier_link, nil) } let(:champ_dossier_link) { dossier.project_champ(type_de_champ_dossier_link) }
let(:champ_textarea) { dossier.project_champ(type_de_champ_textarea, nil) } let(:champ_textarea) { dossier.project_champ(type_de_champ_textarea) }
let(:types_de_champ_public) { [{ type: :checkbox }, { type: :header_section }, { type: :explication }, { type: :dossier_link }, { type: :textarea }] } let(:types_de_champ_public) { [{ type: :checkbox }, { type: :header_section }, { type: :explication }, { type: :dossier_link }, { type: :textarea }] }