-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autodeletion #2923
Draft
elimbaum
wants to merge
6
commits into
main
Choose a base branch
from
eli/autodelete-patients
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Autodeletion #2923
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0638a2c
initial attempt, no tests
elimbaum d3d4956
cleanup, sanity check
elimbaum 3626d13
fix concern, cleanup
elimbaum 331447e
remove delete_date from patient; already moved
elimbaum cb9f895
add tests
elimbaum 05a3cc5
oops typo
elimbaum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,32 @@ | ||
# Allows models to be deleted after some period of time | ||
# By making this a concern, we can reuse the same code for Patients and | ||
# ArchivedPatients | ||
module Autodeletable | ||
extend ActiveSupport::Concern | ||
|
||
# instance method | ||
def delete_date | ||
# if config is blank, we return nil here | ||
initial_call_date + Config.days_until_delete if Config.days_until_delete.present? | ||
end | ||
|
||
# This funny business allows us to call both Patient.autodelete! and | ||
# ArchivedPatient.autodelete! without any extra code | ||
# | ||
# (Any modules that include this Concern will get all methods in the | ||
# ClassMethods module, below, as new class methods of their own!) | ||
def self.included(base) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
def autodelete! | ||
find_each do |p| | ||
d = p.delete_date | ||
# only delete if a delete date exists and is in the past. | ||
p.destroy! if d.present? && d < Time.zone.today | ||
end | ||
end | ||
end | ||
end | ||
|
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
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
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
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,112 @@ | ||
require 'test_helper' | ||
|
||
class AutodeletableTest < ActiveSupport::TestCase | ||
before do | ||
@line = create :line, name: 'DC' | ||
@pt_a = create :patient, | ||
name: 'Amanda', | ||
primary_phone: '222-222-2222', | ||
line: @line, | ||
initial_call_date: 10.days.ago | ||
@pt_b = create :patient, | ||
name: 'Bethany', | ||
primary_phone: '333-333-3333', | ||
line: @line, | ||
initial_call_date: 0.days.ago | ||
@pt_c = create :patient, | ||
name: 'Charley', | ||
primary_phone: '444-444-4444', | ||
line: @line, | ||
initial_call_date: 400.days.ago | ||
@pt_d = create :archived_patient, | ||
line: @line, | ||
initial_call_date: 400.days.ago | ||
|
||
end | ||
|
||
# by default, no deletion: config is nil | ||
describe 'without autodeletion' do | ||
it 'should return nil deletion date' do | ||
assert_nil Config.days_until_delete | ||
|
||
assert_nil @pt_a.delete_date | ||
|
||
assert_no_difference 'Patient.count' do | ||
Patient.autodelete! | ||
end | ||
|
||
assert_no_difference 'ArchivedPatient.count' do | ||
ArchivedPatient.autodelete! | ||
end | ||
end | ||
end | ||
|
||
describe 'with autodeletion' do | ||
before do | ||
c = Config.find_or_create_by(config_key: 'days_until_delete') | ||
c.config_value = { options: ["100"] } | ||
c.save! | ||
end | ||
|
||
it 'should only delete patients outside the window' do | ||
assert_difference 'Patient.count', -1 do | ||
Patient.autodelete! | ||
end | ||
|
||
# specifically, that should be pt_c | ||
refute_includes Patient.all, @pt_c | ||
|
||
assert_difference 'ArchivedPatient.count', -1 do | ||
ArchivedPatient.autodelete! | ||
end | ||
end | ||
end | ||
|
||
describe 'edge cases' do | ||
describe 'config 0 days' do | ||
before do | ||
c = Config.find_or_create_by(config_key: 'days_until_delete') | ||
c.config_value = { options: ["0"] } | ||
c.save! | ||
end | ||
|
||
it 'if config is 0, delete all patients now' do | ||
Timecop.freeze(1.hour.ago) do | ||
Patient.autodelete! | ||
# delete doesn't occur because now hasn't happened yet ;) | ||
assert_equal 1, Patient.count | ||
end | ||
|
||
Timecop.freeze(1.day.after) do | ||
# later, now has happened | ||
Patient.autodelete! | ||
assert_equal 0, Patient.count | ||
end | ||
end | ||
end | ||
|
||
describe 'nonzero config on the edge' do | ||
before do | ||
c = Config.find_or_create_by(config_key: 'days_until_delete') | ||
c.config_value = { options: ["10"] } | ||
c.save! | ||
end | ||
|
||
it 'deletes pt_a on the right day' do | ||
Timecop.freeze(1.day.ago) do | ||
Patient.autodelete! | ||
assert_includes Patient.all, @pt_a | ||
end | ||
|
||
# present day - doesn't delete because <, not <= | ||
Patient.autodelete! | ||
assert_includes Patient.all, @pt_a | ||
|
||
Timecop.freeze(1.day.after) do | ||
Patient.autodelete! | ||
refute_includes Patient.all, @pt_a | ||
end | ||
Comment on lines
+92
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok this is a lil weird, because i'm using |
||
end | ||
end | ||
end | ||
end |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is to gnarly i can make a change ;)