diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 860883ae0..f3b9efe86 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -52,6 +52,7 @@ class TypeDeChamp < ApplicationRecord after_initialize :set_dynamic_type after_create :populate_stable_id before_save :setup_procedure + before_validation :set_default_drop_down_list attr_reader :dynamic_type @@ -140,6 +141,14 @@ class TypeDeChamp < ApplicationRecord ]) end + def drop_down_list? + type_champ.in?([ + TypeDeChamp.type_champs.fetch(:drop_down_list), + TypeDeChamp.type_champs.fetch(:multiple_drop_down_list), + TypeDeChamp.type_champs.fetch(:linked_drop_down_list) + ]) + end + def exclude_from_view? type_champ == TypeDeChamp.type_champs.fetch(:explication) end @@ -178,6 +187,12 @@ class TypeDeChamp < ApplicationRecord private + def set_default_drop_down_list + if drop_down_list? && !drop_down_list + self.drop_down_list_attributes = { value: '' } + end + end + def setup_procedure types_de_champ.each do |type_de_champ| type_de_champ.procedure = procedure diff --git a/spec/controllers/new_administrateur/types_de_champ_controller_spec.rb b/spec/controllers/new_administrateur/types_de_champ_controller_spec.rb new file mode 100644 index 000000000..3722ed60b --- /dev/null +++ b/spec/controllers/new_administrateur/types_de_champ_controller_spec.rb @@ -0,0 +1,61 @@ +describe NewAdministrateur::TypesDeChampController, type: :controller do + let(:admin) { create(:administrateur) } + + describe '#types_de_champs editor api' do + let(:procedure) { create(:procedure) } + + before do + admin.procedures << procedure + sign_in admin + end + + let(:type_champ) { TypeDeChamp.type_champs.fetch(:text) } + + context "create type_de_champ text" do + before do + post :create, params: { + procedure_id: procedure.id, + type_de_champ: { + type_champ: type_champ, + libelle: 'Nouveau champ' + } + } + end + + it { expect(response).to have_http_status(:created) } + end + + context "validate type_de_champ linked_drop_down_list" do + let(:type_champ) { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) } + + before do + post :create, params: { + procedure_id: procedure.id, + type_de_champ: { + type_champ: type_champ, + libelle: 'Nouveau champ' + } + } + end + + it { expect(response).to have_http_status(:unprocessable_entity) } + end + + context "create type_de_champ linked_drop_down_list" do + let(:type_champ) { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) } + + before do + post :create, params: { + procedure_id: procedure.id, + type_de_champ: { + type_champ: type_champ, + libelle: 'Nouveau champ', + drop_down_list_value: '--value--' + } + } + end + + it { expect(response).to have_http_status(:created) } + end + end +end