From 8d093bd9baf63e495258a23a8c78ac881fd46da2 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 18 Apr 2019 16:55:35 +0200 Subject: [PATCH] Fix missing dossier_id on champs inside repetition --- app/models/champ.rb | 13 +++++++++++++ app/models/champs/repetition_champ.rb | 10 ---------- ..._add_missing_dossier_id_to_repetitions.rake | 18 ++++++++++++++++++ spec/models/champ_spec.rb | 3 +++ 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 lib/tasks/deployment/20190418144354_add_missing_dossier_id_to_repetitions.rake diff --git a/app/models/champ.rb b/app/models/champ.rb index c88e0fe3f..a41e732f3 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -1,6 +1,7 @@ class Champ < ApplicationRecord belongs_to :dossier, touch: true belongs_to :type_de_champ, inverse_of: :champ + belongs_to :parent, class_name: 'Champ' has_many :commentaires has_one_attached :piece_justificative_file has_one :virus_scan @@ -18,6 +19,8 @@ class Champ < ApplicationRecord scope :ordered, -> { includes(:type_de_champ).order(:row, 'types_de_champ.order_place') } scope :root, -> { where(parent_id: nil) } + before_create :set_dossier_id, if: :needs_dossier_id? + def public? !private? end @@ -54,4 +57,14 @@ class Champ < ApplicationRecord def main_value_name :value end + + private + + def needs_dossier_id? + !dossier_id && parent_id + end + + def set_dossier_id + self.dossier_id = parent.dossier_id + end end diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index a2f02fa04..7f8313e16 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -1,6 +1,4 @@ class Champs::RepetitionChamp < Champ - before_save :setup_dossier - has_many :champs, -> { ordered }, foreign_key: :parent_id, dependent: :destroy accepts_nested_attributes_for :champs, allow_destroy: true @@ -22,12 +20,4 @@ class Champs::RepetitionChamp < Champ def search_terms # The user cannot enter any information here so it doesn’t make much sense to search end - - private - - def setup_dossier - champs.each do |champ| - champ.dossier = dossier - end - end end diff --git a/lib/tasks/deployment/20190418144354_add_missing_dossier_id_to_repetitions.rake b/lib/tasks/deployment/20190418144354_add_missing_dossier_id_to_repetitions.rake new file mode 100644 index 000000000..06538ea05 --- /dev/null +++ b/lib/tasks/deployment/20190418144354_add_missing_dossier_id_to_repetitions.rake @@ -0,0 +1,18 @@ +namespace :after_party do + desc 'Deployment task: add_missing_dossier_id_to_repetitions' + task add_missing_dossier_id_to_repetitions: :environment do + puts "Running deploy task 'add_missing_dossier_id_to_repetitions'" + + champs = Champ.where(dossier_id: nil) + progress = ProgressReport.new(champs.count) + champs.find_each do |champ| + champ.update_column(:dossier_id, champ.parent.dossier_id) + progress.inc + end + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord.create version: '20190418144354' + end +end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 339a3600a..296310a2b 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -406,6 +406,7 @@ describe Champ do let(:champ_text) { create(:champ_text, row: 0) } let(:champ_integer_number) { create(:champ_integer_number, row: 0) } let(:champ_text_attrs) { attributes_for(:champ_text, row: 1) } + let(:champ_text_row_1) { create(:champ_text, row: 1, parent: champ) } it "associates nested champs to the parent dossier" do expect(champ.rows.size).to eq(0) @@ -441,6 +442,8 @@ describe Champ do expect(row.second).to eq(champ_text) expect(champ.rows.size).to eq(2) + + expect(champ_text_row_1.dossier_id).to eq(champ.dossier_id) end end end