Skip to content

Commit

Permalink
Update medications_controller.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
GayathriVenkatesh authored and julianguyen committed Mar 7, 2020
1 parent b67b146 commit 18f8b58
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/controllers/medications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ def edit
redirect_to_medication(@medication)
end

def add_refill(medicine)
if medicine.refill.nil?
time = Time.zone.now.strftime('%d/%m/%Y')
no_of_days = medicine.total / medicine.dosage
no_of_weeks = no_of_days / medicine.weekly_dosage.length
rem = no_of_days % medicine.weekly_dosage.length
days = 7 * no_of_weeks + rem
return time.to_date + days - 1
end
medicine.refill
end

# POST /medications
# POST /medications.json
def create
@medication =
Medication.new(medication_params.merge(user_id: current_user.id))
@medication.refill = add_refill(@medication)
return unless save_refill_to_google_calendar(@medication)

if @medication.save
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/medications_form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def medication_refill
label: t('medications.form.refill'),
value: @medication.refill&.to_date || nil,
info: t('medications.form.refill_hint'),
required: true
required: false
}.merge(medication_basic_props('refill'))
end

Expand Down

0 comments on commit 18f8b58

Please sign in to comment.