diff --git a/app/tasks/maintenance/clean_header_section_options_task.rb b/app/tasks/maintenance/clean_header_section_options_task.rb new file mode 100644 index 000000000..7bcd250bb --- /dev/null +++ b/app/tasks/maintenance/clean_header_section_options_task.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Maintenance + class CleanHeaderSectionOptionsTask < MaintenanceTasks::Task + # In the rest of PR 10713 + # TypeDeChamp options may contain options which are not consistent with the + # type_champ (e.g. a ‘header_section’ TypeDeChamp which has a + # drop_down_options key/value in its options). + # The aim here is to clean up the options so that only those wich are useful + # for the type_champ in question. + + def collection + TypeDeChamp + .where(type_champ: 'header_section') + .where.not(options: {}) + .where.not("(SELECT COUNT(*) FROM jsonb_each_text(options)) = 1 AND options ? 'header_section_level'") + end + + def process(tdc) + tdc.update(options: tdc.options.slice(:header_section_level)) + end + end +end diff --git a/spec/tasks/maintenance/clean_header_section_options_task_spec.rb b/spec/tasks/maintenance/clean_header_section_options_task_spec.rb new file mode 100644 index 000000000..ba3571141 --- /dev/null +++ b/spec/tasks/maintenance/clean_header_section_options_task_spec.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe CleanHeaderSectionOptionsTask do + describe '#collection' do + subject(:collection) { described_class.collection } + + context 'clean header_section tdc with header_section_level' do + let(:tdc) { + create(:type_de_champ_header_section, + options: { + 'header_section_level' => '1' + }) + } + + it do + expect(collection).not_to include(tdc) + end + end + + context 'clean header_section tdc with no header_section_level' do + let(:tdc) { + create(:type_de_champ_header_section, + options: {}) + } + + it do + expect(collection).not_to include(tdc) + end + end + + context 'header_section tdc with bad data options' do + let(:tdc) { + create(:type_de_champ_header_section, + options: { + 'header_section_level' => '1', + 'key' => 'value' + }) + } + + it do + expect(collection).to include(tdc) + end + end + + context 'other tdc' do + let(:tdc) { + create(:type_de_champ_textarea, + options: { + 'character_limit' => '400' + }) + } + + it do + expect(collection).not_to include(tdc) + end + end + end + + describe "#process" do + subject(:process) { described_class.process(tdc) } + + context 'bad data in options' do + let(:tdc) { + create(:type_de_champ_header_section, + options: { + 'header_section_level' => '1', + 'key' => 'value' + }) + } + + it do + subject + expect(tdc.reload.options).to eq({ 'header_section_level' => '1' }) + end + end + + context 'only bad data in options' do + let(:tdc) { + create(:type_de_champ_header_section, + options: { + 'key' => 'value' + }) + } + + it do + subject + expect(tdc.reload.options).to eq({}) + end + end + end + end +end