Skip to content

Instantly share code, notes, and snippets.

@pathsny
Created May 5, 2010 18:50
Show Gist options
  • Save pathsny/391253 to your computer and use it in GitHub Desktop.
Save pathsny/391253 to your computer and use it in GitHub Desktop.
module DataMapper
module Resource
def transactional_save
transaction do |t|
returning save do |s|
t.rollback unless s
end
end
end
end
end
require File.expand_path('../../../spec_helper', __FILE__)
describe DataMapper::Resource do
transactions :off
class Human
include DataMapper::Resource
property :id, Serial
has n, :pets
end
class Pet
include DataMapper::Resource
belongs_to :human
property :id, Serial
property :name, String, :unique => true
end
before :each do
@human = Human.new
@pet_1 = Pet.new(:name => "puddy")
@pet_2 = Pet.new(:name => "tat")
@human.pets << @pet_1
@human.pets << @pet_2
end
it 'saves the human if all pets also save' do
@human.transactional_save.should be_true
Human.count.should == 1
Pet.count.should == 2
end
it 'does not save the human if one pet does not save' do
@pet_2.name = "puddy"
@human.should be_valid
@human.transactional_save.should be_false
Human.should be_empty
Pet.should be_empty
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment