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,7 +99,12 @@ 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)
database == other.database && id == other.id 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
end
end end
alias :eql? :== alias :eql? :==

View file

@ -154,19 +154,38 @@ describe "Model Base" do
describe "comparisons" do describe "comparisons" do
describe "#==" do describe "#==" do
it "should be true on same document" do context "on saved document" do
p = Project.create it "should be true on same document" do
p.should eql(p) p = Project.create
p.should eql(p)
end
it "should be true after loading" do
p = Project.create
p.should eql(Project.get(p.id))
end
it "should not be true if databases do not match" do
p = Project.create
p2 = p.dup
p2.stub!(:database).and_return('other')
p.should_not eql(p2)
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 end
it "should be true after loading" do context "with new documents" do
p = Project.create it "should be true when attributes match" do
p.should eql(Project.get(p.id)) p = Project.new(:name => 'test')
end o = Project.new(:name => 'test')
it "should not be true if databases do not match" do p.should eql(o)
p = Project.create end
p2 = p.dup it "should not be true when attributes don't match" do
p2.stub!(:database).and_return('other') p = Project.new(:name => 'test')
p.should_not eql(p2) o = Project.new(:name => 'testing')
p.should_not eql(o)
end
end end
end end
end end