Skip to content

Commit b10b6f9

Browse files
Fix position validation error when editing stratified sortitions with imported participants (#23)
* Fix false positive position * Update line too long * Fix lint
1 parent 3b24563 commit b10b6f9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

app/forms/decidim/stratified_sortitions/admin/stratified_sortitions_form.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ def validate_strata_attributes
7979

8080
errors.add(:strata, :cannot_change_stratum_kind_with_sample_participants) if existing_stratum.kind != stratum_form.kind
8181

82-
errors.add(:strata, :cannot_change_stratum_position_with_sample_participants) if existing_stratum.position != stratum_form.position.to_i
82+
if existing_stratum.position.present? && existing_stratum.position != stratum_form.position.to_i
83+
errors.add(:strata, :cannot_change_stratum_position_with_sample_participants)
84+
end
8385
end
8486
end
8587

@@ -119,7 +121,9 @@ def validate_substrata_attributes(existing_stratum, stratum_form)
119121
errors.add(:strata, :cannot_change_substratum_range_with_sample_participants)
120122
end
121123

122-
errors.add(:strata, :cannot_change_substratum_position_with_sample_participants) if existing_substratum.position != substratum_form.position.to_i
124+
if existing_substratum.position.present? && existing_substratum.position != substratum_form.position.to_i
125+
errors.add(:strata, :cannot_change_substratum_position_with_sample_participants)
126+
end
123127
end
124128
end
125129

spec/forms/decidim/sortitions/admin/stratified_sortitions_form_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ module Admin
227227

228228
include_examples "blocks the change with error", "cannot_change_substratum_position_with_sample_participants"
229229
end
230+
231+
context "when substratum position is nil in DB and form sends a position" do
232+
let!(:substratum) { create(:substratum, stratum:, name: { en: "18-25" }, value: "young", range: nil, position: nil, max_quota_percentage: "10") }
233+
let(:stratum_params) { base_stratum_params.merge(substrata: { substratum.id.to_s => base_substratum_params.merge(position: 0) }) }
234+
235+
include_examples "allows the change"
236+
end
237+
end
238+
239+
context "when stratum position is nil in DB and form sends a position" do
240+
let!(:stratum) { create(:stratum, stratified_sortition:, name: { en: "Age" }, kind: "value", position: nil) }
241+
let!(:substratum) { create(:substratum, stratum:, name: { en: "18-25" }, value: "young", range: nil, position: 0, max_quota_percentage: "10") }
242+
let(:stratum_params) { base_stratum_params.merge(position: 0) }
243+
244+
include_examples "allows the change"
230245
end
231246
end
232247
end

0 commit comments

Comments
 (0)