models: fix touch not propagating when using nested attributes (#3907)

Correction d'un problème où la date de modification d'un dossier n'était pas mise à jour lorsque seulement les pièces justificatives étaient modifiées
This commit is contained in:
Pierre de La Morinerie 2019-06-17 13:50:25 +02:00 committed by GitHub
commit 000cba10e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 68 additions and 23 deletions

View file

@ -726,7 +726,7 @@ Rails/HttpStatus:
Enabled: false
Rails/InverseOf:
Enabled: false
Enabled: true
Rails/LexicallyScopedActionFilter:
Enabled: false

View file

@ -1,7 +1,7 @@
class Avis < ApplicationRecord
include EmailSanitizableConcern
belongs_to :dossier, touch: true
belongs_to :dossier, inverse_of: :avis, touch: true
belongs_to :gestionnaire
belongs_to :claimant, class_name: 'Gestionnaire'

View file

@ -1,5 +1,5 @@
class Champ < ApplicationRecord
belongs_to :dossier, touch: true
belongs_to :dossier, inverse_of: :champs, touch: true
belongs_to :type_de_champ, inverse_of: :champ
belongs_to :parent, class_name: 'Champ'
has_many :commentaires

View file

@ -1,5 +1,5 @@
class Champs::RepetitionChamp < Champ
has_many :champs, -> { ordered }, foreign_key: :parent_id, dependent: :destroy
has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
accepts_nested_attributes_for :champs, allow_destroy: true

View file

@ -1,5 +1,5 @@
class Commentaire < ApplicationRecord
belongs_to :dossier, touch: true
belongs_to :dossier, inverse_of: :commentaires, touch: true
belongs_to :piece_justificative
belongs_to :user

View file

@ -19,18 +19,18 @@ class Dossier < ApplicationRecord
has_one :individual, dependent: :destroy
has_one :attestation, dependent: :destroy
has_many :pieces_justificatives, dependent: :destroy
has_many :pieces_justificatives, inverse_of: :dossier, dependent: :destroy
has_one_attached :justificatif_motivation
has_many :champs, -> { root.public_only.ordered }, dependent: :destroy
has_many :champs_private, -> { root.private_only.ordered }, class_name: 'Champ', dependent: :destroy
has_many :commentaires, dependent: :destroy
has_many :champs, -> { root.public_only.ordered }, inverse_of: :dossier, dependent: :destroy
has_many :champs_private, -> { root.private_only.ordered }, class_name: 'Champ', inverse_of: :dossier, dependent: :destroy
has_many :commentaires, inverse_of: :dossier, dependent: :destroy
has_many :invites, dependent: :destroy
has_many :follows, -> { active }
has_many :previous_follows, -> { inactive }, class_name: 'Follow'
has_many :follows, -> { active }, inverse_of: :dossier
has_many :previous_follows, -> { inactive }, class_name: 'Follow', inverse_of: :dossier
has_many :followers_gestionnaires, through: :follows, source: :gestionnaire
has_many :previous_followers_gestionnaires, -> { distinct }, through: :previous_follows, source: :gestionnaire
has_many :avis, dependent: :destroy
has_many :avis, inverse_of: :dossier, dependent: :destroy
has_many :dossier_operation_logs, dependent: :destroy

View file

@ -12,12 +12,12 @@ class Gestionnaire < ApplicationRecord
has_many :assign_to, dependent: :destroy
has_many :procedures, through: :assign_to
has_many :assign_to_with_email_notifications, -> { with_email_notifications }, class_name: 'AssignTo'
has_many :assign_to_with_email_notifications, -> { with_email_notifications }, class_name: 'AssignTo', inverse_of: :gestionnaire
has_many :procedures_with_email_notifications, through: :assign_to_with_email_notifications, source: :procedure
has_many :dossiers, -> { state_not_brouillon }, through: :procedures
has_many :follows, -> { active }
has_many :previous_follows, -> { inactive }, class_name: 'Follow'
has_many :follows, -> { active }, inverse_of: :gestionnaire
has_many :previous_follows, -> { inactive }, class_name: 'Follow', inverse_of: :gestionnaire
has_many :followed_dossiers, through: :follows, source: :dossier
has_many :previously_followed_dossiers, -> { distinct }, through: :previous_follows, source: :dossier
has_many :avis

View file

@ -1,5 +1,5 @@
class PieceJustificative < ApplicationRecord
belongs_to :dossier, touch: true
belongs_to :dossier, inverse_of: :pieces_justificatives, touch: true
belongs_to :type_de_piece_justificative
has_one :commentaire

View file

@ -5,9 +5,9 @@ class Procedure < ApplicationRecord
MAX_DUREE_CONSERVATION = 36
has_many :types_de_piece_justificative, -> { ordered }, dependent: :destroy
has_many :types_de_champ, -> { root.public_only.ordered }, dependent: :destroy
has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', dependent: :destroy
has_many :types_de_piece_justificative, -> { ordered }, inverse_of: :procedure, dependent: :destroy
has_many :types_de_champ, -> { root.public_only.ordered }, inverse_of: :procedure, dependent: :destroy
has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', inverse_of: :procedure, dependent: :destroy
has_many :dossiers, dependent: :restrict_with_exception
has_many :deleted_dossiers, dependent: :destroy

View file

@ -32,7 +32,7 @@ class TypeDeChamp < ApplicationRecord
belongs_to :procedure
belongs_to :parent, class_name: 'TypeDeChamp'
has_many :types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'TypeDeChamp', dependent: :destroy
has_many :types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'TypeDeChamp', inverse_of: :parent, dependent: :destroy
store_accessor :options, :cadastres, :quartiers_prioritaires, :parcelles_agricoles, :old_pj
delegate :tags_for_template, to: :dynamic_type

View file

@ -386,9 +386,11 @@ describe Users::DossiersController, type: :controller do
describe '#update_brouillon' do
before { sign_in(user) }
let!(:dossier) { create(:dossier, user: user) }
let(:first_champ) { dossier.champs.first }
let(:value) { 'beautiful value' }
let(:now) { Time.zone.parse('01/01/2100') }
let(:submit_payload) do
{
id: dossier.id,
@ -402,7 +404,11 @@ describe Users::DossiersController, type: :controller do
end
let(:payload) { submit_payload }
subject { patch :update_brouillon, params: payload }
subject do
Timecop.freeze(now) do
patch :update_brouillon, params: payload
end
end
context 'when the dossier cannot be updated by the user' do
let!(:dossier) { create(:dossier, :en_instruction, user: user) }
@ -421,6 +427,7 @@ describe Users::DossiersController, type: :controller do
expect(response).to redirect_to(merci_dossier_path(dossier))
expect(first_champ.reload.value).to eq('beautiful value')
expect(dossier.reload.updated_at.year).to eq(2100)
expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction))
end
@ -549,9 +556,15 @@ describe Users::DossiersController, type: :controller do
describe '#update' do
before { sign_in(user) }
let!(:dossier) { create(:dossier, :en_construction, user: user) }
let(:procedure) { create(:procedure, :published, :with_type_de_champ, :with_piece_justificative) }
let!(:dossier) { create(:dossier, :en_construction, user: user, procedure: procedure) }
let(:first_champ) { dossier.champs.first }
let(:piece_justificative_champ) { dossier.champs.last }
let(:value) { 'beautiful value' }
let(:file) { Rack::Test::UploadedFile.new("./spec/fixtures/files/piece_justificative_0.pdf", 'application/pdf') }
let(:now) { Time.zone.parse('01/01/2100') }
let(:submit_payload) do
{
id: dossier.id,
@ -565,7 +578,11 @@ describe Users::DossiersController, type: :controller do
end
let(:payload) { submit_payload }
subject { patch :update, params: payload }
subject do
Timecop.freeze(now) do
patch :update, params: payload
end
end
context 'when the dossier cannot be updated by the user' do
let!(:dossier) { create(:dossier, :en_instruction, user: user) }
@ -584,8 +601,28 @@ describe Users::DossiersController, type: :controller do
expect(response).to redirect_to(demande_dossier_path(dossier))
expect(first_champ.reload.value).to eq('beautiful value')
expect(dossier.reload.updated_at.year).to eq(2100)
expect(dossier.reload.state).to eq(Dossier.states.fetch(:en_construction))
end
context 'when only files champs are modified' do
let(:submit_payload) do
{
id: dossier.id,
dossier: {
champs_attributes: {
id: piece_justificative_champ.id,
piece_justificative_file: file
}
}
}
end
it 'updates the dossier modification date' do
subject
expect(dossier.reload.updated_at.year).to eq(2100)
end
end
end
context 'when the update fails' do

View file

@ -127,6 +127,14 @@ FactoryBot.define do
end
end
trait :with_piece_justificative do
after(:build) do |procedure, _evaluator|
type_de_champ = create(:type_de_champ_piece_justificative)
procedure.types_de_champ << type_de_champ
end
end
# Deprecated
trait :with_two_type_de_piece_justificative do
after(:build) do |procedure, _evaluator|
rib = create(:type_de_piece_justificative, :rib, order_place: 1)