Commit 6900c2f9 authored by Greg Gard's avatar Greg Gard

adding admissions; adding chronic gem for date/time parsing

parent 7711fc25
......@@ -60,6 +60,9 @@ gem 'kaminari'
# are/should be responsible for setting scopes and not use automagical gook for this
gem 'discard'
# because it should just be simple...
gem 'chronic'
# **********************
# per env
#
......
......@@ -69,6 +69,7 @@ GEM
chromedriver-helper (1.2.0)
archive-zip (~> 0.10)
nokogiri (~> 1.8)
chronic (0.10.2)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
......@@ -217,6 +218,7 @@ DEPENDENCIES
byebug
capybara (>= 2.15, < 4.0)
chromedriver-helper
chronic
coffee-rails (~> 4.2)
discard
jbuilder (~> 2.5)
......
class AdmissionsController < ApplicationController
#
def index
@admissions = Admission.kept.order(moment: 'desc').page params[:page]
end
#
def new
end
#
def create
@admission.save!
set_ajax_data(
message: "Admission created.",
admission: @admission.ajax_attributes,
redirect_to: admissions_path
)
end
def show
rescue *user_errors => er
set_ajax_user_error er, @admission
rescue => er
set_ajax_stack_error er, @admission
ensure
render_json
end
#
def show
redirect_to admissions_path, notice: "That view isn't implemented."
end
#
def edit
rescue *user_errors => e
redirect_to admissions_path, notice: e
rescue => e
log_stack_error e
redirect_to admissions_path, notice: stack_error_message
end
#
def update
@admission.update! resource_params
set_ajax_data(
message: "Admission updated.",
admission: @admission.ajax_attributes,
redirect_to: admissions_path
)
rescue *user_errors => er
set_ajax_user_error er, @admission
rescue => er
set_ajax_stack_error er, @admission
ensure
render_json
end
def destroy
end
# TODO: undiscard
def destroy
@admission.discard || raise("Unable to delete")
set_ajax_data(
message: "Admission deleted.",
admission: @admission.ajax_attributes,
redirect_to: admissions_path
)
rescue *user_errors => er
set_ajax_user_error er, @admission
rescue => er
set_ajax_stack_error er, @admission
ensure
render_json
end
end
\ No newline at end of file
......@@ -9,6 +9,7 @@ module EMT
# ***************************
# custom errors
DEFAULT_STACK_ERROR_MESSAGE = "There was a system error."
UNKNOWN = "Unkown"
class Error < StandardError; end
class StackError < Error; end
......
......@@ -46,14 +46,16 @@ module EMT
EVAL
end
# - see tests for Date.parse behavior
# - uses Chronic gem -- much more robust/sane/simple date/time parsing
# - if valid and block given passes in valid date and returns value of block
def valid_date(m, v, required: true)
_v = m.send("#{v}_before_type_cast").to_s
return !required if _v.blank?
d = Date.parse(_v)
_d = Chronic.parse(_v)
logger.debug "***********************#{_d.inspect}"
block_given? ? yield(_d) : _d
rescue
nil
......
class Admission < ApplicationRecord
include Discard::Model
def self.params_whitelist
[:patient_id, :facility_id, :moment]
end
# *************************
# validations/callbacks
#
after_initialize :set_defaults
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
# FIXME: moment getting saved as date only
m.errors.add :moment, "missing/invalid" unless valid_date(m, :moment){|d| m.moment = d}
end
# ***************************
# instance methods
#
belongs_to :facility
belongs_to :patient
......@@ -7,6 +31,25 @@ class Admission < ApplicationRecord
has_many :symptoms
has_many :observations
def fmt_patient_name
patient&.name || EMT::UKNOWN
end
def fmt_facility_name
facility&.name || EMT::UKNOWN
end
#
def fmt_moment(default = created_at)
(moment || created_at).fmt(:mdy12)
end
def ajax_attributes
attributes
end
def set_defaults
self.moment = Time.now
end
end
\ No newline at end of file
end
\ No newline at end of file
......@@ -21,9 +21,13 @@ class ApplicationRecord < ActiveRecord::Base
APP_TABLES
end
# ****************************
# instance methods
#
# keeps us from having to say json.extract! in controllers for routine exports
# - override in kids
# - override in kids else defaults to safe nothing
def ajax_attributes(attrs = {})
{}
end
end
<div class='emt-admissions-form'>
<%= form_with model: admission, class: 'emt-form' do |f| %>
<%= emt_form_errors(admission) %>
<div>
<%= f.label :patient_id, "Patient" %>
<%= f.text_field :patient_id %>
</div>
<div>
<%= f.label :facility_id, "Referring Facility" %>
<%= f.text_field :facility_id %>
</div>
<div>
<%= f.label :moment, "Referred on Date/Time:" %>
<%= f.text_field :moment, value: admission.fmt_moment %>
</div>
<%= emt_form_buttons(admission) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-admissions-edit'>
<%= render 'form', admission: @admission %>
</div>
\ No newline at end of file
<div class='emt-admissions'>
<%= link_to "Add Admission", new_admission_path, class: 'emt-btn emt-brand' %>
<%= paginate @admissions %>
<table class='emt-table'>
<tr>
<th>Date/Time</th>
<th>Patient Name</th>
<th>Referring Facility</th>
</tr>
<% @admissions.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.fmt_moment, edit_admission_path(f) %></td>
<td><%= f.fmt_patient_name %></td>
<td><%= f.fmt_facility_name %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-admissions-new'>
<%= render 'form', admission: @admission %>
</div>
\ No newline at end of file
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= emt_form_buttons(facility) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities'>
<%= link_to "Add Facility", new_facility_path, class: 'emt-btn emt-brand' %>
<%= paginate @facilities %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= emt_form_buttons(facility) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities'>
<%= link_to "Add Facility", new_facility_path, class: 'emt-btn emt-brand' %>
<%= paginate @facilities %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= emt_form_buttons(facility) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities'>
<%= link_to "Add Facility", new_facility_path, class: 'emt-btn emt-brand' %>
<%= paginate @facilities %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<div class='emt-facilities-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<div class='emt-facilities-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= emt_form_buttons(facility) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities'>
<%= link_to "Add Facility", new_facility_path, class: 'emt-btn emt-brand' %>
<%= paginate @facilities %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= emt_form_buttons(facility) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities'>
<%= link_to "Add Facility", new_facility_path, class: 'emt-btn emt-brand' %>
<%= paginate @facilities %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= emt_form_buttons(facility) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities'>
<%= link_to "Add Facility", new_facility_path, class: 'emt-btn emt-brand' %>
<%= paginate @facilities %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<%= emt_form_buttons(facility) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
<div class='emt-facilities'>
<%= link_to "Add Facility", new_facility_path, class: 'emt-btn emt-brand' %>
<%= paginate @facilities %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
</div>
\ No newline at end of file
......@@ -8,6 +8,7 @@
module EMT
COMMON_TIME_FORMATS = {
mdy: '%-m/%-d/%Y',
mdy12: '%-m/%-d/%Y %l:%M %p',
dob: '%-m/%-d/%Y'
}
......@@ -25,3 +26,11 @@ Time::DATE_FORMATS.merge!(EMT::TIME_FORMATS)
Date::DATE_FORMATS.merge!(EMT::DATE_FORMATS)
class Time
alias fmt to_formatted_s
end
class Date
alias fmt to_formatted_s
end
\ No newline at end of file
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