-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
data(conditional): add a maintenance task to update conditions based …
…on commune or epci champ
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
app/tasks/maintenance/update_conditions_based_on_commune_or_epci_champ_task.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
module Maintenance | ||
class UpdateConditionsBasedOnCommuneOrEpciChampTask < MaintenanceTasks::Task | ||
include Logic | ||
|
||
def collection | ||
ProcedureRevision.all | ||
end | ||
|
||
def process(revision) | ||
tdc_to_update_ids = [] | ||
|
||
tdcs = revision.types_de_champ_public | ||
|
||
tdcs.where.not(condition: nil).pluck(:condition, :id).each do |condition, id| | ||
if tdcs.where(stable_id: condition.sources, type_champ: ["communes", "epci"]).present? | ||
tdc_to_update_ids << id | ||
end | ||
end | ||
|
||
tdc_to_update_ids.each do |id| | ||
tdc = tdcs.find_by(id:) | ||
|
||
condition = tdc.condition | ||
|
||
if condition.is_a?(And) | ||
new_operands = new_operands_from(tdcs, condition) | ||
tdc.update!(condition: ds_and(new_operands)) | ||
elsif condition.is_a?(Or) | ||
new_operands = new_operands_from(tdcs, condition) | ||
tdc.update!(condition: ds_or(new_operands)) | ||
elsif condition.is_a?(NotEq) | ||
tdc.update!(condition: ds_not_in_departement(condition.left, condition.right)) | ||
elsif condition.is_a?(Eq) | ||
tdc.update!(condition: ds_in_departement(condition.left, condition.right)) | ||
end | ||
end | ||
end | ||
|
||
def count | ||
collection.count | ||
end | ||
|
||
private | ||
|
||
def new_operands_from(tdcs, condition) | ||
condition.operands.map do |sub_condition| | ||
if tdcs.where(stable_id: sub_condition.sources, type_champ: ["communes", "epci"]).present? | ||
if sub_condition.is_a?(NotEq) | ||
ds_not_in_departement(sub_condition.left, sub_condition.right) | ||
elsif sub_condition.is_a?(Eq) | ||
ds_in_departement(sub_condition.left, sub_condition.right) | ||
else | ||
sub_condition | ||
end | ||
else | ||
sub_condition | ||
end | ||
end | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
spec/tasks/maintenance/update_conditions_based_on_commune_or_epci_champ_task_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
module Maintenance | ||
describe UpdateConditionsBasedOnCommuneOrEpciChampTask do | ||
include Logic | ||
describe "#process" do | ||
subject(:process) { described_class.process(revision) } | ||
|
||
let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :communes }, { type: :epci }, { type: :text }]) } | ||
let(:revision) { procedure.active_revision } | ||
|
||
let(:text_tdc) { revision.types_de_champ_public.where(type_champ: 'text').first } | ||
let(:commune_tdc) { revision.types_de_champ_public.where(type_champ: 'communes').first } | ||
let(:epci_tdc) { revision.types_de_champ_public.where(type_champ: 'epci').first } | ||
|
||
before { text_tdc.update(condition: ds_and([ds_eq(champ_value(commune_tdc.stable_id), constant('11')), ds_not_eq(champ_value(epci_tdc.stable_id), constant('84'))])) } | ||
|
||
it "updates condition" do | ||
expect(text_tdc.condition).to eq ds_and([ds_eq(champ_value(commune_tdc.stable_id), constant('11')), ds_not_eq(champ_value(epci_tdc.stable_id), constant('84'))]) | ||
|
||
subject | ||
|
||
text_tdc.reload | ||
|
||
expect(text_tdc.condition).to eq ds_and([ds_in_departement(champ_value(commune_tdc.stable_id), constant('11')), ds_not_in_departement(champ_value(epci_tdc.stable_id), constant('84'))]) | ||
end | ||
end | ||
end | ||
end |