Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "rubygems"
- require "dm-core"
- require "dm-validations"
- require "spec"
- DataMapper.setup(:default, "sqlite3::memory:")
- module MarkForDestruction
- def mark_for_destruction
- @marked_for_destruction = true
- end
- def marked_for_destruction?
- @marked_for_destruction
- end
- end
- module Save
- def save(*args)
- if marked_for_destruction?
- destroy
- else
- super
- end
- end
- end
- class Person
- include DataMapper::Resource
- include MarkForDestruction
- include Save
- property :id, Serial
- has 1, :profile
- end
- class Profile
- include DataMapper::Resource
- include MarkForDestruction
- include Save
- property :id, Serial
- property :person_id, Integer, :nullable => false
- belongs_to :person
- end
- describe "Resource#save" do
- before(:each) do
- DataMapper.auto_migrate!
- end
- it "should save a newly associated resource" do
- p = Person.new
- p.profile = Profile.new
- p.save
- Person.all.size.should == 1
- Profile.all.size.should == 1
- end
- it "should destroy a resource that is marked_for_destruction" do
- person = Person.create
- person.profile = Profile.create :person => person
- person.profile.mark_for_destruction
- person.send(:child_associations).should_not be_empty
- person.save
- Person.all.size.should == 1
- Profile.all.size.should == 0
- end
- end
Add Comment
Please, Sign In to add comment