Last active
March 29, 2016 16:40
-
-
Save mrdougwright/7ef85a0ce930554078a2db0f37c5b7a2 to your computer and use it in GitHub Desktop.
CreateFind Method in Lib, with Spec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
require 'create/create_find_resident' | |
describe CreateFindResident do | |
let!(:resident) { FactoryGirl.create :resident } | |
let(:resident_data) { | |
{ | |
"DisplayName"=>"Smith, Bob M", | |
"ResidentSys"=>123, | |
"UTCAdmissionDate"=>"2015-08-13 14:30:00 +0000", | |
"AdmissionLocation"=>"1FL\\S\\MC\\35\\A" | |
} | |
} | |
context ".with_record" do | |
it "retrieves a resident (if exists) by resident_sys id" do | |
expect(CreateFindResident.with_record(resident_data).id).to eq(resident.id) | |
end | |
it "creates a resident if can't find by resident_sys id" do | |
resident_data.tap{|r| r['ResidentSys'] = 666 } | |
expect{CreateFindResident.with_record(resident_data)}.to change{|f| Resident.count}.by(1) | |
end | |
end | |
end | |
module CreateFindResident | |
def self.with_record(record) | |
resident = Resident.find_by_resident_sys(record['ResidentSys']) | |
unless resident | |
resident = Resident.create!( | |
first_name: record['DisplayName'].split(", ")[1], | |
last_name: record['DisplayName'].split(", ")[0], | |
age: 99, sex: ["M","F"].sample, facility_name: 'Senior Care Centers', | |
facility_id: 1, room_number: record['AdmissionLocation'], | |
admitted_at: record['UTCAdmissionDate'], resident_sys: record['ResidentSys'] | |
) | |
end | |
resident | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment