Skip to content

Commit 89c064d

Browse files
committed
Clear threshold timestamps when decrementing
If we're invalidating a bunch of signatures and it goes through a threshold like 100,000 for a debate or 10,000 for a response then we need to clear the relevant timestamp so that it doesn't appear in the wrong lists in both the public and admin websites.
1 parent d7191a4 commit 89c064d

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

app/models/petition.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,15 @@ def increment_signature_count!(time = Time.current)
358358
def decrement_signature_count!(time = Time.current)
359359
updates = []
360360

361+
if below_threshold_for_debate?
362+
updates << "debate_threshold_reached_at = NULL"
363+
updates << "debate_state = 'pending'"
364+
end
365+
366+
if below_threshold_for_response?
367+
updates << "response_threshold_reached_at = NULL"
368+
end
369+
361370
updates << "signature_count = greatest(signature_count - 1, 1)"
362371
updates << "updated_at = :now"
363372

@@ -384,6 +393,18 @@ def at_threshold_for_debate?
384393
end
385394
end
386395

396+
def below_threshold_for_response?
397+
if response_threshold_reached_at?
398+
signature_count <= Site.threshold_for_response
399+
end
400+
end
401+
402+
def below_threshold_for_debate?
403+
if debate_threshold_reached_at?
404+
signature_count <= Site.threshold_for_debate
405+
end
406+
end
407+
387408
def signatures_by_country
388409
country_petition_journals.joins(:location).preload(:location).to_a.sort_by(&:name)
389410
end

spec/models/petition_spec.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,10 @@
13011301
FactoryGirl.create(:open_petition, {
13021302
signature_count: signature_count,
13031303
last_signed_at: 2.days.ago,
1304-
updated_at: 2.days.ago
1304+
updated_at: 2.days.ago,
1305+
response_threshold_reached_at: 2.days.ago,
1306+
debate_threshold_reached_at: 2.days.ago,
1307+
debate_state: 'awaiting'
13051308
})
13061309
end
13071310

@@ -1325,6 +1328,37 @@
13251328
}.not_to change{ petition.signature_count }
13261329
end
13271330
end
1331+
1332+
context "when the signature count crosses below the threshold for a response" do
1333+
let(:signature_count) { 10 }
1334+
1335+
before do
1336+
expect(Site).to receive(:threshold_for_response).and_return(10)
1337+
end
1338+
1339+
it "resets the timestamp" do
1340+
petition.decrement_signature_count!
1341+
expect(petition.response_threshold_reached_at).to be_nil
1342+
end
1343+
end
1344+
1345+
context "when the signature count crosses below the threshold for a debate" do
1346+
let(:signature_count) { 100 }
1347+
1348+
before do
1349+
expect(Site).to receive(:threshold_for_debate).and_return(100)
1350+
end
1351+
1352+
it "records the time it happened" do
1353+
petition.decrement_signature_count!
1354+
expect(petition.debate_threshold_reached_at).to be_nil
1355+
end
1356+
1357+
it "sets the debate_state to 'pending'" do
1358+
petition.decrement_signature_count!
1359+
expect(petition.debate_state).to eq("pending")
1360+
end
1361+
end
13281362
end
13291363

13301364
describe "at_threshold_for_moderation?" do

0 commit comments

Comments
 (0)