Skip to content

Commit d8debe9

Browse files
kelsinbkeepers
authored andcommitted
Adding support to query all subclasses when using sci
1 parent eedcb49 commit d8debe9

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/mongo_mapper/plugins/sci.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module Plugins
44
module Sci
55
extend ActiveSupport::Concern
66

7+
included do
8+
extend ActiveSupport::DescendantsTracker
9+
end
10+
711
module ClassMethods
812
def inherited(subclass)
913
key :_type, String unless key?(:_type)
@@ -18,7 +22,7 @@ def single_collection_inherited?
1822

1923
def query(options={})
2024
super.tap do |query|
21-
query[:_type] = name if single_collection_inherited?
25+
query[:_type] = {'$in' => [name] + descendants.map(&:name)} if single_collection_inherited?
2226
end
2327
end
2428
end
@@ -31,4 +35,4 @@ def initialize(*args)
3135
end
3236
end
3337
end
34-
end
38+
end

test/functional/test_sci.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ::DocParent
1212
class ::DocDaughter < ::DocParent; end
1313
class ::DocSon < ::DocParent; end
1414
class ::DocGrandSon < ::DocSon; end
15+
class ::DocGrandGrandSon < ::DocGrandSon; end
1516

1617
DocSon.many :children, :class_name => 'DocGrandSon'
1718

@@ -24,6 +25,7 @@ class ::DocGrandSon < ::DocSon; end
2425
Object.send :remove_const, 'DocDaughter' if defined?(::DocDaughter)
2526
Object.send :remove_const, 'DocSon' if defined?(::DocSon)
2627
Object.send :remove_const, 'DocGrandSon' if defined?(::DocGrandSon)
28+
Object.send :remove_const, 'DocGrandGrandSon' if defined?(::DocGrandGrandSon)
2729
end
2830

2931
should "automatically add _type key to store class" do
@@ -86,11 +88,19 @@ class ::DocGrandSon < ::DocSon; end
8688
steve = DocSon.create(:name => 'Steve')
8789
steph = DocDaughter.create(:name => 'Steph')
8890
carrie = DocDaughter.create(:name => 'Carrie')
91+
boris = DocGrandSon.create(:name => 'Boris')
8992

90-
DocGrandSon.all(:order => 'name').should == []
91-
DocSon.all(:order => 'name').should == [john, steve]
93+
DocGrandGrandSon.all(:order => 'name').should == []
94+
DocGrandSon.all(:order => 'name').should == [boris]
95+
DocSon.all(:order => 'name').should == [boris, john, steve]
9296
DocDaughter.all(:order => 'name').should == [carrie, steph]
93-
DocParent.all(:order => 'name').should == [carrie, john, steph, steve]
97+
DocParent.all(:order => 'name').should == [boris, carrie, john, steph, steve]
98+
99+
sigmund = DocGrandGrandSon.create(:name => 'Sigmund')
100+
101+
DocGrandSon.all(:order => 'name').should == [boris, sigmund]
102+
DocSon.all(:order => 'name').should == [boris, john, sigmund, steve]
103+
DocParent.all(:order => 'name').should == [boris, carrie, john, sigmund, steph, steve]
94104
end
95105

96106
should "work with nested hash conditions" do
@@ -227,4 +237,4 @@ class ::OtherChild < ::Parent
227237
Child.new(:_type => 'OtherChild')._type.should == 'Child'
228238
end
229239
end
230-
end
240+
end

0 commit comments

Comments
 (0)