Fix missing dossier_id on champs inside repetition

This commit is contained in:
Paul Chavard 2019-04-18 16:55:35 +02:00
parent 7face5e971
commit 8d093bd9ba
4 changed files with 34 additions and 10 deletions

View file

@ -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

View file

@ -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 doesnt make much sense to search
end
private
def setup_dossier
champs.each do |champ|
champ.dossier = dossier
end
end
end

View file

@ -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

View file

@ -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