Skip to content

Commit 550258f

Browse files
authored
Merge pull request #531 from alphagov/clear-threshold-timestamps-when-decrementing
Clear threshold timestamps when decrementing
2 parents 6ecb4e1 + 89c064d commit 550258f

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
@@ -359,6 +359,15 @@ def increment_signature_count!(time = Time.current)
359359
def decrement_signature_count!(time = Time.current)
360360
updates = []
361361

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

@@ -385,6 +394,18 @@ def at_threshold_for_debate?
385394
end
386395
end
387396

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

spec/models/petition_spec.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,10 @@
13111311
FactoryGirl.create(:open_petition, {
13121312
signature_count: signature_count,
13131313
last_signed_at: 2.days.ago,
1314-
updated_at: 2.days.ago
1314+
updated_at: 2.days.ago,
1315+
response_threshold_reached_at: 2.days.ago,
1316+
debate_threshold_reached_at: 2.days.ago,
1317+
debate_state: 'awaiting'
13151318
})
13161319
end
13171320

@@ -1335,6 +1338,37 @@
13351338
}.not_to change{ petition.signature_count }
13361339
end
13371340
end
1341+
1342+
context "when the signature count crosses below the threshold for a response" do
1343+
let(:signature_count) { 10 }
1344+
1345+
before do
1346+
expect(Site).to receive(:threshold_for_response).and_return(10)
1347+
end
1348+
1349+
it "resets the timestamp" do
1350+
petition.decrement_signature_count!
1351+
expect(petition.response_threshold_reached_at).to be_nil
1352+
end
1353+
end
1354+
1355+
context "when the signature count crosses below the threshold for a debate" do
1356+
let(:signature_count) { 100 }
1357+
1358+
before do
1359+
expect(Site).to receive(:threshold_for_debate).and_return(100)
1360+
end
1361+
1362+
it "records the time it happened" do
1363+
petition.decrement_signature_count!
1364+
expect(petition.debate_threshold_reached_at).to be_nil
1365+
end
1366+
1367+
it "sets the debate_state to 'pending'" do
1368+
petition.decrement_signature_count!
1369+
expect(petition.debate_state).to eq("pending")
1370+
end
1371+
end
13381372
end
13391373

13401374
describe "at_threshold_for_moderation?" do

0 commit comments

Comments
 (0)