Permalink
Browse files

Fancy new PatientSearch object

  • Loading branch information...
jordanbyron committed Jan 27, 2013
1 parent 4e7166a commit 0bd1f1674eb30be53f6432f52575270e0ebde3a4
@@ -4,7 +4,7 @@ class PatientsController < ApplicationController
before_filter :find_patient, :only => [:edit, :update, :destroy, :history]
def index
- @patients = Patient.search(params)
+ @patient_search = PatientSearch.new(:chart_number, :name).search(params)
end
def edit
@@ -2,7 +2,7 @@ class AssignmentDeskController < ApplicationController
before_filter :authenticate_user!
def index
- @patients = Patient.search(params)
+ @patient_search = PatientSearch.new.search(params)
end
def edit
@@ -3,7 +3,7 @@ class TreatmentAreas::PatientsController < ApplicationController
before_filter :find_treatment_area
def index
- @patients = Patient.search(params)
+ @patient_search = PatientSearch.new.search(params)
end
def radiology
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
@@ -2,7 +2,7 @@ class PatientSearch
include Enumerable
def initialize(*fields)
- @fields = fields || [:chart_number]
+ @fields = fields.empty? ? [:chart_number] : fields
@search_params = {}
end
@@ -18,8 +18,8 @@ def each
patients.each {|patient| yield patient, controls_for(patient) }
end
- def controls_for(patient)
- "Hi #{patient.full_name}!"
+ def controls(&block)
+ @patient_controls = block
end
def show?(field)
@@ -36,9 +36,17 @@ def total_pages
patients.total_pages
end
+ def current_page
+ (search_params[:page] || 1).to_i
+ end
+
private
- attr_reader :patients, :fields, :search_params
+ attr_reader :patients, :fields, :search_params, :patient_controls
+
+ def controls_for(patient)
+ h.capture(patient, &patient_controls) if patient_controls
+ end
def find_patients
if search_params[:commit] == "Clear"
@@ -55,10 +63,21 @@ def find_patients
Patient.where(id: chart_number)
else
@search_params = {}
- Patient.where(id: -1)
+ Patient.none
end
@patients = patients.order('id').
paginate(per_page: 30, page: search_params[:page])
end
+
+ # TODO Desc ...
+ #
+ def h
+ @h ||= begin
+ h = ActionController::Base.helpers.extend(Haml::Helpers)
+ h.init_haml_helpers
+ h
+ end
+ end
+
end
@@ -7,40 +7,11 @@
admin_export_patients_path(:filename => "patients"),
:target => "_blank", :class => 'btn btn-small'
-= render "patients/search", name: true
+- @patient_search.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", patient_search: @patient_search
@@ -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"
+- @patient_search.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", patient_search: @patient_search
@@ -1,4 +1,15 @@
-= render "search", patient_search: patient_search
+.bootstrap-form
+ %form{method: 'get', autocomplete: 'off'}
+ .input-append.input-prepend
+ %span.add-on
+ %i.icon-user
+ = text_field_tag :chart_number, patient_search[:chart_number],
+ placeholder: "Chart number", class: "span2", autofocus: 'autofocus'
+ - if patient_search.show? :name
+ = text_field_tag :name, patient_search[:name], placeholder: "Name",
+ class: "span2"
+ = submit_tag "Search", class: 'btn'
+ = submit_tag "Clear", class: 'btn'
- if patient_search.any?
%table
@@ -20,11 +31,8 @@
%td= patient.age
%td= patient.sex
%td.controls= controls
- / = link_to "Check-in",
- / new_patient_path(patient: {previous_chart_number: patient}),
- / class: 'primary'
- = will_paginate @patient_search
+ = will_paginate patient_search
- else
%p
@@ -1,12 +0,0 @@
-.bootstrap-form
- %form{method: 'get', autocomplete: 'off'}
- .input-append.input-prepend
- %span.add-on
- %i.icon-user
- = text_field_tag :chart_number, patient_search[:chart_number],
- placeholder: "Chart number", class: "span2"
- - if patient_search.show? :name
- = text_field_tag :name, patient_search[:name], placeholder: "Name",
- class: "span2"
- = submit_tag "Search", class: 'btn'
- = submit_tag "Clear", class: 'btn'
@@ -1,5 +1,10 @@
- title 'Check-in Previous Patient'
+- @patient_search.controls do |patient|
+ = link_to "Check-in",
+ new_patient_path(patient: {previous_chart_number: patient}),
+ class: 'primary'
+
= render "nav"
= header do
@@ -1,36 +1,9 @@
-- content_for :header_js do
- :plain
- $(function(){
- $('#chart_number').select().focus();
- });
-
- title "#{@treatment_area.name} Checkout"
= header do
%h1= "#{@treatment_area.name} Checkout"
-= render "patients/search"
-
-- 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{:class=> cycle('even', 'odd')}
- %td= patient.id
- %td= patient.last_name
- %td= patient.first_name
- %td= patient.dob
- %td= patient.age
- %td= patient.sex
- %td.controls
- = link_to_checkout(@treatment_area, patient)
+- @patient_search.controls do |patient|
+ = link_to_checkout(@treatment_area, patient)
- = will_paginate @patients
+= render "patients/patients", patient_search: @patient_search

0 comments on commit 0bd1f16

Please sign in to comment.