Skip to content

Commit

Permalink
Merge pull request mongomapper#275 from Kelsin/sci-subclass-querying
Browse files Browse the repository at this point in the history
Adding support to query all subclasses when using sci
  • Loading branch information
bkeepers committed May 25, 2011
2 parents 19d1e42 + cea80bd commit 314fb8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/mongo_mapper/plugins/sci.rb
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -31,4 +35,4 @@ def initialize(*args)
end
end
end
end
end
18 changes: 14 additions & 4 deletions test/functional/test_sci.rb
Expand Up @@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -227,4 +237,4 @@ class ::OtherChild < ::Parent
Child.new(:_type => 'OtherChild')._type.should == 'Child'
end
end
end
end

0 comments on commit 314fb8a

Please sign in to comment.