Patient search #44

Closed
wants to merge 12 commits into
from
Jump to file
+418 −268
Split
View
@@ -11,6 +11,7 @@ gem 'devise'
gem 'dynamic_form'
gem 'draper'
gem 'rack-pjax'
+gem 'virtus'
gem 'jquery-rails'
gem 'will_paginate-bootstrap'
View
@@ -31,6 +31,7 @@ GEM
addressable (2.3.2)
ansi (1.4.3)
arel (3.0.2)
+ backports (2.6.7)
bcrypt-ruby (3.0.1)
better_errors (0.3.2)
coderay (>= 1.0.0)
@@ -70,6 +71,7 @@ GEM
compass-rails (1.0.3)
compass (>= 0.12.2, < 0.14)
database_cleaner (0.9.1)
+ descendants_tracker (0.0.1)
devise (2.2.2)
bcrypt-ruby (~> 3.0)
orm_adapter (~> 0.1)
@@ -181,6 +183,9 @@ GEM
uglifier (1.3.0)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
+ virtus (0.5.4)
+ backports (~> 2.6.1)
+ descendants_tracker (~> 0.0.1)
warden (1.2.1)
rack (>= 1.0)
websocket (1.0.6)
@@ -223,5 +228,6 @@ DEPENDENCIES
timecop
turn
uglifier
+ virtus
will_paginate (~> 3.0.2)
will_paginate-bootstrap
@@ -30,5 +30,7 @@ div.pagination ul
.bootstrap-form
@import twitter/bootstrap/forms
- input, .add-on
- vertical-align: bottom !important
+ input, .add-on, select
+ vertical-align: bottom !important
+ .input-append select
+ width: 175px
@@ -4,7 +4,10 @@ class PatientsController < ApplicationController
before_filter :find_patient, :only => [:edit, :update, :destroy, :history]
def index
- @patients = Patient.search(params)
+ @patients_table = PatientsTable.new(:chart_number, :name, :age,
+ :treatment_area_id, :procedure_id, :count
+ )
+ @patient_search = @patients_table.search(params)
end
def edit
@@ -5,7 +5,6 @@ class ApplicationController < ActionController::Base
before_filter :set_area_id
around_filter :setup_stats
- before_filter :clear_params
attr_accessor :current_area_id, :current_treatment_area_id
@@ -53,10 +52,4 @@ def dexis?
def cdr?
app_config['xray'] == "cdr"
end
-
- def clear_params
- if params[:commit] == "Clear"
- params.delete_if {|k, v| ['chart_number', 'name'].include?(k) }
- end
- end
end
@@ -2,7 +2,8 @@ class AssignmentDeskController < ApplicationController
before_filter :authenticate_user!
def index
- @patients = Patient.search(params)
+ @patients_table = PatientsTable.new
+ @patient_search = @patients_table.search(params)
end
def edit
@@ -38,7 +38,8 @@ def reprint
def previous
@current_tab = "previous"
- @patients = Patient.search(params)
+ @patients_table = PatientsTable.new(:chart_number, :name)
+ @patient_search = @patients_table.search(params)
end
private
@@ -3,7 +3,8 @@ class TreatmentAreas::PatientsController < ApplicationController
before_filter :find_treatment_area
def index
- @patients = Patient.search(params)
+ @patients_table = PatientsTable.new
+ @patient_search = @patients_table.search(params)
end
def radiology
@@ -27,6 +27,10 @@ def clinic_state
app_config["state"]
end
+ def patient_searches_path(*args)
+ request.url
+ end
+
def real_currency(number)
number_to_currency(number,:delimiter => ",", :unit => "$ ",:separator => ".")
end
@@ -0,0 +1,10 @@
+module CaptureHelper
+ include ActionView::Helpers::CaptureHelper
+ include Haml::Helpers
+
+ def self.helpers
+ @helpers ||= Object.new.extend(self).tap do |h|
+ h.init_haml_helpers
+ end
+ end
+end
View
@@ -62,19 +62,11 @@ class Patient < ActiveRecord::Base
attr_accessor :race_other
attr_reader :time_in_pain
- def self.search(params)
- chart_number = params[:chart_number]
- name = params[:name]
-
- conditions = if chart_number.blank? && !name.blank?
- ['first_name ILIKE ? or last_name ILIKE ?', "%#{name}%","%#{name}%"]
- elsif !chart_number.blank? && chart_number.to_i != 0
- {id: chart_number}
- else
- {id: -1} # Don't return results for empty queries
- end
-
- Patient.where(conditions).order('id').paginate(per_page: 30, page: params[:page])
+ # TODO Remove this after Rails 4 upgrade
+ # https://github.com/rails/rails/commit/75de1ce131cd39f68dbe6b68eecf2617a720a8e4
+ #
+ def self.none
+ Patient.where(id: -1)
end
def chart_number
@@ -0,0 +1,65 @@
+class PatientSearch
+ include Virtus
+ include ActiveModel::Conversion
+ extend ActiveModel::Naming
+
+ attribute :chart_number, Integer
+ attribute :treatment_area_id, Integer
+ attribute :procedure_id, Integer
+ attribute :age, Integer
+ attribute :name, String
+
+ def initialize(params)
+ super params[:patient_search]
+ reset_attributes if params[:commit] == 'Clear'
+ end
+
+ def patients
+ if blank_search?
+ return Patient.none
+ elsif chart_number.present?
+ return Patient.where(id: chart_number)
+ end
+
+ # TODO Turn these into scopes?
+
+ patients = Patient.scoped
+ if name.present?
+ patients = patients.where(
+ 'first_name ILIKE :name or last_name ILIKE :name', name: "%#{name}%"
+ )
+ end
+
+ if treatment_area_id.present?
+ patients = patients.joins(:treatment_areas).where(
+ 'treatment_areas.id = ?', treatment_area_id
+ )
+ end
+
+ if age.present?
+ patients = patients.where(
+ "date_part('year', age(date_of_birth)) = ?", age
+ )
+ end
+
+ if procedure_id.present?
+ patients = patients.joins(:procedures).where(
+ 'procedures.id = ?', procedure_id
+ )
+ end
+
+ patients.order('id')
+ end
+
+ def reset_attributes
+ attributes.each do |key, value|
+ self[key] = nil
+ end
+ end
+
+ def blank_search?
+ attributes.values.all?(&:blank?)
+ end
+
+ def persisted?; false; end
+end
@@ -0,0 +1,51 @@
+class PatientsTable
+ include Enumerable
+
+ def initialize(*fields)
+ @fields = fields.empty? ? [:chart_number] : fields
+ end
+
+ def search(params)
+ @patient_search = PatientSearch.new(params)
+
+ @patients = patient_search.patients.
+ paginate(per_page: 30, page: params[:page])
+
+ @patient_search
+ end
+
+ def each
+ patients.each {|patient| yield patient, controls_for(patient) }
+ end
+
+ def controls(&block)
+ @patient_controls = block
+ end
+
+ def show?(field)
+ fields.include?(field)
+ end
+
+ # Will Paginate methods
+
+ def total_pages
+ patients.total_pages
+ end
+
+ def current_page
+ (patients.current_page || 1).to_i
+ end
+
+ def count
+ patients.total_entries
+ end
+
+ private
+
+ attr_reader :patients, :fields, :patient_search, :patient_controls
+
+ def controls_for(patient)
+ CaptureHelper.helpers.capture(patient, &patient_controls) if patient_controls
+ end
+
+end
@@ -7,40 +7,11 @@
admin_export_patients_path(:filename => "patients"),
:target => "_blank", :class => 'btn btn-small'
-= render "patients/search", name: true
+- @patients_table.controls do |patient|
+ = link_to 'History', history_admin_patient_path(patient)
+ = link_to 'Edit', edit_admin_patient_path(patient)
+ = link_to 'Print', chart_patient_path(patient)
+ = link_to 'Destroy', admin_patient_path(patient),
+ data: {confirm: 'Are you sure?'}, method: :delete
-- if @patients.any?
- %table
- %thead
- %tr
- %th Chart #
- %th Last name
- %th First name
- %th Date of birth
- %th Age
- %th Sex
- %tbody
- - @patients.each do |patient|
- %tr
- %td= patient.id
- %td= patient.last_name
- %td= patient.first_name
- %td= patient.dob
- %td= patient.age
- %td= patient.sex
- %td.controls
- = link_to 'History', history_admin_patient(patient)
- = link_to 'Edit', edit_admin_patient_path(patient)
- = link_to 'Print', chart_patient_path(patient)
- = link_to 'Destroy', admin_patient_path(patient),
- data: {confirm: 'Are you sure?'}, method: :delete
-
- = will_paginate @patients
-
-- elsif params[:chart_number].blank? && params[:name].blank?
- %p
- Enter the patient's chart number or name in the box above, then click
- %strong Search
-- else
- %p
- %strong No results found
+= render "patients/patients"
@@ -1,36 +1,10 @@
- title 'Assignment Desk'
-- content_for :header do
- :javascript
- $(function(){
- $('#chart_number').select();
- $('#chart_number').focus();
- });
-
= header do
%h1 Assignment Desk
-= render "patients/search"
+- @patients_table.controls do |patient|
+ = link_to "Assign", edit_assignment_desk_path(patient),
+ class: 'primary'
-- if @patients.any?
- %table
- %thead
- %tr
- %th Chart #
- %th Last name
- %th First name
- %th Date of birth
- %th Age
- %th Sex
- %tbody
- - @patients.each do |patient|
- %tr
- %td= patient.id
- %td= patient.last_name
- %td= patient.first_name
- %td= patient.dob
- %td= patient.age
- %td= patient.sex
- %td.controls
- = link_to "Assign", edit_assignment_desk_path(patient),
- class: 'primary'
+= render "patients/patients"
Oops, something went wrong.