Permalink
Browse files

First draft of patient search

  • Loading branch information...
1 parent d50ff92 commit 4e7166ac5211145beaa90b8f48d143032a0a8945 @jordanbyron jordanbyron committed Jan 25, 2013
@@ -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
@@ -38,7 +38,7 @@ def reprint
def previous
@current_tab = "previous"
- @patients = Patient.search(params)
+ @patient_search = PatientSearch.new(:chart_number, :name).search(params)
end
private
@@ -0,0 +1,64 @@
+class PatientSearch
+ include Enumerable
+
+ def initialize(*fields)
+ @fields = fields || [:chart_number]
+ @search_params = {}
+ end
+
+ def search(params)
+ @search_params = params
+
+ find_patients
+
+ self
+ end
+
+ def each
+ patients.each {|patient| yield patient, controls_for(patient) }
+ end
+
+ def controls_for(patient)
+ "Hi #{patient.full_name}!"
+ end
+
+ def show?(field)
+ fields.include? field
+ end
+
+ def [](param)
+ search_params[param]
+ end
+
+ # Will Paginate methods
+
+ def total_pages
+ patients.total_pages
+ end
+
+ private
+
+ attr_reader :patients, :fields, :search_params
+
+ def find_patients
+ if search_params[:commit] == "Clear"
+ @search_params = {}
+ end
+
+ chart_number = search_params[:chart_number]
+ name = search_params[:name]
+
+ patients = if chart_number.blank? && !name.blank?
+ Patient.where('first_name ILIKE :name or last_name ILIKE :name',
+ name: "%#{name}%")
+ elsif !chart_number.blank? && chart_number.to_i != 0
+ Patient.where(id: chart_number)
+ else
+ @search_params = {}
+ Patient.where(id: -1)
+ end
+
+ @patients = patients.order('id').
+ paginate(per_page: 30, page: search_params[:page])
+ end
+end
@@ -0,0 +1,32 @@
+= render "search", patient_search: patient_search
+
+- if patient_search.any?
+ %table
+ %thead
+ %tr
+ %th Chart #
+ %th Last name
+ %th First name
+ %th Date of birth
+ %th Age
+ %th Sex
+ %tbody
+ - patient_search.each do |patient, controls|
+ %tr
+ %td= patient.id
+ %td= patient.last_name
+ %td= patient.first_name
+ %td= patient.dob
+ %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
+
+- else
+ %p
+ Enter the patient's chart number in the box above, then click
+ %strong Search
@@ -3,10 +3,10 @@
.input-append.input-prepend
%span.add-on
%i.icon-user
- = text_field_tag :chart_number, params[:chart_number],
+ = text_field_tag :chart_number, patient_search[:chart_number],
placeholder: "Chart number", class: "span2"
- - if defined?(name) && name == true
- = text_field_tag :name, params[:name], placeholder: "Name",
+ - 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'
@@ -5,37 +5,5 @@
= header do
%h1 Check-in Previous Patient
-= render "search", name: true
+= render "patients", patient_search: @patient_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
- %td= patient.id
- %td= patient.last_name
- %td= patient.first_name
- %td= patient.dob
- %td= patient.age
- %td= patient.sex
- %td.controls
- = link_to "Check-in", new_patient_path(patient: {previous_chart_number: patient.id}),
- class: 'primary'
-
- = will_paginate @patients
-
-- elsif params[:chart_number].blank?
- %p
- Enter the patient's chart number in the box above, then click
- %strong Search
-- else
- %p
- %strong No results found

0 comments on commit 4e7166a

Please sign in to comment.