From da34afc8826ae931e4e87454a14f12fd77bfa7f9 Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Tue, 24 Jan 2023 23:06:12 +0000 Subject: [PATCH] Parameterize Member#regenerate_notifications --- app/models/member.rb | 4 ++-- test/models/member_test.rb | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/models/member.rb b/app/models/member.rb index be86f44..e318963 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -48,7 +48,7 @@ class Member < ApplicationRecord save! end - def regenerate_notifications + def regenerate_notifications(from=Date.today) notifications.where(status: 'scheduled').delete_all dates = notifications.pluck(:to_be_sent_on) @@ -63,7 +63,7 @@ class Member < ApplicationRecord { to_be_sent_on: expires_on + 60.days, template: "expired_60d_ago" }, { to_be_sent_on: expires_on + 90.days, template: "cancelled" }, ].reject do |n| - n[:to_be_sent_on].past? || dates.include?(n[:to_be_sent_on]) + n[:to_be_sent_on].before?(from) || dates.include?(n[:to_be_sent_on]) end.each do |n| notifications.create(n.merge(status: "scheduled")) end diff --git a/test/models/member_test.rb b/test/models/member_test.rb index 7388cd6..51f8bf1 100644 --- a/test/models/member_test.rb +++ b/test/models/member_test.rb @@ -13,32 +13,32 @@ class MemberTest < ActiveSupport::TestCase test "no expired in the first year and 90 days" do (1.year + 90.days).in_days.to_i.times do |n| Timecop.freeze(Date.today + n.days) do - assert_not_equal @member.expected_status, :cancelled + assert_not_equal :cancelled, @member.expected_status end end end test "expired after 1 year and 90 days" do Timecop.freeze(Date.today + 1.year + 90.days) do - assert_equal @member.expected_status, :cancelled + assert_equal :cancelled, @member.expected_status end end test "regenerate notifications creates all 6 notifications" do @member.regenerate_notifications - assert_equal @member.notifications.count, 6 + assert_equal 6, @member.notifications.count end test "regenerate_notifications does not create notifications in the past" do Timecop.freeze(Date.today + 1.year) do @member.regenerate_notifications - assert_equal @member.notifications.count, 4 + assert_equal 4, @member.notifications.count end Timecop.freeze(Date.today + 2.year) do @member.regenerate_notifications - assert_equal @member.notifications.count, 0 + assert_equal 0, @member.notifications.count end end @@ -52,4 +52,10 @@ class MemberTest < ActiveSupport::TestCase assert_equal 1, @member.notifications.where(status: 'sent').count assert_equal 5, @member.notifications.where(status: 'scheduled').count end + + test "regenerate_notifications when given a date generates only notifications after that date" do + @member.regenerate_notifications(Date.today + 1.year) + + assert_equal 4, @member.notifications.count + end end