Comparing using attribute hash if ids are nil

This commit is contained in:
Sam Lown 2011-06-25 02:30:47 +02:00
parent a8a1372e57
commit e91812ca53
3 changed files with 38 additions and 13 deletions

View file

@ -9,6 +9,7 @@
* Validation callbacks now support context (thanks kostia) * Validation callbacks now support context (thanks kostia)
* Document comparisons now performed using database and document ID (pointer by neocsr) * Document comparisons now performed using database and document ID (pointer by neocsr)
* Automatic config generation now supported (thanks lucasrenan) * Automatic config generation now supported (thanks lucasrenan)
* Comparing documents resorts to Hash comparison if both IDs are nil. (pointer by kostia)
## 1.1.0.rc1 - 2011-06-08 ## 1.1.0.rc1 - 2011-06-08

View file

@ -99,8 +99,13 @@ module CouchRest
# a Hash comparison on the attributes. # a Hash comparison on the attributes.
def == other def == other
return false unless other.is_a?(Base) return false unless other.is_a?(Base)
if id.nil? && other.id.nil?
# no ids? assume comparing nested and revert to hash comparison
to_hash == other.to_hash
else
database == other.database && id == other.id database == other.database && id == other.id
end end
end
alias :eql? :== alias :eql? :==
end end

View file

@ -154,6 +154,7 @@ describe "Model Base" do
describe "comparisons" do describe "comparisons" do
describe "#==" do describe "#==" do
context "on saved document" do
it "should be true on same document" do it "should be true on same document" do
p = Project.create p = Project.create
p.should eql(p) p.should eql(p)
@ -168,6 +169,24 @@ describe "Model Base" do
p2.stub!(:database).and_return('other') p2.stub!(:database).and_return('other')
p.should_not eql(p2) p.should_not eql(p2)
end end
it "should always be false if one document not saved" do
p = Project.create(:name => 'test')
o = Project.new(:name => 'test')
p.should_not eql(o)
end
end
context "with new documents" do
it "should be true when attributes match" do
p = Project.new(:name => 'test')
o = Project.new(:name => 'test')
p.should eql(o)
end
it "should not be true when attributes don't match" do
p = Project.new(:name => 'test')
o = Project.new(:name => 'testing')
p.should_not eql(o)
end
end
end end
end end