refactor(dossier): use update_champs_private and update_champs_public methods
This commit is contained in:
parent
ee57d00507
commit
1021a31f7b
6 changed files with 155 additions and 52 deletions
|
@ -4,9 +4,14 @@ module TurboChampsConcern
|
||||||
private
|
private
|
||||||
|
|
||||||
def champs_to_turbo_update(params, champs)
|
def champs_to_turbo_update(params, champs)
|
||||||
champ_ids = params.keys.map(&:to_i)
|
to_update = if params.values.filter { _1.key?(:with_public_id) }.empty?
|
||||||
|
champ_ids = params.keys.map(&:to_i)
|
||||||
|
champs.filter { _1.id.in?(champ_ids) }
|
||||||
|
else
|
||||||
|
champ_public_ids = params.keys
|
||||||
|
champs.filter { _1.public_id.in?(champ_public_ids) }
|
||||||
|
end.filter { _1.refresh_after_update? || _1.forked_with_changes? }
|
||||||
|
|
||||||
to_update = champs.filter { _1.id.in?(champ_ids) && (_1.refresh_after_update? || _1.forked_with_changes?) }
|
|
||||||
to_show, to_hide = champs.filter(&:conditional?)
|
to_show, to_hide = champs.filter(&:conditional?)
|
||||||
.partition(&:visible?)
|
.partition(&:visible?)
|
||||||
.map { champs_to_one_selector(_1 - to_update) }
|
.map { champs_to_one_selector(_1 - to_update) }
|
||||||
|
|
|
@ -273,8 +273,8 @@ module Instructeurs
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_annotations
|
def update_annotations
|
||||||
dossier_with_champs.assign_attributes(champs_private_params)
|
dossier_with_champs.update_champs_attributes(champs_private_attributes_params, :private)
|
||||||
if dossier.champs_private_all.any?(&:changed?)
|
if dossier.champs.any?(&:changed_for_autosave?) || dossier.champs_private_all.any?(&:changed_for_autosave?) # TODO remove second condition after one deploy
|
||||||
dossier.last_champ_private_updated_at = Time.zone.now
|
dossier.last_champ_private_updated_at = Time.zone.now
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -282,7 +282,7 @@ module Instructeurs
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.turbo_stream do
|
format.turbo_stream do
|
||||||
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_private_params.fetch(:champs_private_all_attributes), dossier.champs_private_all)
|
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_private_attributes_params, dossier.champs.filter(&:private?))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -294,7 +294,12 @@ module Instructeurs
|
||||||
|
|
||||||
def annotation
|
def annotation
|
||||||
@dossier = dossier_with_champs(pj_template: false)
|
@dossier = dossier_with_champs(pj_template: false)
|
||||||
annotation = @dossier.champs_private_all.find(params[:annotation_id])
|
annotation = if params[:with_public_id].present?
|
||||||
|
type_de_champ = @dossier.find_type_de_champ_by_stable_id(params[:annotation_id], :private)
|
||||||
|
@dossier.project_champ(type_de_champ, params[:row_id])
|
||||||
|
else
|
||||||
|
@dossier.champs_private_all.find(params[:annotation_id])
|
||||||
|
end
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.turbo_stream do
|
format.turbo_stream do
|
||||||
|
@ -392,12 +397,36 @@ module Instructeurs
|
||||||
end
|
end
|
||||||
|
|
||||||
def champs_private_params
|
def champs_private_params
|
||||||
champs_params = params.require(:dossier).permit(champs_private_attributes: [
|
champ_attributes = [
|
||||||
:id, :value, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :code_departement, value: [],
|
:id,
|
||||||
champs_attributes: [:id, :_destroy, :value, :primary_value, :secondary_value, :piece_justificative_file, :value_other, :external_id, :numero_allocataire, :code_postal, :code_departement, :feature, value: []]
|
:value,
|
||||||
])
|
:value_other,
|
||||||
champs_params[:champs_private_all_attributes] = champs_params.delete(:champs_private_attributes) || {}
|
:external_id,
|
||||||
champs_params
|
:primary_value,
|
||||||
|
:secondary_value,
|
||||||
|
:numero_allocataire,
|
||||||
|
:code_postal,
|
||||||
|
:identifiant,
|
||||||
|
:numero_fiscal,
|
||||||
|
:reference_avis,
|
||||||
|
:ine,
|
||||||
|
:piece_justificative_file,
|
||||||
|
:code_departement,
|
||||||
|
:accreditation_number,
|
||||||
|
:accreditation_birthdate,
|
||||||
|
:feature,
|
||||||
|
:with_public_id,
|
||||||
|
value: []
|
||||||
|
]
|
||||||
|
# Strong attributes do not support records (indexed hash); they only support hashes with
|
||||||
|
# static keys. We create a static hash based on the available keys.
|
||||||
|
public_ids = params.dig(:dossier, :champs_private_attributes)&.keys || []
|
||||||
|
champs_private_attributes = public_ids.map { [_1, champ_attributes] }.to_h
|
||||||
|
params.require(:dossier).permit(champs_private_attributes:)
|
||||||
|
end
|
||||||
|
|
||||||
|
def champs_private_attributes_params
|
||||||
|
champs_private_params.fetch(:champs_private_attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark_demande_as_read
|
def mark_demande_as_read
|
||||||
|
|
|
@ -284,7 +284,7 @@ module Users
|
||||||
end
|
end
|
||||||
|
|
||||||
format.turbo_stream do
|
format.turbo_stream do
|
||||||
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_public_params.fetch(:champs_public_all_attributes), dossier.champs_public_all)
|
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_public_attributes_params, dossier.champs.filter(&:public?))
|
||||||
render :update, layout: false
|
render :update, layout: false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -298,7 +298,7 @@ module Users
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.turbo_stream do
|
format.turbo_stream do
|
||||||
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_public_params.fetch(:champs_public_all_attributes), dossier.champs_public_all)
|
@to_show, @to_hide, @to_update = champs_to_turbo_update(champs_public_attributes_params, dossier.champs.filter(&:public?))
|
||||||
render :update, layout: false
|
render :update, layout: false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -310,7 +310,12 @@ module Users
|
||||||
|
|
||||||
def champ
|
def champ
|
||||||
@dossier = dossier_with_champs(pj_template: false)
|
@dossier = dossier_with_champs(pj_template: false)
|
||||||
champ = @dossier.champs_public_all.find(params[:champ_id])
|
champ = if params[:with_public_id].present?
|
||||||
|
type_de_champ = @dossier.find_type_de_champ_by_stable_id(params[:champ_id], :public)
|
||||||
|
@dossier.project_champ(type_de_champ, params[:row_id])
|
||||||
|
else
|
||||||
|
@dossier.champs_public_all.find(params[:champ_id])
|
||||||
|
end
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.turbo_stream do
|
format.turbo_stream do
|
||||||
|
@ -478,7 +483,7 @@ module Users
|
||||||
end
|
end
|
||||||
|
|
||||||
def champs_public_params
|
def champs_public_params
|
||||||
champs_params = params.require(:dossier).permit(champs_public_attributes: [
|
champ_attributes = [
|
||||||
:id,
|
:id,
|
||||||
:value,
|
:value,
|
||||||
:value_other,
|
:value_other,
|
||||||
|
@ -496,10 +501,18 @@ module Users
|
||||||
:accreditation_number,
|
:accreditation_number,
|
||||||
:accreditation_birthdate,
|
:accreditation_birthdate,
|
||||||
:feature,
|
:feature,
|
||||||
|
:with_public_id,
|
||||||
value: []
|
value: []
|
||||||
])
|
]
|
||||||
champs_params[:champs_public_all_attributes] = champs_params.delete(:champs_public_attributes) || {}
|
# Strong attributes do not support records (indexed hash); they only support hashes with
|
||||||
champs_params
|
# static keys. We create a static hash based on the available keys.
|
||||||
|
public_ids = params.dig(:dossier, :champs_public_attributes)&.keys || []
|
||||||
|
champs_public_attributes = public_ids.map { [_1, champ_attributes] }.to_h
|
||||||
|
params.require(:dossier).permit(champs_public_attributes:)
|
||||||
|
end
|
||||||
|
|
||||||
|
def champs_public_attributes_params
|
||||||
|
champs_public_params.fetch(:champs_public_attributes)
|
||||||
end
|
end
|
||||||
|
|
||||||
def dossier_scope
|
def dossier_scope
|
||||||
|
@ -532,8 +545,8 @@ module Users
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_dossier_and_compute_errors
|
def update_dossier_and_compute_errors
|
||||||
@dossier.assign_attributes(champs_public_params)
|
@dossier.update_champs_attributes(champs_public_attributes_params, :public)
|
||||||
if @dossier.champs_public_all.any?(&:changed_for_autosave?)
|
if @dossier.champs.any?(&:changed_for_autosave?) || @dossier.champs_public_all.any?(&:changed_for_autosave?) # TODO remove second condition after one deploy
|
||||||
@dossier.last_champ_updated_at = Time.zone.now
|
@dossier.last_champ_updated_at = Time.zone.now
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,9 @@ class Champ < ApplicationRecord
|
||||||
include ChampConditionalConcern
|
include ChampConditionalConcern
|
||||||
include ChampsValidateConcern
|
include ChampsValidateConcern
|
||||||
|
|
||||||
|
# TODO: remove after one deploy
|
||||||
|
attr_writer :with_public_id
|
||||||
|
|
||||||
belongs_to :dossier, inverse_of: false, touch: true, optional: false
|
belongs_to :dossier, inverse_of: false, touch: true, optional: false
|
||||||
belongs_to :type_de_champ, inverse_of: :champ, optional: false
|
belongs_to :type_de_champ, inverse_of: :champ, optional: false
|
||||||
belongs_to :parent, class_name: 'Champ', optional: true
|
belongs_to :parent, class_name: 'Champ', optional: true
|
||||||
|
|
|
@ -1005,25 +1005,25 @@ describe Instructeurs::DossiersController, type: :controller do
|
||||||
dossier_id: dossier.id,
|
dossier_id: dossier.id,
|
||||||
dossier: {
|
dossier: {
|
||||||
champs_private_attributes: {
|
champs_private_attributes: {
|
||||||
'0': {
|
champ_multiple_drop_down_list.public_id => {
|
||||||
id: champ_multiple_drop_down_list.id,
|
with_public_id: true,
|
||||||
value: ['', 'val1', 'val2']
|
value: ['', 'val1', 'val2']
|
||||||
},
|
},
|
||||||
'1': {
|
champ_datetime.public_id => {
|
||||||
id: champ_datetime.id,
|
with_public_id: true,
|
||||||
value: '2019-12-21T13:17'
|
value: '2019-12-21T13:17'
|
||||||
},
|
},
|
||||||
'2': {
|
champ_linked_drop_down_list.public_id => {
|
||||||
id: champ_linked_drop_down_list.id,
|
with_public_id: true,
|
||||||
primary_value: 'primary',
|
primary_value: 'primary',
|
||||||
secondary_value: 'secondary'
|
secondary_value: 'secondary'
|
||||||
},
|
},
|
||||||
'3': {
|
champ_repetition.champs.first.public_id => {
|
||||||
id: champ_repetition.champs.first.id,
|
with_public_id: true,
|
||||||
value: 'text'
|
value: 'text'
|
||||||
},
|
},
|
||||||
'4': {
|
champ_drop_down_list.public_id => {
|
||||||
id: champ_drop_down_list.id,
|
with_public_id: true,
|
||||||
value: '__other__',
|
value: '__other__',
|
||||||
value_other: 'other value'
|
value_other: 'other value'
|
||||||
}
|
}
|
||||||
|
@ -1074,12 +1074,11 @@ describe Instructeurs::DossiersController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with invalid champs_public (DecimalNumberChamp)' do
|
after do
|
||||||
let(:types_de_champ_public) do
|
Timecop.return
|
||||||
[
|
end
|
||||||
{ type: :decimal_number }
|
|
||||||
]
|
context "with new values for champs_private (legacy)" do
|
||||||
end
|
|
||||||
let(:params) do
|
let(:params) do
|
||||||
{
|
{
|
||||||
procedure_id: procedure.id,
|
procedure_id: procedure.id,
|
||||||
|
@ -1095,9 +1094,63 @@ describe Instructeurs::DossiersController, type: :controller do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'update champs_private' do
|
||||||
|
patch :update_annotations, params: params, format: :turbo_stream
|
||||||
|
champ_datetime.reload
|
||||||
|
expect(champ_datetime.value).to eq(Time.zone.parse('2024-03-30T07:03:00').iso8601)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "without new values for champs_private" do
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
procedure_id: procedure.id,
|
||||||
|
dossier_id: dossier.id,
|
||||||
|
dossier: {
|
||||||
|
champs_private_attributes: {},
|
||||||
|
champs_public_attributes: {
|
||||||
|
champ_multiple_drop_down_list.public_id => {
|
||||||
|
with_public_id: true,
|
||||||
|
value: ['', 'val1', 'val2']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it {
|
||||||
|
expect(dossier.reload.last_champ_private_updated_at).to eq(nil)
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with invalid champs_public (DecimalNumberChamp)" do
|
||||||
|
let(:types_de_champ_public) do
|
||||||
|
[
|
||||||
|
{ type: :decimal_number }
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:champ_decimal_number) { dossier.champs_public.first }
|
||||||
|
|
||||||
|
let(:params) do
|
||||||
|
{
|
||||||
|
procedure_id: procedure.id,
|
||||||
|
dossier_id: dossier.id,
|
||||||
|
dossier: {
|
||||||
|
champs_private_attributes: {
|
||||||
|
champ_datetime.public_id => {
|
||||||
|
with_public_id: true,
|
||||||
|
value: '2024-03-30T07:03'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
it 'update champs_private' do
|
it 'update champs_private' do
|
||||||
too_long_float = '3.1415'
|
too_long_float = '3.1415'
|
||||||
dossier.champs_public.first.update_column(:value, too_long_float)
|
champ_decimal_number.update_column(:value, too_long_float)
|
||||||
patch :update_annotations, params: params, format: :turbo_stream
|
patch :update_annotations, params: params, format: :turbo_stream
|
||||||
champ_datetime.reload
|
champ_datetime.reload
|
||||||
expect(champ_datetime.value).to eq(Time.zone.parse('2024-03-30T07:03:00').iso8601)
|
expect(champ_datetime.value).to eq(Time.zone.parse('2024-03-30T07:03:00').iso8601)
|
||||||
|
|
|
@ -674,12 +674,12 @@ describe Users::DossiersController, type: :controller do
|
||||||
dossier: {
|
dossier: {
|
||||||
groupe_instructeur_id: dossier.groupe_instructeur_id,
|
groupe_instructeur_id: dossier.groupe_instructeur_id,
|
||||||
champs_public_attributes: {
|
champs_public_attributes: {
|
||||||
first_champ.id => {
|
first_champ.public_id => {
|
||||||
id: first_champ.id,
|
with_public_id: true,
|
||||||
value: value
|
value: value
|
||||||
},
|
},
|
||||||
piece_justificative_champ.id => {
|
piece_justificative_champ.public_id => {
|
||||||
id: piece_justificative_champ.id,
|
with_public_id: true,
|
||||||
piece_justificative_file: file
|
piece_justificative_file: file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -719,7 +719,7 @@ describe Users::DossiersController, type: :controller do
|
||||||
{
|
{
|
||||||
id: dossier.id,
|
id: dossier.id,
|
||||||
dossier: {
|
dossier: {
|
||||||
champs_public_attributes: { first_champ.id => { id: first_champ.id } }
|
champs_public_attributes: { first_champ.public_id => { with_public_id: true } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -747,7 +747,7 @@ describe Users::DossiersController, type: :controller do
|
||||||
{
|
{
|
||||||
id: dossier.id,
|
id: dossier.id,
|
||||||
dossier: {
|
dossier: {
|
||||||
champs_public_attributes: { first_champ.id => { id: first_champ.id, value: value } }
|
champs_public_attributes: { first_champ.public_id => { with_public_id: true, value: value } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -790,12 +790,12 @@ describe Users::DossiersController, type: :controller do
|
||||||
dossier: {
|
dossier: {
|
||||||
groupe_instructeur_id: dossier.groupe_instructeur_id,
|
groupe_instructeur_id: dossier.groupe_instructeur_id,
|
||||||
champs_public_attributes: {
|
champs_public_attributes: {
|
||||||
first_champ.id => {
|
first_champ.public_id => {
|
||||||
id: first_champ.id,
|
with_public_id: true,
|
||||||
value: value
|
value: value
|
||||||
},
|
},
|
||||||
piece_justificative_champ.id => {
|
piece_justificative_champ.public_id => {
|
||||||
id: piece_justificative_champ.id,
|
with_public_id: true,
|
||||||
piece_justificative_file: file
|
piece_justificative_file: file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -855,8 +855,8 @@ describe Users::DossiersController, type: :controller do
|
||||||
id: dossier.id,
|
id: dossier.id,
|
||||||
dossier: {
|
dossier: {
|
||||||
champs_public_attributes: {
|
champs_public_attributes: {
|
||||||
piece_justificative_champ.id => {
|
piece_justificative_champ.public_id => {
|
||||||
id: piece_justificative_champ.id,
|
with_public_id: true,
|
||||||
piece_justificative_file: file
|
piece_justificative_file: file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -951,8 +951,8 @@ describe Users::DossiersController, type: :controller do
|
||||||
id: dossier.id,
|
id: dossier.id,
|
||||||
dossier: {
|
dossier: {
|
||||||
champs_public_attributes: {
|
champs_public_attributes: {
|
||||||
first_champ.id => {
|
first_champ.public_id => {
|
||||||
id: first_champ.id,
|
with_public_id: true,
|
||||||
value: value
|
value: value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue