models: require belong_to associations on type_de_champ
- Make `type_de_champ.procedure` a requirement; - Move the procedure_id assignation to `before_validation` (otherwise the record is invalid, and never gets saved);
This commit is contained in:
parent
6328011f60
commit
5a7bd24fba
3 changed files with 6 additions and 4 deletions
|
@ -49,7 +49,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
repetition: 'repetition'
|
||||
}
|
||||
|
||||
belongs_to :procedure
|
||||
belongs_to :procedure, optional: false
|
||||
belongs_to :revision, class_name: 'ProcedureRevision', optional: true
|
||||
|
||||
belongs_to :parent, class_name: 'TypeDeChamp', optional: true
|
||||
|
@ -73,8 +73,8 @@ class TypeDeChamp < ApplicationRecord
|
|||
serialize :options, WithIndifferentAccess
|
||||
|
||||
after_initialize :set_dynamic_type
|
||||
before_validation :setup_procedure
|
||||
after_create :populate_stable_id
|
||||
before_save :setup_procedure
|
||||
|
||||
attr_reader :dynamic_type
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ FactoryBot.define do
|
|||
mandatory { false }
|
||||
add_attribute(:private) { false }
|
||||
|
||||
association :procedure
|
||||
|
||||
factory :type_de_champ_text do
|
||||
type_champ { TypeDeChamp.type_champs.fetch(:text) }
|
||||
end
|
||||
|
|
|
@ -156,14 +156,14 @@ shared_examples 'type_de_champ_spec' do
|
|||
describe "repetition" do
|
||||
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_text) { create(:type_de_champ_text, procedure: procedure) }
|
||||
let(:type_de_champ_integer_number_attrs) { attributes_for(:type_de_champ_integer_number) }
|
||||
|
||||
it "associates nested types_de_champ to the parent procedure" do
|
||||
expect(type_de_champ.types_de_champ.size).to eq(0)
|
||||
expect(procedure.types_de_champ.size).to eq(1)
|
||||
|
||||
procedure.update(types_de_champ_attributes: [
|
||||
procedure.update!(types_de_champ_attributes: [
|
||||
{
|
||||
id: type_de_champ.id,
|
||||
libelle: type_de_champ.libelle,
|
||||
|
|
Loading…
Add table
Reference in a new issue