Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Protocols, and Protocol Drugs #25

Merged
merged 11 commits into from
Jun 5, 2018
84 changes: 84 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}
54 changes: 54 additions & 0 deletions app/controllers/admin/protocol_drugs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class Admin::ProtocolDrugsController < ApplicationController
before_action :set_protocol_drug, only: %i[show edit update destroy]
before_action :set_protocols, only: %i[create edit update new]

def index
@protocol_drugs = ProtocolDrug.all
end

def show
end

def new
@protocol_drug = ProtocolDrug.new
end

def edit
end

def create
@protocol_drug = ProtocolDrug.new(protocol_drug_params)
if @protocol_drug.save
redirect_to [:admin, :protocols], notice: 'Protocol drug was successfully created.'
else
render :new
end
end

def update
if @protocol_drug.update(protocol_drug_params)
redirect_to [:admin, :protocols], notice: 'Protocol drug was successfully updated.'
else
render :edit
end
end

def destroy
@protocol_drug.destroy
redirect_to [:admin, :protocols], notice: 'Protocol drug was successfully destroyed.'
end

private

def set_protocols
@protocols = Protocol.all
end

def set_protocol_drug
@protocol_drug = ProtocolDrug.find(params[:id])
end

def protocol_drug_params
params.require(:protocol_drug).permit(:name, :dosage, :rxnorm_code, :protocol_id)
end
end
49 changes: 49 additions & 0 deletions app/controllers/admin/protocols_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class Admin::ProtocolsController < ApplicationController
before_action :set_protocol, only: %i[show edit update destroy]

def index
@protocols = Protocol.all
end

def show
end

def new
@protocol = Protocol.new
end

def edit
end

def create
@protocol = Protocol.new(protocol_params)
if @protocol.save
redirect_to [:admin, @protocol], notice: 'Protocol was successfully created.'
else
render :new
end
end

def update
if @protocol.update(protocol_params)
redirect_to [:admin, @protocol], notice: 'Protocol was successfully updated.'
else
render :edit
end
end

def destroy
@protocol.destroy
redirect_to admin_protocols_url, notice: 'Protocol was successfully destroyed.'
end

private

def set_protocol
@protocol = Protocol.find(params[:id])
end

def protocol_params
params.require(:protocol).permit(:name, :follow_up_days)
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/patients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def merge_if_valid(single_patient_params)
end

def find_records_to_sync(since, limit)
Patient.updated_on_server_since(processed_since, limit)
Patient.updated_on_server_since(since, limit)
end

def transform_to_response(patient)
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/api/v1/protocols_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Api::V1::ProtocolsController < Api::V1::SyncController
def sync_to_user
__sync_to_user__('protocols')
end

private

def find_records_to_sync(since, limit)
Protocol.updated_on_server_since(since, limit).includes(:protocol_drugs)
end

def transform_to_response(protocol)
protocol.as_json(include: :protocol_drugs)
end
end
3 changes: 3 additions & 0 deletions app/models/address.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class Address < ApplicationRecord
include Mergeable

validates :device_created_at, presence: true
validates :device_updated_at, presence: true
end
1 change: 0 additions & 1 deletion app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
validates_presence_of :device_created_at, :device_updated_at

def self.updated_on_server_since(timestamp, number_of_records = nil)
where('updated_at >= ?', timestamp)
Expand Down
3 changes: 3 additions & 0 deletions app/models/blood_pressure.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class BloodPressure < ApplicationRecord
include Mergeable
belongs_to :patient, optional: true

validates :device_created_at, presence: true
validates :device_updated_at, presence: true
end
2 changes: 2 additions & 0 deletions app/models/patient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Patient < ApplicationRecord
has_many :phone_numbers, class_name: 'PatientPhoneNumber'
has_many :blood_pressures

validates :device_created_at, presence: true
validates :device_updated_at, presence: true
validates_associated :address, if: :address
validates_associated :phone_numbers, if: :phone_numbers
end
3 changes: 3 additions & 0 deletions app/models/patient_phone_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ class PatientPhoneNumber < ApplicationRecord

PHONE_TYPE = %w[mobile landline].freeze
belongs_to :patient

validates :device_created_at, presence: true
validates :device_updated_at, presence: true
end
11 changes: 11 additions & 0 deletions app/models/protocol.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Protocol < ApplicationRecord
has_many :protocol_drugs
before_create :assign_id

