Permalink
Browse files

Add additional options to search form and extract capture helper

  • Loading branch information...
jordanbyron committed Feb 2, 2013
1 parent 724ead4 commit ee6f3641085e6a64a6c20b3b2ae0b20be3aede61
@@ -5,6 +5,7 @@
@import twitter/bootstrap/sprites
@import twitter/bootstrap/pagination
@import twitter/bootstrap/tooltip
+@import twitter/bootstrap/labels-badges
@import twitter/bootstrap/component-animations
a.btn
@@ -30,5 +31,7 @@ div.pagination ul
.bootstrap-form
@import twitter/bootstrap/forms
- input, .add-on
- vertical-align: bottom !important
+ input, .add-on, select
+ vertical-align: bottom !important
+ .input-append select
+ width: 175px
@@ -4,7 +4,10 @@ class PatientsController < ApplicationController
before_filter :find_patient, :only => [:edit, :update, :destroy, :history]
def index
- @patient_search = PatientsTable.new(:chart_number, :name).search(params)
+ @patients_table = PatientsTable.new(:chart_number, :name, :age,
+ :treatment_area_id, :procedure_id, :count
+ )
+ @patient_search = @patients_table.search(params)
end
def edit
@@ -2,7 +2,8 @@ class AssignmentDeskController < ApplicationController
before_filter :authenticate_user!
def index
- @patient_search = PatientsTable.new.search(params)
+ @patients_table = PatientsTable.new
+ @patient_search = @patients_table.search(params)
end
def edit
@@ -38,7 +38,8 @@ def reprint
def previous
@current_tab = "previous"
- @patient_search = PatientsTable.new(:chart_number, :name).search(params)
+ @patients_table = PatientsTable.new(:chart_number, :name)
+ @patient_search = @patients_table.search(params)
end
private
@@ -3,7 +3,8 @@ class TreatmentAreas::PatientsController < ApplicationController
before_filter :find_treatment_area
def index
- @patient_search = PatientsTable.new.search(params)
+ @patients_table = PatientsTable.new
+ @patient_search = @patients_table.search(params)
end
def radiology
@@ -27,6 +27,10 @@ def clinic_state
app_config["state"]
end
+ def patient_searches_path(*args)
+ request.url
+ end
+
def real_currency(number)
number_to_currency(number,:delimiter => ",", :unit => "$ ",:separator => ".")
end
@@ -0,0 +1,10 @@
+module CaptureHelper
+ include ActionView::Helpers::CaptureHelper
+ include Haml::Helpers
+
+ def self.helpers
+ @helpers ||= Object.new.extend(self).tap do |h|
+ h.init_haml_helpers
+ end
+ end
+end
@@ -1,17 +1,22 @@
class PatientSearch
include Virtus
+ include ActiveModel::Conversion
+ extend ActiveModel::Naming
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
+ def initialize(params)
+ super params[:patient_search]
+ reset_attributes if params[:commit] == 'Clear'
+ end
+
+ def patients
+ if blank_search?
+ return Patient.none
elsif chart_number.present?
return Patient.where(id: chart_number)
end
@@ -55,4 +60,6 @@ def reset_attributes
def blank_search?
attributes.values.all?(&:blank?)
end
+
+ def persisted?; false; end
end
@@ -8,10 +8,10 @@ def initialize(*fields)
def search(params)
@patient_search = PatientSearch.new(params)
- @patients = patient_search.execute.
+ @patients = patient_search.patients.
paginate(per_page: 30, page: params[:page])
- self
+ @patient_search
end
def each
@@ -26,10 +26,6 @@ def show?(field)
fields.include?(field)
end
- def [](param)
- patient_search.send(param)
- end
-
# Will Paginate methods
def total_pages
@@ -40,23 +36,16 @@ def current_page
(patients.current_page || 1).to_i
end
+ def count
+ patients.total_entries
+ 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
- ActionController::Base.helpers.clone.tap do |h|
- h.extend(Haml::Helpers)
- h.init_haml_helpers
- end
- end
+ CaptureHelper.helpers.capture(patient, &patient_controls) if patient_controls
end
end
@@ -7,11 +7,11 @@
admin_export_patients_path(:filename => "patients"),
:target => "_blank", :class => 'btn btn-small'
-- @patient_search.controls do |patient|
+- @patients_table.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
-= render "patients/patients", patient_search: @patient_search
+= render "patients/patients"
@@ -3,8 +3,8 @@
= header do
%h1 Assignment Desk
-- @patient_search.controls do |patient|
+- @patients_table.controls do |patient|
= link_to "Assign", edit_assignment_desk_path(patient),
class: 'primary'
-= render "patients/patients", patient_search: @patient_search
+= render "patients/patients"
@@ -1,17 +1,29 @@
.bootstrap-form
- %form{method: 'get', autocomplete: 'off'}
+ = form_for @patient_search, method: :get, html: {autocomplete: 'off'} do |f|
.input-append.input-prepend
%span.add-on
- %i.icon-user
- = text_field_tag :chart_number, patient_search[:chart_number],
+ - if @patients_table.show?(:count) && @patients_table.count > 0
+ = @patients_table.count
+ - else
+ %i.icon-user
+ = f.text_field :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 @patients_table.show? :name
+ = f.text_field :name, placeholder: "Name", class: "span2"
+ - if @patients_table.show? :age
+ = f.text_field :age, placeholder: "Age", class: "span1"
+ - if @patients_table.show? :treatment_area_id
+ = f.select :treatment_area_id,
+ TreatmentArea.order('name').map {|t| [t.name, t.id] },
+ include_blank: 'Treatment Area'
+ - if @patients_table.show? :procedure_id
+ = f.select :procedure_id,
+ Procedure.order('code').map {|t| [t.full_description, t.id] },
+ include_blank: 'Procedure'
+ = f.submit "Search", class: 'btn'
+ = f.submit "Clear", class: 'btn'
-- if patient_search.any?
+- if @patients_table.any?
%table
%thead
%tr
@@ -22,7 +34,7 @@
%th Age
%th Sex
%tbody
- - patient_search.each do |patient, controls|
+ - @patients_table.each do |patient, controls|
%tr
%td= patient.id
%td= patient.last_name
@@ -32,7 +44,7 @@
%td= patient.sex
%td.controls= controls
- = will_paginate patient_search
+ = will_paginate @patients_table
- else
%p
@@ -1,6 +1,6 @@
- title 'Check-in Previous Patient'
-- @patient_search.controls do |patient|
+- @patients_table.controls do |patient|
= link_to "Check-in",
new_patient_path(patient: {previous_chart_number: patient}),
class: 'primary'
@@ -10,5 +10,5 @@
= header do
%h1 Check-in Previous Patient
-= render "patients", patient_search: @patient_search
+= render "patients"
@@ -3,7 +3,7 @@
= header do
%h1= "#{@treatment_area.name} Checkout"
-- @patient_search.controls do |patient|
+- @patients_table.controls do |patient|
= link_to_checkout(@treatment_area, patient)
-= render "patients/patients", patient_search: @patient_search
+= render "patients/patients"
@@ -11,19 +11,19 @@ class PatientSearchTest < ActiveSupport::TestCase
first_name: 'Michael', last_name: 'Jordan'
)
- @patient_search = PatientSearch.new
+ @patient_search = PatientSearch.new({})
end
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
+ assert_equal [@jordan_byron], @patient_search.patients
end
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
+ assert_equal [@jordan_byron, @michael_jordan], @patient_search.patients
end
test 'search by assigned treatment area' do
@@ -33,10 +33,10 @@ class PatientSearchTest < ActiveSupport::TestCase
@michael_jordan.treatment_areas << @other_treatment_area
@patient_search.treatment_area_id = @treatment_area.id
- assert_equal [@jordan_byron], @patient_search.execute
+ assert_equal [@jordan_byron], @patient_search.patients
@patient_search.treatment_area_id = @other_treatment_area.id
- assert_equal [@michael_jordan], @patient_search.execute
+ assert_equal [@michael_jordan], @patient_search.patients
end
test 'search by age' do
@@ -46,10 +46,10 @@ class PatientSearchTest < ActiveSupport::TestCase
@michael_jordan.update_attributes(date_of_birth: Date.today - 23.years)
@patient_search.age = 65
- assert_equal [@jordan_byron], @patient_search.execute
+ assert_equal [@jordan_byron], @patient_search.patients
@patient_search.age = 23
- assert_equal [@michael_jordan], @patient_search.execute
+ assert_equal [@michael_jordan], @patient_search.patients
end
test 'search by procedure' do
@@ -61,35 +61,33 @@ class PatientSearchTest < ActiveSupport::TestCase
@michael_jordan.procedures << other_procedure
@patient_search.procedure_id = procedure.id
- assert_equal [@jordan_byron], @patient_search.execute
+ assert_equal [@jordan_byron], @patient_search.patients
@patient_search.procedure_id = other_procedure.id
- assert_equal [@jordan_byron, @michael_jordan], @patient_search.execute
+ assert_equal [@jordan_byron, @michael_jordan], @patient_search.patients
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
+ assert_equal [], @patient_search.patients
+ assert ActiveRecord::Relation === @patient_search.patients
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'
-
- puts @patient_search.commit
+ @patient_search = PatientSearch.new(commit: 'Clear', patient_search: {
+ chart_number: @jordan_byron.id
+ })
- assert_equal [], @patient_search.execute
+ assert_equal [], @patient_search.patients
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
+ @patient_search = PatientSearch.new(commit: 'Clear', patient_search: {
+ chart_number: @jordan_byron.id
+ })
refute @patient_search.chart_number
end

0 comments on commit ee6f364

Please sign in to comment.