Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Document#== checks the classes as well as ids

  • Loading branch information...
commit d7d5de18aa33a8ccb13807c56e304c00f840a7bc 1 parent b8e8d08
@durran durran authored
Showing with 32 additions and 10 deletions.
  1. +2 −2 lib/mongoid/document.rb
  2. +30 −8 spec/unit/mongoid/document_spec.rb
View
4 lib/mongoid/document.rb
@@ -34,8 +34,8 @@ def <=>(other)
#
# @return [ true, false ] True if the ids are equal, false if not.
def ==(other)
- return false unless other.is_a?(Document)
- raw_attributes["_id"] == other.raw_attributes["_id"]
+ self.class == other.class &&
+ raw_attributes["_id"] == other.raw_attributes["_id"]
end
# Performs class equality checking.
View
38 spec/unit/mongoid/document_spec.rb
@@ -27,23 +27,45 @@
context "when comparable is a document" do
- let(:other) do
- Person.new
- end
-
context "when it has the same id" do
- before do
- other.id = person.id
+ context "when the classes are not the same" do
+
+ let(:other) do
+ Post.new
+ end
+
+ before do
+ other.id = person.id
+ end
+
+ it "returns false" do
+ person.should_not == other
+ end
end
- it "returns true" do
- person.should == other
+ context "when the classes are the same" do
+
+ let(:other) do
+ Person.new
+ end
+
+ before do
+ other.id = person.id
+ end
+
+ it "returns true" do
+ person.should == other
+ end
end
end
context "when it has a different id" do
+ let(:other) do
+ Person.new
+ end
+
context "when the instances are the same" do
it "returns true" do
Please sign in to comment.
Something went wrong with that request. Please try again.