Skip to content

Instantly share code, notes, and snippets.

@ramhoj
Created April 27, 2010 14:41
Show Gist options
  • Save ramhoj/380792 to your computer and use it in GitHub Desktop.
Save ramhoj/380792 to your computer and use it in GitHub Desktop.
class CallsController < ApplicationController
calls_controller
before_filter :resource, :only => [:create, :destroy]
before_filter :check_for_valid_call, :only => [:update, :destroy, :show]
before_filter :check_for_other_call, :only => [:create]
def create
@patient.calls << PatientCall.new
begin
@patient.save!
current_user.start_working!(@patient)
redirect_to verify_patient_personal_information_path(@patient)
rescue Mongoid::Errors::Validations => e
AppError.create({:message => e.message, :document_id => @patient.id, :document_collection => 'Patient'})
flash[:error] = "You cannot begin service on this patient because their record is invalid."
redirect_to edit_patient_path(@patient)
end
end
update! do |success, failure|
success.html do
@patient.save!
if resource.fully_verified? or resource.skipped_personal_information_verification?
json_redirect_to patient_call_path(@patient), :load_in_dialog => true
else
redirect_to_edit_and_verify
end
end
end
def show
@interventions = @patient.pending_interventions
resource.completion_action = 'failed' if resource.skipped_personal_information_verification
end
def destroy
current_user.stop_working!(@patient)
resource.cancel!
flash[:notice] = "Call for #{@patient.full_name} canceled"
json_redirect_to root_url
end
private
def check_for_other_call
if current_user.working_patient && current_user.working_patient_id != @patient.id
current_patient = current_user.working_patient
flash[:error] = "You cannot start a call with this patient because you are already on a call with #{@template.link_to(current_patient.full_name, edit_patient_path(current_patient))}</a>"
json_redirect_to edit_patient_path(@patient)
end
end
def check_for_valid_call
if resource.nil? or current_user.working_patient_id != @patient.id
flash[:error] = "You cannot complete this patient because you have not started a call with this patient"
json_redirect_to edit_patient_path(@patient)
end
end
def redirect_to_edit_and_verify
flash[:error] = "All steps must be verified before completing this call"
json_redirect_to edit_patient_path(@patient)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment