Skip to content

Instantly share code, notes, and snippets.

@royw
Created January 4, 2009 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royw/43165 to your computer and use it in GitHub Desktop.
Save royw/43165 to your computer and use it in GitHub Desktop.
# divorce specs
#
# A divorce will dissolve the relationship between two objects, leaving
# the objects intact. Usage is symmetrical for all relationships
#
# Usage:
#
# 1:1 class Alpha belongs to Beta
# a.beta = b
# a.beta.divorce
# or
# b.alpha.divorce
#
# 1:n class Alpha belongs to Beta
# a.beta = b
# a.beta.divorce
# or
# b.alphas[0].divorce
#
# n:n class Alpha habtm Beta
# a.betas << b
# a.betas[0].divorce
# or
# b.alphas[0].divorce
#
before
class Alpha
include DataMapper::Resource
property :id, Serial
belongs_to :beta
belongs_to :charlie
has n, :deltas, :through => Resource
end
class Beta
include DataMapper::Resource
property :id, Serial
has 1, :alpha
end
class Charlie
include DataMapper::Resource
property :id, Serial
has n, :alphas
end
class Delta
include DataMapper::Resource
property :id, Serial
has n, :alphas, :through => Resource
end
DateMapper.auto_migrate!
end
it "should remove the 1:1 relationship from belongs side without removing either object" do
a = Alpha.create
b = Beta.create
a.beta = b
a.save
raise SetupError unless (a.beta == b) && (a == b.alpha)
a.beta.divorce
a.save
Alpha.first(:id => a.id).should_not be_nil &&
Beta.first(:id => b.id).should_not be_nil &&
a.beta.should be_nil &&
b.alpha.should be_nil
end
it "should remove the 1:1 relationship from has side without removing either object" do
a = Alpha.create
b = Beta.create
a.beta = b
a.save
raise SetupError unless (a.beta == b) && (a == b.alpha)
b.alpha.divorce
b.save
Alpha.first(:id => a.id).should_not be_nil &&
Beta.first(:id => b.id).should_not be_nil &&
a.beta.should be_nil &&
b.alpha.should be_nil
end
it "should remove the 1:n relationship from the belongs side without removing either object" do
a1 = Alpha.create
a2 = Alpha.create
c = Charlie.create
a1.charlie = c
a1.save
c.alphas << a2
c.save
raise SetupError unless (a1.charlie == c) && (a2.charlie == c) && (c.alphas.length == 2)
a1.charlie.divorce
a1.save
Alpha.first(:id => a1.id).should_not be_nil &&
Alpha.first(:id => a2.id).should_not be_nil &&
Charlie.first(:id => c.id).should_not be_nil &&
Alpha.first(:id => a1.id).charlie.should be_nil &&
c.alphas.length.should == 1
end
it "should remove the 1:n relationship from the has side without removing either object" do
a1 = Alpha.create
a2 = Alpha.create
c = Charlie.create
a1.charlie = c
a1.save
c.alphas << a2
c.save
raise SetupError unless (a1.charlie == c) && (a2.charlie == c) && (c.alphas.length == 2)
c.alphas[0].divorce
c.save
Alpha.first(:id => a1.id).should_not be_nil &&
Alpha.first(:id => a2.id).should_not be_nil &&
Charlie.first(:id => c.id).should_not be_nil &&
Alpha.first(:id => a1.id).charlie.should be_nil &&
c.alphas.length.should == 1
end
it "should remove the n:n relationship without removing either object" do
a1 = Alpha.create
a2 = Alpha.create
d1 = Delta.create
d2 = Delta.create
a1.deltas << d1
a1.save
d2.alphas << a2
d2.save
raise SetupError unless (a1.deltas.length == 1) && (a2.deltas.length == 1) && (AlphaDelta.count == 2)
a1.deltas[0].divorce
a1.save
Alpha.first(:id => a1.id).should_not be_nil &&
Alpha.first(:id => a2.id).should_not be_nil &&
Delta.first(:id => d1.id).should_not be_nil &&
Delta.first(:id => d2.id).should_not be_nil &&
AlphaDelta.count.should == 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment