Skip to content

Commit

Permalink
Added a few details to SCI.
Browse files Browse the repository at this point in the history
Allows getting the sci parent and root. Needed for scoped identity map
that is shared for sci models.
  • Loading branch information
jnunemaker committed Jul 9, 2012
1 parent 16eaf2d commit 26cdfe0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/mongo_mapper/plugins/sci.rb
Expand Up @@ -13,9 +13,30 @@ def inherited(subclass)
key :_type, String unless key?(:_type)
subclass.instance_variable_set("@single_collection_inherited", true)
subclass.set_collection_name(collection_name) unless subclass.embeddable?
subclass.single_collection_parent = self
super
end

def single_collection_root
parent = single_collection_parent || self
root = parent

while parent
parent = parent.single_collection_parent
root = parent unless parent.nil?
end

root
end

def single_collection_parent
@single_collection_parent
end

def single_collection_parent=(parent)
@single_collection_parent = parent
end

def single_collection_inherited?
@single_collection_inherited == true
end
Expand Down
16 changes: 16 additions & 0 deletions test/functional/test_sci.rb
Expand Up @@ -36,6 +36,22 @@ class ::DocGrandGrandSon < ::DocGrandSon; end
DocDaughter.collection.name.should == DocParent.collection.name
end

should "know single_collection_parent" do
DocParent.single_collection_parent.should be_nil
DocDaughter.single_collection_parent.should == DocParent
DocSon.single_collection_parent.should == DocParent
DocGrandSon.single_collection_parent.should == DocSon
DocGrandGrandSon.single_collection_parent.should == DocGrandSon
end

should "know single_collection_root" do
DocParent.single_collection_root.should == DocParent
DocDaughter.single_collection_root.should == DocParent
DocSon.single_collection_root.should == DocParent
DocGrandSon.single_collection_root.should == DocParent
DocGrandGrandSon.single_collection_root.should == DocParent
end

context ".single_collection_inherited?" do
should "be false if has not inherited" do
DocParent.should_not be_single_collection_inherited
Expand Down

0 comments on commit 26cdfe0

Please sign in to comment.