Commit 3c626a36 authored by Greg Gard's avatar Greg Gard

transfers now a child of admission

parent c83b64bc
......@@ -37,9 +37,7 @@ $(function(){
$('#form_errors', scope).show().html($.map(data.errors, (e) => $('<li>').text(e)));
})
// FIXME: jquery-ujs and/or rails returns malformed json response on destroy that gets rendered as string
// remove this handler once we figure out why. falling back to old faithful...
// - maybe todo with jsonapi -- chrome says response is fine but jquery doesn't see it as json.
// TODO: refactor to use ujs events. event fires from delete button not form
$('.emt-form #delete_button').on('click', (ev) => {
ev.preventDefault();
if (!confirm('Are you sure?')){ return false };
......
......@@ -15,7 +15,7 @@ class PatientsController < ApplicationController
set_ajax_data(
message: "Patient created.",
patient: @patient.ajax_attributes,
redirect_to: patients_path
redirect_to: edit_patient_path(@patient)
)
rescue *user_errors => er
......
......@@ -6,23 +6,23 @@ class TransfersController < ApplicationController
#
def index
@transfers = Transfer.kept.order(created_at: 'desc').page params[:page]
@transfers = resource_parent.transfers.kept.order(created_at: 'desc').page params[:page]
end
#
def new
@transfer = Transfer.new
@transfer = resource_parent.transfers.new
end
#
def create
@transfer = Transfer.new(resource_params)
@transfer = resource_parent.transfers.new(resource_params)
@transfer.save!
set_ajax_data(
message: "Transfer created.",
transfer: @transfer.ajax_attributes,
redirect_to: transfers_path
redirect_to: admission_transfers_path(resource_parent)
)
rescue *user_errors => er
......@@ -35,24 +35,23 @@ class TransfersController < ApplicationController
#
def show
redirect_to transfers_path, notice: "That view isn't implemented."
redirect_to resource_parent, notice: "That view isn't implemented."
end
# render the transfer html
def form
set_resource
render html: @transfer.build_form(Admission.last)
render html: @transfer.build_form(resource_parent)
end
#
def edit
rescue *user_errors => e
redirect_to transfers_path, notice: e
redirect_to admission_transfers_path(resource_parent), notice: e
rescue => e
log_stack_error e
redirect_to transfers_path, notice: stack_error_message
redirect_to admission_transfers_path(resource_parent), notice: stack_error_message
end
#
......@@ -61,7 +60,7 @@ class TransfersController < ApplicationController
set_ajax_data(
message: "Transfer updated.",
transfer: @transfer.ajax_attributes,
redirect_to: transfers_path
redirect_to: admission_transfers_path(resource_parent)
)
rescue *user_errors => er
......@@ -78,7 +77,7 @@ class TransfersController < ApplicationController
set_ajax_data(
message: "Transfer deleted.",
transfer: @transfer.ajax_attributes,
redirect_to: transfers_path
redirect_to: admission_transfers_path(resource_parent)
)
rescue *user_errors => er
......@@ -92,8 +91,13 @@ class TransfersController < ApplicationController
# ************************
private
def set_resource_parent
return unless _id = params[:admission_id]
self.resource_parent = @admission = Admission.kept.find(_id)
end
def set_resource
self.resource = @transfer = Transfer.kept.find(params[:id])
self.resource = @transfer = resource_parent.transfers.kept.find(params[:id])
end
......
......@@ -117,7 +117,6 @@ module ApplicationHelper
[
{label: "Admissions", url: patient_admissions_path(resource.id)},
{label: "Transfers", url: patient_transfers_path(resource.id)},
{label: "Allergies", url: patient_allergies_path(resource.id)},
{label: "Labs", url: patient_diagnostic_procedures_path(resource.id)},
{label: "Medications", url: patient_medication_orders_path(resource.id)},
......@@ -134,6 +133,8 @@ module ApplicationHelper
end
def emt_patient_header(resource)
return unless resource
%Q[<div class='emt-patient_header'>
#{tag.h4 "#{resource.name} MR#: #{resource.mr}"}
#{emt_patient_nav_buttons(resource)}
......@@ -164,6 +165,8 @@ module ApplicationHelper
end
def emt_admission_header(resource)
return unless resource
%Q[<div class='emt-admission_header'>
#{tag.h4 "Admission Date: #{resource.fmt_moment}"}
#{emt_admission_nav_buttons(resource)}
......@@ -191,6 +194,8 @@ module ApplicationHelper
end
def emt_transfer_header(resource)
return unless resource
%Q[<div class='emt-transfer_header'>
#{tag.h4 "Transfer Date: #{resource.fmt_created_at}"}
#{emt_transfer_nav_buttons(resource)}
......
......@@ -6,6 +6,10 @@ class Admission < ApplicationRecord
[:patient_id, :facility_id, :moment]
end
def self.select_options
kept.all.map{|p| ["Admission: #{p.fmt_moment}", p.id]}
end
# *************************
# validations/callbacks
#
......@@ -30,6 +34,7 @@ class Admission < ApplicationRecord
belongs_to :facility
belongs_to :patient
has_many :transfers
has_many :diagnoses
has_many :symptoms
has_many :observations
......@@ -50,6 +55,10 @@ class Admission < ApplicationRecord
# *****************************
# utils
#
def name
"#{self.class.name}: #{fmt_moment}"
end
def fmt_patient_name
patient&.name || EMT::UKNOWN
end
......
......@@ -37,11 +37,11 @@ class Patient < ApplicationRecord
# instance methods
#
has_many :admissions
has_many :transfers, :through => :admissions
has_many :diagnoses, :through => :admissions
has_many :symptoms, :through => :admissions
has_many :observations, :through => :admissions
has_many :transfers
has_many :allergies
has_many :medication_orders
has_many :diagnostic_procedures
......
......@@ -18,8 +18,7 @@ class Transfer < ApplicationRecord
end
validate do |m|
m.errors.add :patient_id, "missing/invalid" unless m.patient_id.to_i > 0
m.errors.add :facility_id, "missing/invalid" unless m.facility_id.to_i > 0
m.errors.add :admission_id, "missing/invalid" unless m.admission_id.to_i > 0
m.errors.add :form_sent_at, "missing/invalid" unless valid_date(m, :form_sent_at, required: false)
end
......@@ -27,19 +26,10 @@ class Transfer < ApplicationRecord
# ***************************
# instance methods
#
belongs_to :facility
belongs_to :patient
belongs_to :admission
has_many :diagnoses
has_many :symptoms
has_many :observations
def fmt_patient_name
patient&.name || EMT::UKNOWN
end
def fmt_facility_name
facility&.name || EMT::UKNOWN
def fmt_admission_name
admission&.name || EMT::UKNOWN
end
#
......
<div class='emt-transfers-form'>
<%= form_with model: transfer, class: 'emt-form' do |f| %>
<%= form_with model: [resource_parent, transfer], class: 'emt-form' do |f| %>
<%= emt_form_errors(transfer) %>
<div>
<%= f.label :patient_id, "Patient" %>
<%= f.select :patient_id, Patient.select_options, include_blank: 'Select patient' %>
</div>
<div>
<%= f.label :facility_id, "Referring Facility" %>
<%= f.select :facility_id, Facility.select_options, include_blank: 'Select facility' %>
<%= f.label :admission_id, "Admission" %>
<%= f.select :admission_id, Admission.select_options, include_blank: 'Select admission date' %>
</div>
<div>
......
......@@ -15,15 +15,6 @@
</div>
</div>
<div>
<div class='emt-card'>
<div><%= link_to "Transfers", transfers_path %></div>
<div>Add and view/print Transfer forms</div>
</div>
</div>
</div>
<div class='emt-grid-3'>
<div>
<div class='emt-card'>
<div><%= link_to "Facilities", facilities_path %></div>
......
<div class='emt-transfers-edit'>
<%= emt_transfer_header @transfer %>
<%= emt_admission_header @transfer %>
<%= render 'form', transfer: @transfer %>
</div>
\ No newline at end of file
<div class='emt-transfers'>
<%= link_to "Add Transfer", new_transfer_path, class: 'emt-btn emt-brand' %>
<%= emt_admission_header resource_parent %>
<%= link_to "Add Transfer", new_admission_transfer_path(resource_parent), class: 'emt-btn emt-brand' %>
<%= paginate @transfers %>
<table class='emt-table'>
<tr>
<th>Form Created</th>
<th>Form Sent Date/Time</th>
<th>Patient Name</th>
<th>Referring Facility</th>
<th>Notes</th>
<th>Actions</th>
</tr>
<% @transfers.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.fmt_created_at, edit_transfer_path(f) %></td>
<td><%= link_to f.fmt_created_at, edit_admission_transfer_path(resource_parent, f) %></td>
<td><%= f.fmt_form_sent_at %></td>
<td><%= f.fmt_patient_name %></td>
<td><%= f.fmt_facility_name %></td>
<td><%= f.notes %></td>
<td><%= link_to "View Form", form_transfer_path(f), class: 'emt-btn emt-brand' %></td>
<td><%= link_to "View Form", form_admission_transfer_path(resource_parent, f), class: 'emt-btn emt-brand' %></td>
</tr>
<% end %>
......
<div class='emt-transfers-new'>
<%= emt_admission_header resource_parent %>
<%= render 'form', transfer: @transfer %>
</div>
\ No newline at end of file
......@@ -10,7 +10,9 @@ Rails.application.routes.draw do
resources :admissions do
resources :patients
resources :transfers
resources :transfers do
get 'form', on: :member
end
resources :diagnoses
resources :observations
resources :symptoms
......@@ -18,7 +20,6 @@ Rails.application.routes.draw do
resources :patients do
resources :admissions
resources :transfers
resources :allergies
resources :diagnoses
resources :diagnostic_procedures
......@@ -28,9 +29,6 @@ Rails.application.routes.draw do
resources :transfers do
get 'dashboard', on: :collection
get 'form', on: :member
resources :patients
resources :admissions
end
end # scope
......
class MakeTransferAChildOfAdmission < ActiveRecord::Migration[5.2]
def change
remove_column :transfers, :patient_id
remove_column :transfers, :facility_id
add_column :transfers, :admission_id, :integer
end
end
class SetNotNullOnTransfersAdmissionId < ActiveRecord::Migration[5.2]
def change
change_column :transfers, :admission_id, :integer, null: false
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_05_31_081136) do
ActiveRecord::Schema.define(version: 2018_06_01_090403) do
create_table "admissions", force: :cascade do |t|
t.integer "facility_id", null: false
......@@ -118,7 +118,6 @@ ActiveRecord::Schema.define(version: 2018_05_31_081136) do
end
create_table "transfers", force: :cascade do |t|
t.integer "patient_id", null: false
t.integer "facility_id", null: false
t.text "data"
t.text "notes"
......@@ -126,9 +125,9 @@ ActiveRecord::Schema.define(version: 2018_05_31_081136) do
t.datetime "discarded_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "admission_id", null: false
t.index ["discarded_at"], name: "index_transfers_on_discarded_at"
t.index ["facility_id"], name: "index_transfers_on_facility_id"
t.index ["patient_id"], name: "index_transfers_on_patient_id"
end
create_table "treatments", force: :cascade do |t|
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment