diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 3950d37ea..91ae22e30 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -24,7 +24,8 @@ class TypeDeChamp < ActiveRecord::Base validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :type_champ, presence: true, allow_blank: false, allow_nil: false - # validates :order_place, presence: true, allow_blank: false, allow_nil: false + + before_validation :change_header_section_mandatory def self.type_de_champs_list_fr type_champs.map { |champ| [ I18n.t("activerecord.attributes.type_de_champ.type_champs.#{champ.last}"), champ.first ] } @@ -33,4 +34,8 @@ class TypeDeChamp < ActiveRecord::Base def field_for_list? !(type_champ == 'textarea' || type_champ == 'header_section') end + + def change_header_section_mandatory + self.mandatory = false if self.type_champ == 'header_section' + end end \ No newline at end of file diff --git a/db/migrate/20161004175442_fix_type_de_champ_header_section_with_mandatory_true.rb b/db/migrate/20161004175442_fix_type_de_champ_header_section_with_mandatory_true.rb new file mode 100644 index 000000000..945d4a910 --- /dev/null +++ b/db/migrate/20161004175442_fix_type_de_champ_header_section_with_mandatory_true.rb @@ -0,0 +1,11 @@ +class FixTypeDeChampHeaderSectionWithMandatoryTrue < ActiveRecord::Migration + class TypeDeChamp < ActiveRecord::Base + + end + + def change + TypeDeChamp.all.each do |type_de_champ| + type_de_champ.update_column(:mandatory, false) if type_de_champ.type_champ == 'header_section' + end + end +end diff --git a/db/schema.rb b/db/schema.rb index a238ccf10..09ad2760b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160927154248) do +ActiveRecord::Schema.define(version: 20161004175442) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index 493dd64a4..c50d36a4f 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -34,7 +34,7 @@ describe Admin::TypesDeChampController, type: :controller do describe '#update' do let(:libelle) { 'mon libelle' } - let(:type_champ) { 'text' } + let(:type_champ) { 'header_section' } let(:description) { 'titi' } let(:order_place) { '' } let(:types_de_champ_id) { '' } @@ -78,9 +78,23 @@ describe Admin::TypesDeChampController, type: :controller do subject { procedure.types_de_champ.first } it { expect(subject.libelle).to eq('mon libelle') } - it { expect(subject.type_champ).to eq('text') } + it { expect(subject.type_champ).to eq('header_section') } it { expect(subject.description).to eq('titi') } - it { expect(subject.mandatory).to be_truthy } + end + + context 'when type_champ is header_section and mandatory is true' do + let(:type_champ) { 'header_section' } + let(:mandatory) { 'on' } + + before do + request + procedure.reload + end + + subject { procedure.types_de_champ.first } + + it { expect(subject.type_champ).to eq type_champ } + it { expect(subject.mandatory).to be_falsey } end context 'when type_de_champ already exist' do @@ -88,20 +102,24 @@ describe Admin::TypesDeChampController, type: :controller do let(:type_de_champ) { procedure.types_de_champ.first } let(:types_de_champ_id) { type_de_champ.id } let(:libelle) { 'toto' } - let(:type_champ) { 'text' } + let(:type_champ) { 'header_section' } let(:description) { 'citrouille' } let(:order_place) { '0' } let(:mandatory) { 'on' } + before do request procedure.reload end + subject { procedure.types_de_champ.first } + it { expect(subject.libelle).to eq('toto') } - it { expect(subject.type_champ).to eq('text') } + it { expect(subject.type_champ).to eq('header_section') } it { expect(subject.description).to eq('citrouille') } it { expect(subject.order_place).to eq(0) } it { expect(subject.order_place).to be_truthy } + it { expect(subject.mandatory).to be_falsey } end end context 'when procedure is not found' do