Skip to content
This repository has been archived by the owner on Apr 30, 2018. It is now read-only.

Commit

Permalink
Create new medication records if passed into medical_records
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbarclay committed Mar 13, 2017
1 parent b934474 commit 8438088
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 125 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def server_error(exception)
puts exception
unless performed?
respond_to do |format|
format.html { render status: status, json: status }
format.html { render status: :error, json: status }
format.all { head status }
end
end
Expand Down
45 changes: 22 additions & 23 deletions app/controllers/medical_records_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ class MedicalRecordsController < ApplicationController
before_action :authenticate_user

def create
medicine = medicine_params
medications = params[:medications]
@medical_record = MedicalRecord.new medical_record_params

if @medical_record.save
if not medicine.nil?
save_medicine_in_db(medicine, @medical_record.id)
puts Medice.all.to_json
unless medications.nil?
create_medications(medications, @medical_record.id)
end
render status: 201, json: { success: true }
else
Expand Down Expand Up @@ -38,22 +37,6 @@ def filter_medical_records_keys(medical_records)
{ id: medical_record.id, exam_notes: medical_record.exam_notes, created_at: medical_record.created_at.to_i }
end
end

def save_medicine_in_db(medicine, medical_record_id)
if medicine.kind_of?(Array)
medicine.each do |med|
med.medical_record_id = medical_record_id
Medicine.new med
end
else
medicine = JSON.parse(medicine).attributes
@med = Medicine.new(name: medicine, patient_id: medicine['patient_id'])
puts @med.to_json
@med.medical_record_id = medical_record_id
@med.save
puts @med.errors.full_message
end
end

def medical_record_params
params.require(:medical_record).permit(:summary, :date, :exam_notes, :signature, :temperature, :medications, :eyes, :oral,
Expand All @@ -68,8 +51,24 @@ def medical_record_params
:respiratoryA, :patient_id)
end

def medicine_params
params.require(:medicine).permit(:name, :patient_id)
def create_medications(medications, medical_record_id)
success = nil
medication_records = parse_medications(medications, medical_record_id)
Medication.transaction do
success = medication_records.map(&:save)
unless success.all?
raise ActiveRecord::Rollback
end
end
success
end

def parse_medications(medications, medical_record_id)
medications.map do |medication|
medication['medical_record_id'] = medical_record_id
Medication.new medication.permit(:name, :patient_id, :medical_record_id,
:date)
end
end

end
Expand Down
44 changes: 0 additions & 44 deletions app/controllers/medication_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/medical_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class MedicalRecord < ApplicationRecord
belongs_to :patient

has_many :notes
has_many :medicines
has_many :medications
validates :patient_id, presence: true, allow_bank: false

validates :temperature, presence: true, allow_blank: true, numericality: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/medication.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Medicine < ApplicationRecord
class Medication < ApplicationRecord
belongs_to :medical_record
belongs_to :patient

Expand Down
2 changes: 1 addition & 1 deletion app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Patient < ApplicationRecord

has_many :medical_records

has_many :medicines
has_many :medications

has_many :images

Expand Down
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
post 'signup', to: 'users#create'
post 'user_token', to: 'user_token#create'
post 'login', to: 'user_token#create'
get 'patients/:patient_id/medicine/:id', to: 'medicine#show'
get 'patients/:patient_id/medicine/', to: 'medicine#index'
post 'patients/:patient_id/medications/', to: 'medications#show'
get 'patients/:patient_id/medications/:id', to: 'medications#show'
get 'patients/:patient_id/medications/', to: 'medications#index'
# post 'patient/:patient_id/medical_records/:medical_record_id', to: 'medical_records#create'
# get 'patients/:patient_id/medical_records', to: 'medical_record#index'
# get 'patients/:patient_id/medical_records/:medical_record_id', to: 'medical_records#show'
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20170310084044_create_medication.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateMedication < ActiveRecord::Migration[5.0]
def change
create_table :medication do |t|
create_table :medications do |t|
t.string :name
t.belongs_to :medical_record, index: true
t.belongs_to :patient, index: true
Expand Down
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@
t.text "follow_up_instructions"
end

create_table "medicines", force: :cascade do |t|
create_table "medications", force: :cascade do |t|
t.string "name"
t.integer "medical_record_id"
t.integer "patient_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["medical_record_id"], name: "index_medicines_on_medical_record_id", using: :btree
t.index ["patient_id"], name: "index_medicines_on_patient_id", using: :btree
t.index ["medical_record_id"], name: "index_medications_on_medical_record_id", using: :btree
t.index ["patient_id"], name: "index_medications_on_patient_id", using: :btree
end

create_table "notes", force: :cascade do |t|
Expand Down
13 changes: 0 additions & 13 deletions test/fixtures/medicines.yml

This file was deleted.

4 changes: 2 additions & 2 deletions test/integration/medical_records_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class MedicalRecordsTest < ActionDispatch::IntegrationTest
# end
def setup
@patient_id = patients(:one).id.to_s
@medicine = medicines(:one)
@medicine.medical_record_id = ''
@medication = medications(:one)
@medication.medical_record_id = ''
@medical_record = {
patient_id: @patient_id,
temperature: 38.5,
Expand Down
32 changes: 0 additions & 32 deletions test/integration/medication_test.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/models/medication_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class MedicationTest < ActiveSupport::TestCase
def setup
@medicine = medicines(:one)
@medicine = medications(:one)
end

test 'medicine is valid' do
Expand Down

0 comments on commit 8438088

Please sign in to comment.