require_relative 'resource' module Medivo class Order < Resource set_element_name 'customer' set_collection_name 'customers' validates_presence_of :first_name, :last_name, :email, :address, :state, :zip, :dob, :home_phone, :psc, :test_types, :gender, :account_number validates_inclusion_of :gender, :in => %w(Male Female) validates_with Medivo::DateValidator, :fields=>[:dob] validates_with Medivo::TestTypesValidator, :fields=>[:test_types] extend ActiveModel::Callbacks # need to set up this dummy call .. for subclass to be able to use it at all define_model_callbacks :create, :only=>[] def initialize(attributes = {}, persisted = false) super(attributes, persisted) self.draw_location = "PSC" self.take_tests_same_day = true end def read_attribute_for_validation(key) @attributes[key] end # overriding save to return false if it does not save without errors def save new? ? create : update valid? end # overriding create to capture 404 status and put that in error # messages instead of throwing exception def create run_callbacks(:create) super @remote_errors = nil rescue ActiveResource::ResourceNotFound => e @remote_errors = e load_remote_errors(e, true) end def to_param requisition_id end def requisition_id requisition_number end def self.get_order(requsition_id) find(requsition_id) end # loads the results with order def self.find_with_results(requsition_id) find(requsition_id, :params=>{:include=>:reconciled_results}) end # must use #find_with_results to get the order before you call this method def results Array.wrap(try(:reconciled_results).try(:result)) end # must use #find_with_results to get the order before you call this method def results_summary try(:reconciled_results).try(:results_summary) end def simulate_result(lab_prefix, type='normal') raise "Incorrect Result Type" unless %w(normal false_positive positive).include?(type) and %w(qd lc).include?(lab_prefix) body = case type when 'normal' then "result" when 'false_positive', 'positive' then "result#{Base64.encode64(File.read("lib/pdfs/#{lab_prefix}_#{type}.hl7"))}" end post(:mock_order, {}, body) end end end