Merge pull request #10968 from demarches-simplifiees/tech-nettoyage-options-header-section
Tech: task, nettoyage des options des header_section
This commit is contained in:
commit
2b8e2b41da
2 changed files with 118 additions and 0 deletions
23
app/tasks/maintenance/clean_header_section_options_task.rb
Normal file
23
app/tasks/maintenance/clean_header_section_options_task.rb
Normal 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
|
|
@ -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
|
Loading…
Reference in a new issue