Skip to content

Commit

Permalink
Medication values can now be captured when entering a medication, add…
Browse files Browse the repository at this point in the history
…resses #697.
  • Loading branch information
remomueller committed Mar 7, 2019
1 parent da21ff7 commit 1ebc04b
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 13 deletions.
11 changes: 9 additions & 2 deletions app/controllers/medications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

# Allows editors to track subject medications.
class MedicationsController < ApplicationController
before_action :authenticate_user!
before_action :find_viewable_project_or_redirect, only: [:index, :show]
Expand Down Expand Up @@ -36,6 +39,7 @@ def new
def create
@medication = @subject.medications.where(project: @project).new(medication_params)
if @medication.save
@medication.save_medication_variables!
redirect_to [@project, @subject, @medication], notice: "Medication was successfully created."
else
render :new
Expand All @@ -45,6 +49,7 @@ def create
# PATCH /medications/1
def update
if @medication.update(medication_params)
@medication.save_medication_variables!
redirect_to [@project, @subject, @medication], notice: "Medication was successfully updated."
else
render :edit
Expand All @@ -66,15 +71,17 @@ def find_medication_or_redirect

def medication_params
params.require(:medication).permit(
:position, :name, :start_date_fuzzy_edit, :stop_date_fuzzy_edit,
:position, :name,
:start_date_fuzzy_edit, :stop_date_fuzzy_edit,
:start_date_fuzzy_mo_1, :start_date_fuzzy_mo_2,
:start_date_fuzzy_dy_1, :start_date_fuzzy_dy_2,
:start_date_fuzzy_yr_1, :start_date_fuzzy_yr_2,
:start_date_fuzzy_yr_3, :start_date_fuzzy_yr_4,
:stop_date_fuzzy_mo_1, :stop_date_fuzzy_mo_2,
:stop_date_fuzzy_dy_1, :stop_date_fuzzy_dy_2,
:stop_date_fuzzy_yr_1, :stop_date_fuzzy_yr_2,
:stop_date_fuzzy_yr_3, :stop_date_fuzzy_yr_4
:stop_date_fuzzy_yr_3, :stop_date_fuzzy_yr_4,
medication_variables: {}
)
end
end
1 change: 1 addition & 0 deletions app/models/concerns/medications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module Medications
# Relationships
has_many :medication_variables, -> { current }
has_many :medications
has_many :medication_values
end
end
25 changes: 20 additions & 5 deletions app/models/medication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Medication < ApplicationRecord
# Relationships
belongs_to :project
belongs_to :subject
has_many :medication_values

attr_writer :start_date_fuzzy_mo_1, :start_date_fuzzy_mo_2,
:start_date_fuzzy_dy_1, :start_date_fuzzy_dy_2,
Expand All @@ -24,7 +25,7 @@ class Medication < ApplicationRecord
:stop_date_fuzzy_yr_1, :stop_date_fuzzy_yr_2,
:stop_date_fuzzy_yr_3, :stop_date_fuzzy_yr_4

attr_accessor :start_date_fuzzy_edit, :stop_date_fuzzy_edit
attr_accessor :start_date_fuzzy_edit, :stop_date_fuzzy_edit, :medication_variables

# Methods

Expand Down Expand Up @@ -206,14 +207,14 @@ def set_fuzzy_date(key)
errors.add(key, "day should be 99 if month is unknown")
elsif yr.present? && mo == 99 && dy == 99
if yr <= Time.zone.today.year
date_string = "#{yrs}#{mos}#{dys}"
date_string = "#{yrs}#{mos}#{dys}"
else
errors.add(:"#{key}_yr", "can't be in future")
errors.add(key, "can't be in future")
end
elsif yr.present? && mo.present? && dy == 99
if yr < Time.zone.today.year && mo.in?(1..12) || yr == Time.zone.today.year && mo.in?(1..Time.zone.today.month)
date_string = "#{yrs}#{mos}#{dys}"
date_string = "#{yrs}#{mos}#{dys}"
else
errors.add(:"#{key}_yr", "can't be in future")
errors.add(:"#{key}_mo", "can't be in future")
Expand All @@ -228,7 +229,7 @@ def set_fuzzy_date(key)
errors.add(:"#{key}_dy", "can't be in future")
errors.add(key, "can't be in future")
else
date_string = "#{yrs}#{mos}#{dys}"
date_string = "#{yrs}#{mos}#{dys}"
end
else
errors.add(:"#{key}_yr", "invalid date")
Expand All @@ -247,6 +248,20 @@ def set_fuzzy_date(key)
end

def digit?(value)
return !(/^\d$/ =~ value).nil?
!(/^\d$/ =~ value).nil?
end

def save_medication_variables!
return if medication_variables.blank?

medication_variables.each do |key, value|
medication_variable = project.medication_variables.find_by(id: key)
next unless medication_variable

