Commit 4eb479d8 authored by Greg Gard's avatar Greg Gard

adding treatments

parent 4d3aa2ad
class PatientsController < ApplicationController
class TreatmentsController < ApplicationController
#
def index
@treatments = resource_parent.treatments.kept.order(created_at: 'desc').page params[:page]
end
#
def new
@treatment = resource_parent.treatments.new
end
#
def create
@treatment = resource_parent.treatments.new(resource_params)
@treatment.save!
set_ajax_data(
message: "Treatment created.",
treatment: @treatment.ajax_attributes,
redirect_to: patient_treatments_path(resource_parent)
)
end
def show
rescue *user_errors => er
set_ajax_user_error er, @treatment
rescue => er
set_ajax_stack_error er, @treatment
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 patient_treatments_path(resource_parent), notice: e
rescue => e
log_stack_error e
redirect_to patient_treatments_path(resource_parent), notice: stack_error_message
end
#
def update
@treatment.update! resource_params
set_ajax_data(
message: "Treatment updated.",
treatment: @treatment.ajax_attributes,
redirect_to: patient_treatments_path(resource_parent)
)
rescue *user_errors => er
set_ajax_user_error er, @treatment
rescue => er
set_ajax_stack_error er, @treatment
ensure
render_json
end
# TODO: undiscard
def destroy
@treatment.discard || raise("Unable to delete")
set_ajax_data(
message: "Treatment deleted.",
treatment: @treatment.ajax_attributes,
redirect_to: patient_treatments_path(resource_parent)
)
rescue *user_errors => er
set_ajax_user_error er, @treatment
rescue => er
set_ajax_stack_error er, @treatment
ensure
render_json
end
# **********************
private
def set_resource_parent
self.resource_parent = @patient = Patient.kept.find(params[:patient_id])
end
def set_resource
self.resource = @treatment = resource_parent.treatments.kept.find(params[:id])
end
end
\ No newline at end of file
class Treatment < ApplicationRecord
belongs_to :patient
include Discard::Model
def self.params_whitelist
[:patient_id, :description, :necessity]
end
# *************************
# validations/callbacks
#
after_initialize :set_defaults
before_validation do |m|
end
validate do |m|
m.errors.add :patient_id, "missing/invalid" unless m.patient_id.to_i > 0
m.errors.add :description, "missing/invalid" unless valid_description(m, :description)
m.errors.add :necessity, "missing/invalid" unless valid_name(m, :description, required: false)
end
# ***************************
# instance methods
#
belongs_to :patient
def fmt_patient_name
patient&.name || EMT::UKNOWN
end
#
def fmt_created_at
created_at.fmt(:mdy12)
end
def ajax_attributes
attributes
end
def set_defaults
end
end
\ No newline at end of file
<div class='emt-treatments-form'>
<%= form_with model: [resource_parent, treatment], class: 'emt-form' do |f| %>
<%= emt_form_errors(treatment) %>
<div>
<%= f.label :description, "Description" %>
<%= f.text_field :description %>
</div>
<div>
<%= f.label :description, "Evidence of Medical Necessity" %>
<%= f.text_field :necessity %>
</div>
<%= emt_form_buttons(treatment, resource_parent) %>
<% end %>
</div>
\ No newline at end of file
<div class='emt-treatments-edit'>
<%= emt_patient_header resource_parent %>
<%= render 'form', treatment: @treatment %>
</div>
\ No newline at end of file
<div class='emt-treatments'>
<%= emt_patient_header resource_parent %>
<%= link_to "Add Treatment", new_patient_treatment_path(resource_parent), class: 'emt-btn emt-brand' %>
<%= paginate @treatments %>
<table class='emt-table'>
<tr>
<th>Treatment Description</th>
<th>Necessity</th>
<th>Date Entered</th>
</tr>
<% @treatments.each_with_index do |f, idx| %>
<tr>
<td><%= link_to f.description, edit_patient_treatment_path(resource_parent, f) %></td>
<td><%= f.necessity %></td>
<td><%= f.fmt_created_at %></td>
</tr>
<% end %>
</table>
</div>
\ No newline at end of file
<div class='emt-treatments-new'>
<%= emt_patient_header resource_parent %>
<%= render 'form', treatment: @treatment %>
</div>
\ No newline at end of file
......@@ -4,6 +4,8 @@ class CreateTreatment < ActiveRecord::Migration[5.2]
t.integer :patient_id, null: false
t.text :description, null: false
t.text :necessity
t.timestamps null: false
end
end
end
......@@ -136,6 +136,8 @@ ActiveRecord::Schema.define(version: 2018_05_31_081136) do
t.text "description", null: false
t.text "necessity"
t.datetime "discarded_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["discarded_at"], name: "index_treatments_on_discarded_at"
end
......
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