task: clean header_section options

This commit is contained in:
benoitqueyron 2024-10-18 15:54:11 +02:00
parent fcb868c6a8
commit 1169dae310
No known key found for this signature in database
GPG key ID: AD3C38C9ACA84135
2 changed files with 118 additions and 0 deletions

View file

@ -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

View file

@ -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