medication_value = medication_values.where(
project: project, subject: subject, medication_variable: medication_variable
).first_or_create
medication_value.update(value: value)
end
end
end
14 changes: 14 additions & 0 deletions app/models/medication_value.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# Tracks a subject's medication value provided for a project medication
# variable.
class MedicationValue < ApplicationRecord
# Validations
validates :medication_variable_id, uniqueness: { scope: :medication_id }

# Relationships
belongs_to :project
belongs_to :medication_variable
belongs_to :subject
belongs_to :medication
end
1 change: 1 addition & 0 deletions app/models/medication_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ class MedicationVariable < ApplicationRecord

# Relationships
belongs_to :project
has_many :medication_values
end
1 change: 1 addition & 0 deletions app/models/subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def name
has_many :ae_adverse_events, -> { current }
has_many :randomizations, -> { current }
has_many :medications, -> { current }
has_many :medication_values, -> { current }
has_many :sheets, -> { current }
has_many :subject_events, -> { joins(:event).order(:event_date, "events.position") }

Expand Down
12 changes: 8 additions & 4 deletions app/views/medications/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
= form_with model: [medication.project, medication.subject, medication], local: true do |form|
= render "forms/horizontal/text_field", form: form, object: medication, key: :name, autocomplete: "off"
=# render "forms/horizontal/text_field", form: form, object: medication, key: :indication
=# render "forms/horizontal/text_field", form: form, object: medication, key: :dose
=# render "forms/horizontal/text_field", form: form, object: medication, key: :units
=# render "forms/horizontal/text_field", form: form, object: medication, key: :frequency

- @project.medication_variables.each do |medication_variable|
.form-group.row
= form.label medication_variable.name, nil, class: "col-md-2 col-form-label"
.col-md-10
- medication_value = medication.medication_values.find_by(medication_variable: medication_variable)
- value = params.dig(:medication, :medication_variables, medication_variable.id.to_s.to_sym) || medication_value&.value
= text_field_tag "medication[medication_variables][#{medication_variable.id}]", value, class: "form-control", autocomplete: "off", autocorrect: "off", autocapitalize: "none", spellcheck: "false"

.form-group.row
= form.label :start_date, "Start date", class: "col-md-2 col-form-label"
Expand Down
4 changes: 4 additions & 0 deletions app/views/medications/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
.dashboard-container
= render "forms/horizontal/show/text", object: @medication, key: :position
= render "forms/horizontal/show/text", object: @medication, key: :name
- @project.medication_variables.each do |medication_variable|
- medication_value = @medication.medication_values.find_by(medication_variable: medication_variable)
= render "forms/horizontal/show/generic", title: medication_variable.name, content: medication_value&.value

= render "forms/horizontal/show/generic", title: "Start date", content: @medication.fuzzy_pretty(:start_date_fuzzy)
= render "forms/horizontal/show/generic", title: "Stop date", content: @medication.fuzzy_pretty(:stop_date_fuzzy)

Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20190307173518_create_medication_values.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateMedicationValues < ActiveRecord::Migration[6.0]
def change
create_table :medication_values do |t|
t.bigint :project_id
t.bigint :medication_variable_id
t.bigint :subject_id
t.bigint :medication_id
t.string :value
t.timestamps

t.index :project_id
t.index :subject_id
t.index [:medication_variable_id, :medication_id], unique: true, name: "index_med_values_on_medication_and_med_variable"
end
end
end
15 changes: 13 additions & 2 deletions test/controllers/medications_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require "test_helper"

# Tests to assure project editors can create and update subject medications.
class MedicationsControllerTest < ActionDispatch::IntegrationTest
setup do
@project_editor = users(:meds_project_editor)
Expand All @@ -12,7 +15,13 @@ def medication_params
{
name: @medication.name,
start_date_fuzzy: @medication.start_date_fuzzy,
stop_date_fuzzy: @medication.stop_date_fuzzy
stop_date_fuzzy: @medication.stop_date_fuzzy,
medication_variables: {
medication_variables(:indication).id.to_s => "Some reason",
medication_variables(:unit).id.to_s => "tablespoon",
medication_variables(:frequency).id.to_s => "1X per day",
medication_variables(:route).id.to_s => "S.C. - subcutaneous"
}
}
end

Expand All @@ -31,7 +40,9 @@ def medication_params
test "should create medication" do
login(@project_editor)
assert_difference("Medication.count") do
post project_subject_medications_url(@project, @subject), params: { medication: medication_params }
assert_difference("MedicationValue.count", 4) do
post project_subject_medications_url(@project, @subject), params: { medication: medication_params }
end
end
assert_redirected_to [@project, @subject, Medication.last]
end
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/medication_values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
one:
project: medications
medication_variable: indication
subject: meds_01
medication: one
value: This is the reason.

two:
project: medications
medication_variable: unit
subject: meds_01
medication: one
value: tablespoon
7 changes: 7 additions & 0 deletions test/models/medication_value_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class MedicationValueTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 1ebc04b

Please sign in to comment.