Skip to content

Instantly share code, notes, and snippets.

@Ruxton
Forked from osiro/driver.rb
Created March 6, 2012 07:37
Show Gist options
  • Save Ruxton/1984719 to your computer and use it in GitHub Desktop.
Save Ruxton/1984719 to your computer and use it in GitHub Desktop.
class Driver
....
after_destroy :set_new_current_driver_to_insurance_detail
protected
def set_new_current_driver_to_insurance_detail
@insurance_detail = self.insurance_detail
@insurance_detail.update_attribute(:current_driver, @insurance_detail.drivers.first) if @insurance_detail.drivers.any? and @insurance_detail.current_driver_id == self.id
end
end
describe "#set_new_current_driver_to_insurance_detail" do
let(:driver) { Factory :driver }
let(:insurance_detail) { Factory :insurance_detail }
context "when insurance detail has no more drivers" do
before(:each) do
driver.update_attribute(:insurance_detail, insurance_detail)
insurance_detail.update_attribute(:current_driver, driver)
end
it "should not change current_driver_id" do
original_id = insurance_detail.current_driver_id
driver.destroy
insurance_detail.reload
expect { insurance_detail.current_driver_id == original_id }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment