Skip to content

Commit 30d450c

Browse files
authored
Remove reminder's deletion from MedicationsController (#2057)
1 parent 52438eb commit 30d450c

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

Diff for: app/controllers/medications_controller.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ def update
9292
# DELETE /medications/1.json
9393
def destroy
9494
@medication.destroy
95-
TakeMedicationReminder.where(medication_id: @medication.id).destroy_all
96-
RefillReminder.where(medication_id: @medication.id).destroy_all
95+
9796
redirect_to_path(medications_path)
9897
end
9998

Diff for: app/models/medication.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class Medication < ApplicationRecord
5050
extend FriendlyId
5151
friendly_id :name
5252
belongs_to :user, foreign_key: :user_id
53-
has_one :take_medication_reminder
54-
has_one :refill_reminder
53+
has_one :take_medication_reminder, dependent: :destroy
54+
has_one :refill_reminder, dependent: :destroy
5555
accepts_nested_attributes_for :take_medication_reminder
5656
accepts_nested_attributes_for :refill_reminder
5757
validates :name, :dosage, :refill, :user_id, :total, :strength, :dosage_unit,

Diff for: spec/requests/medications_spec.rb

+14-14
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,15 @@ def put_update(medication_params)
236236
end
237237

238238
context 'when medication has a daily reminder' do
239-
let!(:medication) do
240-
create(:medication, :with_daily_reminder, user: user)
241-
end
239+
let!(:medication) { create(:medication, user: user) }
240+
241+
before { medication.take_medication_reminder.update(active: true) }
242242

243243
it 'destroys the medication' do
244-
expect(TakeMedicationReminder.active.count).to eq(1)
245-
expect { delete medication_path(medication) }
246-
.to(change(Medication, :count).by(-1))
247-
expect(TakeMedicationReminder.active.count).to eq(0)
244+
expect {
245+
delete medication_path(medication)
246+
}.to change { Medication.count }.by(-1)
247+
.and change { TakeMedicationReminder.count }.to(0)
248248
end
249249

250250
it 'redirects to the medications path for html requests' do
@@ -259,15 +259,15 @@ def put_update(medication_params)
259259
end
260260

261261
context 'when medication has a refill reminder' do
262-
let!(:medication) do
263-
create(:medication, :with_refill_reminder, user: user)
264-
end
262+
let!(:medication) { create(:medication, user: user) }
263+
264+
before { medication.refill_reminder.update(active: true) }
265265

266266
it 'destroys the medication' do
267-
expect(RefillReminder.active.count).to eq(1)
268-
expect { delete medication_path(medication) }
269-
.to(change(Medication, :count).by(-1))
270-
expect(RefillReminder.active.count).to eq(0)
267+
expect {
268+
delete medication_path(medication)
269+
}.to change { Medication.count }.by(-1)
270+
.and change { RefillReminder.count }.to(0)
271271
end
272272

273273
it 'redirects to the medications path for html requests' do

0 commit comments

Comments
 (0)