diff --git a/lib/tasks/deployment/20240917131034_remove_empty_options_from_drop_down_list.rake b/lib/tasks/deployment/20240917131034_remove_empty_options_from_drop_down_list.rake new file mode 100644 index 000000000..4cd552361 --- /dev/null +++ b/lib/tasks/deployment/20240917131034_remove_empty_options_from_drop_down_list.rake @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +namespace :after_party do + desc 'Deployment task: remove_empty_options_from_drop_down_list' + task remove_empty_options_from_drop_down_list: :environment do + ids = TypeDeChamp + .where(type_champ: ['drop_down_list', 'multiple_drop_down_list', 'linked_drop_down_list']) + .where("options->'drop_down_options' @> '[\"\"]'::jsonb").ids + + progress = ProgressReport.new(ids.count) + + TypeDeChamp.where(id: ids).select(:id, :options, :type_champ).find_each do |drop_down_list| + drop_down_list.drop_down_options.delete('') + drop_down_list.save!(validate: false) + + progress.inc + end + + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/lib/tasks/deployment/20240917131034_remove_empty_options_from_drop_down_list.rake_spec.rb b/spec/lib/tasks/deployment/20240917131034_remove_empty_options_from_drop_down_list.rake_spec.rb new file mode 100644 index 000000000..3e181f1eb --- /dev/null +++ b/spec/lib/tasks/deployment/20240917131034_remove_empty_options_from_drop_down_list.rake_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +describe '20240917131034_remove_empty_options_from_drop_down_list.rake' do + let(:rake_task) { Rake::Task['after_party:remove_empty_options_from_drop_down_list'] } + + let!(:drop_down_list_with_empty_option) do + create(:type_de_champ_drop_down_list, drop_down_options: ['', '1', '2']) + end + + let!(:drop_down_list_with_other_empty_option) do + create(:type_de_champ_drop_down_list, drop_down_options: ['1', '', '2', '']) + end + + let!(:witness_drop_down_list) do + create(:type_de_champ_drop_down_list, drop_down_options: ['1', '2']) + end + + before do + rake_task.invoke + + [ + drop_down_list_with_empty_option, + drop_down_list_with_other_empty_option, + witness_drop_down_list + ].each(&:reload) + end + + after { rake_task.reenable } + + it 'removes the empty option' do + expect(drop_down_list_with_empty_option.drop_down_options).to eq(['1', '2']) + expect(drop_down_list_with_other_empty_option.drop_down_options).to eq(['1', '2']) + expect(witness_drop_down_list.drop_down_options).to eq(['1', '2']) + end +end