Permalink
Browse files

Adding support to query all subclasses when using sci

  • Loading branch information...
kelsin authored and bkeepers committed May 19, 2011
1 parent eedcb49 commit d8debe91575c38f6db5c59abe867cd605fea8724
Showing with 20 additions and 6 deletions.
  1. +6 −2 lib/mongo_mapper/plugins/sci.rb
  2. +14 −4 test/functional/test_sci.rb
@@ -4,6 +4,10 @@ module Plugins
module Sci
extend ActiveSupport::Concern
+ included do
+ extend ActiveSupport::DescendantsTracker
+ end
+
module ClassMethods
def inherited(subclass)
key :_type, String unless key?(:_type)
@@ -18,7 +22,7 @@ def single_collection_inherited?
def query(options={})
super.tap do |query|
- query[:_type] = name if single_collection_inherited?
+ query[:_type] = {'$in' => [name] + descendants.map(&:name)} if single_collection_inherited?
end
end
end
@@ -31,4 +35,4 @@ def initialize(*args)
end
end
end
-end
+end
@@ -12,6 +12,7 @@ class ::DocParent
class ::DocDaughter < ::DocParent; end
class ::DocSon < ::DocParent; end
class ::DocGrandSon < ::DocSon; end
+ class ::DocGrandGrandSon < ::DocGrandSon; end
DocSon.many :children, :class_name => 'DocGrandSon'
@@ -24,6 +25,7 @@ class ::DocGrandSon < ::DocSon; end
Object.send :remove_const, 'DocDaughter' if defined?(::DocDaughter)
Object.send :remove_const, 'DocSon' if defined?(::DocSon)
Object.send :remove_const, 'DocGrandSon' if defined?(::DocGrandSon)
+ Object.send :remove_const, 'DocGrandGrandSon' if defined?(::DocGrandGrandSon)
end
should "automatically add _type key to store class" do
@@ -86,11 +88,19 @@ class ::DocGrandSon < ::DocSon; end
steve = DocSon.create(:name => 'Steve')
steph = DocDaughter.create(:name => 'Steph')
carrie = DocDaughter.create(:name => 'Carrie')
+ boris = DocGrandSon.create(:name => 'Boris')
- DocGrandSon.all(:order => 'name').should == []
- DocSon.all(:order => 'name').should == [john, steve]
+ DocGrandGrandSon.all(:order => 'name').should == []
+ DocGrandSon.all(:order => 'name').should == [boris]
+ DocSon.all(:order => 'name').should == [boris, john, steve]
DocDaughter.all(:order => 'name').should == [carrie, steph]
- DocParent.all(:order => 'name').should == [carrie, john, steph, steve]
+ DocParent.all(:order => 'name').should == [boris, carrie, john, steph, steve]
+
+ sigmund = DocGrandGrandSon.create(:name => 'Sigmund')
+
+ DocGrandSon.all(:order => 'name').should == [boris, sigmund]
+ DocSon.all(:order => 'name').should == [boris, john, sigmund, steve]
+ DocParent.all(:order => 'name').should == [boris, carrie, john, sigmund, steph, steve]
end
should "work with nested hash conditions" do
@@ -227,4 +237,4 @@ class ::OtherChild < ::Parent
Child.new(:_type => 'OtherChild')._type.should == 'Child'
end
end
-end
+end

0 comments on commit d8debe9

Please sign in to comment.