Permalink
Browse files

Search by procedure!

  • Loading branch information...
1 parent bb7f993 commit 9ea145945a1daef60a90ec3521434222323ba429 @brentvatne brentvatne committed Jan 27, 2013
Showing with 26 additions and 4 deletions.
  1. +13 −2 app/models/patient_search.rb
  2. +13 −2 test/unit/patient_search_test.rb
@@ -3,19 +3,22 @@ class PatientSearch
attribute :chart_number, Integer
attribute :treatment_area_id, Integer
+ attribute :procedure_id, Integer
attribute :age, Integer
attribute :name, String
attribute :commit, String
def execute
+ # TODO We can move the commit check here into the PatientsTable
if blank_search? || commit == 'Clear'
return reset_attributes && Patient.none
elsif chart_number.present?
return Patient.where(id: chart_number)
end
- patients = Patient.scoped
+ # 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}%"
@@ -29,7 +32,15 @@ def execute
end
if age.present?
- patients = patients.where("date_part('year', age(date_of_birth)) = ?", age)
+ 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')
@@ -40,8 +40,8 @@ class PatientSearchTest < ActiveSupport::TestCase
end
test 'search by age' do
- # We probably want to have options like greater than, less than, equal to,
- # but leaving this out for now.
+ # @jordan: We probably want to have options like greater than, less than,
+ # equal to, but leaving this out for now.
@jordan_byron.update_attributes(date_of_birth: Date.today - 65.years)
@michael_jordan.update_attributes(date_of_birth: Date.today - 23.years)
@@ -53,7 +53,18 @@ class PatientSearchTest < ActiveSupport::TestCase
end
test 'search by procedure' do
+ procedure = FactoryGirl.create(:procedure)
+ other_procedure = FactoryGirl.create(:procedure)
+ @jordan_byron.procedures << procedure
+ @jordan_byron.procedures << other_procedure
+ @michael_jordan.procedures << other_procedure
+
+ @patient_search.procedure_id = procedure.id
+ assert_equal [@jordan_byron], @patient_search.execute
+
+ @patient_search.procedure_id = other_procedure.id
+ assert_equal [@jordan_byron, @michael_jordan], @patient_search.execute
end
test 'combine search options (except chart number)' do

0 comments on commit 9ea1459

Please sign in to comment.