Skip to content
Browse files

Starting work on lab_requests controller. ATP.

  • Loading branch information...
1 parent c969869 commit e2694f92291cf2c8a4b3bc58720e4faf5e8bd139 @MikeBlyth committed
View
28 app/controllers/lab_requests_controller.rb
@@ -5,4 +5,32 @@ class LabRequestsController < ApplicationController
config.columns[:provider].form_ui = :select
config.columns[:comments].inplace_edit = true
end
+
+ def create
+ # selected_services = params.delete(:services)
+ add_services
+ if @record = LabRequest.create(params[:lab_request])
+ flash[:notice] = 'Successfully created lab request'
+ else
+ render :new
+ end
+ end
+
+ def update
+ # selected_services = params.delete(:services)
+ new_params = params[:lab_request]
+ patient_id = new_params.delete(:patient_id)
+ add_services
+ if @record.update_attributes(new_params)
+ flash[:notice] = 'Successfully updated'
+ else
+ render :new
+ end
+ end
+
+ def add_services
+ params[:lab_request][:lab_results_attributes] =
+ (params[:services] || {}).keys.map {|s| {lab_service_id: s.to_i, status: :pending}}
+ end
+
end
View
3 app/models/lab_request.rb
@@ -12,12 +12,13 @@
class LabRequest < ActiveRecord::Base
- attr_accessible :date, :hct, :patient_id, :provider_id, :patient, :provider
+ attr_accessible :date, :hct, :patient_id, :provider_id, :patient, :provider, :lab_results_attributes
belongs_to :patient
belongs_to :provider
has_many :lab_results
validates_presence_of :provider_id, :patient_id
before_validation :default_date
+ accepts_nested_attributes_for :lab_results
def to_s
"Lab requests for #{patient}, #{date}"
View
2 app/models/lab_result.rb
@@ -21,7 +21,7 @@ class LabResult < ActiveRecord::Base
belongs_to :lab_request
belongs_to :lab_service
has_one :patient, through: :lab_request
- validates_presence_of :lab_request_id, :lab_service_id
+ validates_presence_of :lab_service_id
after_find :numerify_result
# Get selected labs (by abbrevs) for patient covering "days_since" days from present
View
2 app/views/lab_results/_select.html.haml
@@ -1,3 +1,3 @@
-= check_box_tag("service[#{service[:id]}]", 1, service[:selected])
+= check_box_tag("services[#{service[:id]}]", 1, service[:selected])
= service[:abbrev]
%br
View
38 spec/controllers/lab_requests_controller_spec.rb
@@ -0,0 +1,38 @@
+require "spec_helper"
+
+# Given an array of lab service ids [5,6], and the base params, return params with
+# the lab services inserted in the way that the is done by the view, e.g.
+# params updated to {:lab_request => {...}, :services => {'5' =>'1', '6' =>'6'}} or whatever
+def set_selected_services_params(params, selected_service_ids)
+ svc_hash = {}
+ selected_service_ids.each {|id| svc_hash[id.to_s] = '1'}
+ params[:services] = svc_hash
+ return params
+end
+
+describe LabRequestsController do
+
+ let(:patient) {FactoryGirl.create(:patient)}
+ let(:provider) {FactoryGirl.create(:provider)}
+ let(:base_params) { {:lab_request => {patient_id: patient.id, provider_id: provider.id}, :services => {}} }
+ it 'creates an empty request (no labs ordered)' do
+ params = {:lab_request => {patient_id: patient.id, provider_id: provider.id}}
+ lambda {post :create, params}.should change(LabRequest, :count).by(1)
+ request = LabRequest.last
+ request.provider.should eq provider
+ request.patient.should eq patient
+ request.date.to_date.should eq Date.today
+ end
+
+ it 'creates a request with labs ordered' do
+ selected = [5,6]
+ params = set_selected_services_params(base_params, selected)
+ lambda {post :create, params}.should change(LabResult, :count).by(2)
+ request = LabRequest.last
+ results = request.lab_results
+ results.map {|r| r.lab_service_id}.sort.should eq selected
+ results.map {|r| r.lab_request_id}.should eq [request.id, request.id]
+ end
+
+
+end
View
1 spec/models/lab_result_spec.rb
@@ -4,7 +4,6 @@
let(:lab_result) {FactoryGirl.build(:lab_result)}
describe 'validation' do
- it { should validate_presence_of(:lab_request_id)}
it { should validate_presence_of(:lab_service_id)}
end
View
2 spec/other/growth_chart_spec.rb
@@ -51,7 +51,7 @@ def point_match(point, nominal)
it 'creates cd4 series' do
cd4_series = @chart.cd4_series.sort # sorting is normally done at rendering time
- puts "labs = #{LabResult.all}"
+#puts "labs = #{LabResult.all}"
point_match(cd4_series[:data][0], [0.5,1000])
point_match(cd4_series[:data][1], [1.0,300])
cd4_series[:x_name].should == :age

0 comments on commit e2694f9

Please sign in to comment.
Something went wrong with that request. Please try again.