Adding comparison using ids rather than hashes

This commit is contained in:
Sam Lown 2011-06-09 01:49:09 +02:00
parent 7875113d95
commit 778e486328
3 changed files with 39 additions and 1 deletions

View file

@ -92,6 +92,21 @@ module CouchRest
alias :to_param :id alias :to_param :id
alias :new_record? :new? alias :new_record? :new?
alias :new_document? :new? alias :new_document? :new?
# Compare this model with another by confirming to see
# if the IDs and their databases match!
#
# Camparison of the database is required in case the
# model has been proxied or loaded elsewhere.
#
# A Basic CouchRest document will only ever compare using
# a Hash comparison on the attributes.
def == other
return false unless other.is_a?(Base)
database == other.database && id == other.id
end
alias :eql? :==
end end
end end
end end

6
spec/fixtures/models/project.rb vendored Normal file
View file

@ -0,0 +1,6 @@
class Project < CouchRest::Model::Base
use_database DB
property :name, String
timestamps!
view_by :name
end

View file

@ -142,8 +142,25 @@ describe "Model Base" do
@obj.destroyed?.should be_true @obj.destroyed?.should be_true
end end
end end
end
describe "comparisons" do
describe "#==" do
it "should be true on same document" do
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
end
end end
describe "update attributes without saving" do describe "update attributes without saving" do