Commit 78ec082b authored by Greg Gard's avatar Greg Gard

adding observations

parent 4eb479d8
class ObservationsController < ApplicationController
#
def index
@observations = resource_parent.observations.kept.order(created_at: 'desc').page params[:page]
end
#
def new
@observation = resource_parent.observations.new
end
#
def create
@observation = resource_parent.observations.new(resource_params)
@observation.save!
set_ajax_data(
message: "Observation created.",
observation: @observation.ajax_attributes,
redirect_to: admission_observations_path(resource_parent)
)
end
def show
rescue *user_errors => er
set_ajax_user_error er, @observation
rescue => er
set_ajax_stack_error er, @observation
ensure
render_json
end
#
def show
redirect_to resource_parent, notice: "That view isn't implemented."
end
#
def edit
rescue *user_errors => e
redirect_to admission_observations_path(resource_parent), notice: e
rescue => e
log_stack_error e
redirect_to admission_observations_path(resource_parent), notice: stack_error_message
end
#
def update
@observation.update! resource_params
set_ajax_data(
message: "Observation updated.",
observation: @observation.ajax_attributes,
redirect_to: admission_observations_path(resource_parent)
)
rescue *user_errors => er
set_ajax_user_error er, @observation
rescue => er
set_ajax_stack_error er, @observation
ensure
render_json
end
# TODO: undiscard
def destroy
@observation.discard || raise("Unable to delete")
set_ajax_data(
message: "Observation deleted.",
observation: @observation.ajax_attributes,
redirect_to: admission_observations_path(resource_parent)
)
rescue *user_errors => er
set_ajax_user_error er, @observation
rescue => er
set_ajax_stack_error er, @observation
ensure
render_json
end
# **********************
private
def set_resource_parent
self.resource_parent = @admission = Admission.kept.find(params[:admission_id])
end
def set_resource
self.resource = @observation = resource_parent.observations.kept.find(params[:id])
end
end
\ No newline at end of file
......@@ -149,7 +149,8 @@ module ApplicationHelper
[
{label: "Patients", url: admission_patients_path(resource.id)},
{label: "Transfers", url: admission_transfers_path(resource.id)},
{label: "Diagnoses", url: admission_diagnoses_path(resource.id)}
{label: "Diagnoses", url: admission_diagnoses_path(resource.id)},
{label: "Observations", url: admission_observations_path(resource.id)}
]
end
......
class Observation < ApplicationRecord
include Discard::Model
def self.params_whitelist
[:admission_id, :description, :moment]
end
# *************************
# validations/callbacks
#
after_initialize :set_defaults
before_validation do |m|
m.moment = (Chronic.parse(m.moment_before_type_cast.to_s) rescue nil)
end
validate do |m|
m.errors.add :admission_id, "missing/invalid" unless m.admission_id.to_i > 0
m.errors.add :description, "missing/invalid" unless valid_description(m, :description)
m.errors.add :moment, "invalid/missing" unless valid_date(m, :moment)
end
# *******************************
# ***************************
# instance methods
#
belongs_to :admission
#
def fmt_moment
moment.fmt(:mdy12)
end
#
def fmt_created_at
created_at.fmt(:mdy12)
end
def ajax_attributes
attributes
end
def set_defaults
end
def set_defaults
self.moment ||= Time.now
......
......@@ -19,8 +19,6 @@ class Patient < ApplicationRecord
m.dob = (Chronic.parse(m.dob_before_type_cast.to_s) rescue nil)
end
validate do |m|
m.errors.add :last_name, "missing/invalid" unless valid_person_name_part(m, :last_name)
m.errors.add :first_name, "missing/invalid" unless valid_person_name_part(m, :first_name)
......
<div class='emt-facilities-form'>
<%= form_with model: facility, class: 'emt-form' do |f| %>
<%= emt_form_errors(facility) %>
<div class='emt-observations-form'>
<%= form_with model: [resource_parent, observation], class: 'emt-form' do |f| %>
<%= emt_form_errors(observation) %>
<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :description %>
<%= f.text_field :description %>
</div>
<div>
<%= f.label :moment, "Date/Time Observed:" %>
<%= f.text_field :moment, value: observation.fmt_moment %>
</div>
<%= emt_form_buttons(facility) %>
<%= emt_form_buttons(observation, resource_parent) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-patients-edit'>
<%= render 'form', facility: @facility %>
<div class='emt-observations-edit'>
<%= emt_admission_header resource_parent %>
<%= render 'form', observation: @observation %>
</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 %>
<div class='emt-observations'>
<%= emt_admission_header resource_parent %>
<%= link_to "Add Observation", new_admission_observation_path(resource_parent), class: 'emt-btn emt-brand' %>
<%= paginate @observations %>
<table class='emt-table'>
<tr>
<th>Name</th>
<th>Description</th>
<th>Date Observed</th>
</tr>
<% @facilities.each_with_index do |f, idx| %>
<% @observations.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.name, edit_facility_path(f) %></td>
<td><%= f.description %></td>
<td><%= link_to f.description, edit_admission_observation_path(resource_parent, f) %></td>
<td><%= f.fmt_moment %></td>
</tr>
<% end %>
</table>
......
<div class='emt-patients-new'>
<%= render 'form', facility: @facility %>
<div class='emt-observations-new'>
<%= emt_admission_header resource_parent %>
<%= render 'form', observation: @observation %>
</div>
\ 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