Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor PatientSearch using Virtus, add treatment_area_id parameter

  • Loading branch information...
commit 70b88a718862ec2794c6ff547ae8cbf2ac176114 1 parent 156da26
@brentvatne brentvatne authored
View
1  Gemfile
@@ -11,6 +11,7 @@ gem 'devise'
gem 'dynamic_form'
gem 'draper'
gem 'rack-pjax'
+gem 'virtus'
gem 'jquery-rails'
gem 'will_paginate-bootstrap'
View
6 Gemfile.lock
@@ -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
View
53 app/models/patient_search.rb
@@ -1,39 +1,42 @@
class PatientSearch
+ include Virtus
- def initialize(params={})
- @params = params
- end
+ attribute :chart_number, Integer
+ attribute :treatment_area_id, Integer
+ attribute :name, String
+ attribute :commit, String
def execute
- if params[:commit] == "Clear"
- @params = {}
+ if blank_search? || commit == 'Clear'
+ return reset_attributes && Patient.none
+ elsif chart_number.present?
+ return Patient.where(id: chart_number)
+ end
+
+ patients = Patient.scoped
+
+ if name.present?
+ patients = patients.where(
+ 'first_name ILIKE :name or last_name ILIKE :name', name: "%#{name}%"
+ )
end
- chart_number = params[:chart_number]
- name = params[:name]
-
- 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}%")
- else
- @params = {}
- Patient.none
+ if treatment_area_id.present?
+ patients = patients.joins(:treatment_areas).where(
+ 'treatment_areas.id = ?', treatment_area_id
+ )
end
patients.order('id')
end
- def []=(key, value)
- @params[key] = value
+ def reset_attributes
+ attributes.each do |key, value|
+ self[key] = nil
+ end
end
- def [](key)
- params[key]
+ def blank_search?
+ attributes.values.all?(&:blank?)
end
-
- private
- attr_reader :params
-
-end
+end
View
52 test/unit/patient_search_test.rb
@@ -14,36 +14,66 @@ class PatientSearchTest < ActiveSupport::TestCase
@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'
+ test 'search 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'
+ test 'search 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 'search by assigned treatment area' do
+ @treatment_area = FactoryGirl.create(:treatment_area)
+ @other_treatment_area = FactoryGirl.create(:treatment_area)
+ @jordan_byron.treatment_areas << @treatment_area
+ @michael_jordan.treatment_areas << @other_treatment_area
+
+ @patient_search.treatment_area_id = @treatment_area.id
+ assert_equal [@jordan_byron], @patient_search.execute
+
+ @patient_search.treatment_area_id = @other_treatment_area.id
+ assert_equal [@michael_jordan], @patient_search.execute
+ end
+
+ test 'search by age' do
+ end
+
+ test 'search by procedure' do
+
+ end
+
+ test 'combine search options (except chart number)' do
+ # Should be able to combine any search options other than chart number
+ 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"
+ @patient_search.chart_number = @jordan_byron.id
+ @patient_search.commit = 'Clear'
+
+ puts @patient_search.commit
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.chart_number = @jordan_byron.id
+ @patient_search.commit = "Clear"
@patient_search.execute
- refute @patient_search[:chart_number]
+ refute @patient_search.chart_number
+ end
+
+ test '#blank_search? returns true when there are no search params' do
+ assert @patient_search.blank_search?
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.