validates :name, presence: true
validates :follow_up_days, numericality: true, presence: true

def assign_id
self.id = SecureRandom.uuid
end
end
11 changes: 11 additions & 0 deletions app/models/protocol_drug.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ProtocolDrug < ApplicationRecord
belongs_to :protocol
before_create :assign_id

validates :name, presence: true
validates :dosage, presence: true

def assign_id
self.id = SecureRandom.uuid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should discuss this. I think we should switch all of these records to create UUIDs as ID by default, or move UUID to a separate field and use bigint IDs. As it is, this is messy; I ran into the same issue with Facility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating UUIDs by default in the database sounds like the way to go. We'll do this for all entities that are created via the admin interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll do this after the facilities PR is merged in, so that we don't conflict with your changes of adding pgcrypto or another extension.

end
end
43 changes: 41 additions & 2 deletions app/payloads/api/v1/spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ def self.blood_pressures
items: { '$ref' => '#/definitions/blood_pressure' } }
end

def self.protocol_drug
{ type: :object,
properties: {
id: { '$ref' => '#/definitions/uuid' },
created_at: { '$ref' => '#/definitions/timestamp' },
updated_at: { '$ref' => '#/definitions/timestamp' },
protocol_id: { '$ref' => '#/definitions/uuid' },
rxnorm_code: { type: :string },
dosage: { type: :string },
name: { type: :string } },
required: %w[id dosage name protocol_id] }
end

def self.protocol
{ type: :object,
properties: {
id: { '$ref' => '#/definitions/uuid' },
created_at: { '$ref' => '#/definitions/timestamp' },
updated_at: { '$ref' => '#/definitions/timestamp' },
name: { type: :string },
follow_up_days: { type: :integer },
protocol_drugs: { type: :array,
items: { '$ref' => '#/definitions/protocol_drug' } } },
required: %w[id name protocol_drugs] }
end

###############
# API Specs

Expand Down Expand Up @@ -161,14 +187,25 @@ def self.patient_sync_to_user_response_spec
{ type: :object,
properties: {
patients: { '$ref' => '#/definitions/nested_patients' },
processed_since: { '$ref' => '#/definitions/processed_since' } } }
processed_since: { '$ref' => '#/definitions/processed_since' } },
required: %w[patients processed_since] }
end

def self.blood_pressure_sync_to_user_response_spec
{ type: :object,
properties: {
blood_pressures: { '$ref' => '#/definitions/blood_pressures' },
processed_since: { '$ref' => '#/definitions/processed_since' } } }
processed_since: { '$ref' => '#/definitions/processed_since' } },
required: %w[blood_pressures processed_since]}
end

def self.protocol_sync_to_user_response_spec
{ type: :object,
properties: {
protocols: { type: :array,
items: { '$ref' => '#/definitions/protocol' } },
processed_since: { '$ref' => '#/definitions/processed_since' } },
required: %w[protocols processed_since] }
end

def self.all_definitions
Expand All @@ -184,6 +221,8 @@ def self.all_definitions
nested_patients: nested_patients,
blood_pressure: blood_pressure_spec,
blood_pressures: blood_pressures,
protocol: protocol,
protocol_drug: protocol_drug,
non_empty_string: non_empty_string,
error_spec: error_spec }
end
Expand Down
37 changes: 37 additions & 0 deletions app/views/admin/protocol_drugs/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<%= form_with(model: [:admin, protocol_drug], local: true) do |form| %>
<% if protocol_drug.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(protocol_drug.errors.count, "error") %> prohibited this protocol_drug from being saved:</h2>

<ul>
<% protocol_drug.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :protocol_drug_name %>
</div>

<div class="field">
<%= form.label :dosage %>
<%= form.text_field :dosage, id: :protocol_drug_dosage %>
</div>

<div class="field">
<%= form.label :rxnorm_code %>
<%= form.text_field :rxnorm_code, id: :protocol_drug_rxnorm_code %>
</div>

<div class="field">
<%= form.label :protocol_id %>
<%= form.select 'protocol_id', options_from_collection_for_select(@protocols, "id", "name") %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/admin/protocol_drugs/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Protocol Drug</h1>

<%= render 'form', protocol_drug: @protocol_drug %>

<%= link_to 'Show', [:admin, @protocol_drug] %> |
<%= link_to 'Back', admin_protocol_drugs_path %>
Loading