Fix champ repetition belongs_to associations
This commit is contained in:
parent
531feb3515
commit
1beefe4469
5 changed files with 61 additions and 13 deletions
|
@ -1,4 +1,6 @@
|
|||
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
|
||||
|
@ -10,4 +12,10 @@ class Champs::RepetitionChamp < Champ
|
|||
def search_terms
|
||||
# The user cannot enter any information here so it doesn’t make much sense to search
|
||||
end
|
||||
|
||||
def setup_dossier
|
||||
champs.each do |champ|
|
||||
champ.dossier = dossier
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,8 +4,8 @@ class Procedure < ApplicationRecord
|
|||
MAX_DUREE_CONSERVATION = 36
|
||||
|
||||
has_many :types_de_piece_justificative, -> { ordered }, dependent: :destroy
|
||||
has_many :types_de_champ, -> { public_only.ordered }, dependent: :destroy
|
||||
has_many :types_de_champ_private, -> { private_only.ordered }, class_name: 'TypeDeChamp', 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 :dossiers
|
||||
has_many :deleted_dossiers, dependent: :destroy
|
||||
|
||||
|
|
|
@ -61,12 +61,14 @@ class TypeDeChamp < ApplicationRecord
|
|||
|
||||
after_initialize :set_dynamic_type
|
||||
after_create :populate_stable_id
|
||||
before_save :setup_procedure
|
||||
|
||||
attr_reader :dynamic_type
|
||||
|
||||
scope :public_only, -> { where(private: false) }
|
||||
scope :private_only, -> { where(private: true) }
|
||||
scope :ordered, -> { order(order_place: :asc) }
|
||||
scope :root, -> { where(parent_id: nil) }
|
||||
|
||||
has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
|
||||
def build(params = {})
|
||||
|
@ -82,6 +84,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
has_one_attached :piece_justificative_template
|
||||
|
||||
accepts_nested_attributes_for :drop_down_list
|
||||
accepts_nested_attributes_for :types_de_champ, allow_destroy: true
|
||||
|
||||
validates :libelle, presence: true, allow_blank: false, allow_nil: false
|
||||
validates :type_champ, presence: true, allow_blank: false, allow_nil: false
|
||||
|
@ -160,6 +163,12 @@ class TypeDeChamp < ApplicationRecord
|
|||
|
||||
private
|
||||
|
||||
def setup_procedure
|
||||
types_de_champ.each do |type_de_champ|
|
||||
type_de_champ.procedure = procedure
|
||||
end
|
||||
end
|
||||
|
||||
def populate_stable_id
|
||||
if !stable_id
|
||||
update_column(:stable_id, id)
|
||||
|
|
|
@ -401,17 +401,31 @@ describe Champ do
|
|||
end
|
||||
|
||||
describe "repetition" do
|
||||
let(:champ) { create(:champ_repetition) }
|
||||
let(:dossier) { create(:dossier) }
|
||||
let(:champ) { create(:champ_repetition, dossier: dossier) }
|
||||
let(:champ_text) { create(:champ_text, row: 0) }
|
||||
let(:champ_integer_number) { create(:champ_integer_number, row: 0) }
|
||||
let(:champ_text2) { create(:champ_text, row: 1) }
|
||||
let(:champ_text_attrs) { attributes_for(:champ_text, row: 1) }
|
||||
|
||||
it {
|
||||
it "associates nested champs to the parent dossier" do
|
||||
expect(champ.rows.size).to eq(0)
|
||||
dossier.reload
|
||||
expect(dossier.champs.size).to eq(2)
|
||||
|
||||
champ.champs << champ_text2
|
||||
dossier.update(champs_attributes: [
|
||||
{
|
||||
id: champ.id,
|
||||
champs_attributes: [champ_text_attrs]
|
||||
}
|
||||
])
|
||||
|
||||
champ.reload
|
||||
dossier.reload
|
||||
expect(dossier.champs.size).to eq(2)
|
||||
expect(champ.rows.size).to eq(1)
|
||||
|
||||
expect(champ.champs.first.dossier).to eq(dossier)
|
||||
|
||||
champ.champs << champ_integer_number
|
||||
row = champ.reload.rows.first
|
||||
expect(row.size).to eq(1)
|
||||
|
@ -423,6 +437,6 @@ describe Champ do
|
|||
expect(row.second).to eq(champ_text)
|
||||
|
||||
expect(champ.rows.size).to eq(2)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -112,18 +112,35 @@ shared_examples 'type_de_champ_spec' do
|
|||
end
|
||||
|
||||
describe "repetition" do
|
||||
let(:type_de_champ) { create(:type_de_champ_repetition) }
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:type_de_champ) { create(:type_de_champ_repetition, procedure: procedure) }
|
||||
let(:type_de_champ_text) { create(:type_de_champ_text) }
|
||||
let(:type_de_champ_integer_number) { create(:type_de_champ_integer_number) }
|
||||
let(:type_de_champ_integer_number_attrs) { attributes_for(:type_de_champ_integer_number) }
|
||||
|
||||
it {
|
||||
it "associates nested types_de_champ to the parent procedure" do
|
||||
expect(type_de_champ.types_de_champ.size).to eq(0)
|
||||
type_de_champ.types_de_champ << type_de_champ_integer_number
|
||||
expect(procedure.types_de_champ.size).to eq(1)
|
||||
|
||||
procedure.update(types_de_champ_attributes: [
|
||||
{
|
||||
id: type_de_champ.id,
|
||||
libelle: type_de_champ.libelle,
|
||||
types_de_champ_attributes: [type_de_champ_integer_number_attrs]
|
||||
}
|
||||
])
|
||||
procedure.reload
|
||||
type_de_champ.reload
|
||||
|
||||
expect(procedure.types_de_champ.size).to eq(1)
|
||||
expect(type_de_champ.types_de_champ.size).to eq(1)
|
||||
|
||||
expect(type_de_champ.types_de_champ.first.parent).to eq(type_de_champ)
|
||||
expect(type_de_champ.types_de_champ.first.procedure).to eq(procedure)
|
||||
expect(type_de_champ.types_de_champ.first.private?).to eq(false)
|
||||
|
||||
type_de_champ.types_de_champ << type_de_champ_text
|
||||
expect(type_de_champ.types_de_champ.size).to eq(2)
|
||||
expect(type_de_champ_integer_number.parent).to eq(type_de_champ)
|
||||
expect(type_de_champ_text.parent).to eq(type_de_champ)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue