diff --git a/app/models/champ.rb b/app/models/champ.rb index da1919a83..505bfaa97 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -148,7 +148,7 @@ class Champ < ApplicationRecord # predictable input name. def input_name if parent_id - "#{parent.input_name}[#{champs_attributes_accessor}][#{id}]" + "#{parent.input_name}[champs_attributes][#{id}]" else "dossier[#{champs_attributes_accessor}][#{id}]" end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index b256a04d9..3391ec4f0 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -602,5 +602,25 @@ describe Champ do expect(champ.reload.data).to eq data end end + + context "#input_name" do + let(:champ) { create(:champ_text) } + it { expect(champ.input_name).to eq "dossier[champs_attributes][#{champ.id}]" } + + context "when private" do + let(:champ) { create(:champ_text, private: true) } + it { expect(champ.input_name).to eq "dossier[champs_private_attributes][#{champ.id}]" } + end + + context "when has parent" do + let(:champ) { create(:champ_text, parent: create(:champ_text)) } + it { expect(champ.input_name).to eq "dossier[champs_attributes][#{champ.parent_id}][champs_attributes][#{champ.id}]" } + end + + context "when has private parent" do + let(:champ) { create(:champ_text, private: true, parent: create(:champ_text, private: true)) } + it { expect(champ.input_name).to eq "dossier[champs_private_attributes][#{champ.parent_id}][champs_attributes][#{champ.id}]" } + end + end end end