Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create PatientSearch and rename PatientsTable

  • Loading branch information...
commit c6a99a96eb18a863a955dfecc8a9deda57fd6f19 1 parent 2c482ea
@jordanbyron jordanbyron authored
View
2  app/controllers/admin/patients_controller.rb
@@ -4,7 +4,7 @@ class PatientsController < ApplicationController
before_filter :find_patient, :only => [:edit, :update, :destroy, :history]
def index
- @patient_search = PatientSearch.new(:chart_number, :name).search(params)
+ @patient_search = PatientsTable.new(:chart_number, :name).search(params)
end
def edit
View
2  app/controllers/assignment_desk_controller.rb
@@ -2,7 +2,7 @@ class AssignmentDeskController < ApplicationController
before_filter :authenticate_user!
def index
- @patient_search = PatientSearch.new.search(params)
+ @patient_search = PatientsTable.new.search(params)
end
def edit
View
2  app/controllers/patients_controller.rb
@@ -38,7 +38,7 @@ def reprint
def previous
@current_tab = "previous"
- @patient_search = PatientSearch.new(:chart_number, :name).search(params)
+ @patient_search = PatientsTable.new(:chart_number, :name).search(params)
end
private
View
2  app/controllers/treatment_areas/patients_controller.rb
@@ -3,7 +3,7 @@ class TreatmentAreas::PatientsController < ApplicationController
before_filter :find_treatment_area
def index
- @patient_search = PatientSearch.new.search(params)
+ @patient_search = PatientsTable.new.search(params)
end
def radiology
View
86 app/models/patient_search.rb
@@ -1,83 +1,39 @@
class PatientSearch
- include Enumerable
- def initialize(*fields)
- @fields = fields.empty? ? [:chart_number] : fields
- @search_params = {}
+ def initialize(params={})
+ @params = 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(&block)
- @patient_controls = block
- end
-
- def show?(field)
- fields.include? field
- end
-
- def [](param)
- search_params[param]
- end
-
- # Will Paginate methods
-
- def total_pages
- patients.total_pages
- end
-
- def current_page
- (search_params[:page] || 1).to_i
- end
-
- private
-
- 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"
- @search_params = {}
+ def execute
+ if params[:commit] == "Clear"
+ @params = {}
end
- chart_number = search_params[:chart_number]
- name = search_params[:name]
+ chart_number = params[:chart_number]
+ name = params[:name]
- patients = if chart_number.blank? && !name.blank?
+ patients = if chart_number.present?
+ Patient.where(id: chart_number)
+ elsif name.present?
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 = {}
+ @params = {}
Patient.none
end
- @patients = patients.order('id').
- paginate(per_page: 30, page: search_params[:page])
+ patients.order('id')
end
- # TODO Desc ...
- #
- def h
- @h ||= begin
- h = ActionController::Base.helpers.extend(Haml::Helpers)
- h.init_haml_helpers
- h
- end
+ def []=(key, value)
+ @params[key] = value
end
+ def [](key)
+ params[key]
+ end
+
+ private
+ attr_reader :params
+
end
View
61 app/models/patients_table.rb
@@ -0,0 +1,61 @@
+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.execute.
+ paginate(per_page: 30, page: params[:page])
+
+ self
+ 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
+
+ def [](param)
+ patient_search[param]
+ end
+
+ # Will Paginate methods
+
+ def total_pages
+ patients.total_pages
+ end
+
+ def current_page
+ (patient_search[:page] || 1).to_i
+ end
+
+ private
+
+ attr_reader :patients, :fields, :patient_search, :patient_controls
+
+ def controls_for(patient)
+ h.capture(patient, &patient_controls) if patient_controls
+ end
+
+ # TODO Desc ...
+ #
+ def h
+ @h ||= begin
+ h = ActionController::Base.helpers.extend(Haml::Helpers)
+ h.init_haml_helpers
+ h
+ end
+ end
+
+end
View
50 test/unit/patient_search_test.rb
@@ -0,0 +1,50 @@
+require "test_helper"
+
+class PatientSearchTest < ActiveSupport::TestCase
+
+ setup do
+ @jordan_byron = FactoryGirl.create(:patient,
+ first_name: 'Jordan', last_name: 'Byron'
+ )
+
+ @michael_jordan = FactoryGirl.create(:patient,
+ first_name: 'Michael', last_name: 'Jordan'
+ )
+
+ @patient_search = PatientSearch.new
+ end
+
+ test 'searches by chart number if it is given (even if name is given)' do
+ @patient_search[:chart_number] = @jordan_byron.id
+ @patient_search[:name] = 'Not the name'
+ assert_equal [@jordan_byron], @patient_search.execute
+ end
+
+ test 'searches by name if given and no chart number' do
+ @patient_search[:chart_number] = nil
+ @patient_search[:name] = 'Jordan'
+ assert_equal [@jordan_byron, @michael_jordan], @patient_search.execute
+ end
+
+ test 'if no search parameters, returns an empty active record result set' do
+ assert_equal [], @patient_search.execute
+ assert ActiveRecord::Relation === @patient_search.execute
+ end
+
+ test 'if the commit param is "Clear" then it returns an empty result set' do
+ @patient_search[:chart_number] = @jordan_byron.id
+ @patient_search[:commit] = "Clear"
+
+ assert_equal [], @patient_search.execute
+ end
+
+ test 'if the commit param is "Clear" then clear out search params' do
+ @patient_search[:chart_number] = @jordan_byron.id
+ @patient_search[:commit] = "Clear"
+
+ @patient_search.execute
+
+ refute @patient_search[:chart_number]
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.