Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use the correct class when loading instances for descendents of model…

… classes that use single table inheritance

Before, only the model class that loaded the
single_table_inheritance plugin had the logic to return instances
of subclasses.  This copies that logic into subclasses, so that if
you load all instances of a subclass, it can return instances of
subsubclasses.  Thanks to Max Aller on sequel-talk for pointing
out this bug.
  • Loading branch information...
commit c9db41ce9886e8b9596038fb2ca8998edfd9ade6 1 parent 66be40d
@jeremyevans authored
View
2  CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Use the correct class when loading instances for descendents of model classes that use single table inheritance (jeremyevans)
+
* Support for COLLATE in column definitions (jfirebaugh)
* Don't use a schema when creating a temporary table (jeremyevans)
View
2  lib/sequel/plugins/single_table_inheritance.rb
@@ -108,8 +108,10 @@ def inherited(subclass)
key = skm[subclass]
sti_subclass_added(key)
ska = [key]
+ rp = dataset.row_proc
subclass.set_dataset(sd.filter(SQL::QualifiedIdentifier.new(table_name, sk)=>ska), :inherited=>true)
subclass.instance_eval do
+ dataset.row_proc = rp
@sti_key = sk
@sti_key_array = ska
@sti_dataset = sd
View
11 spec/extensions/single_table_inheritance_spec.rb
@@ -54,6 +54,17 @@ def @ds.fetch_rows(sql)
StiTest.all.collect{|x| x.class}.should == [StiTest, StiTestSub1, StiTestSub2]
end
+ it "should return rows with the correct class for subclasses based on the polymorphic_key value" do
+ class ::StiTestSub1Sub < StiTestSub1
+ end
+ ds = StiTestSub1.dataset
+ def ds.fetch_rows(sql)
+ yield({:kind=>'StiTestSub1'})
+ yield({:kind=>'StiTestSub1Sub'})
+ end
+ StiTestSub1.all.collect{|x| x.class}.should == [StiTestSub1, StiTestSub1Sub]
+ end
+
it "should fallback to the main class if the given class does not exist" do
def @ds.fetch_rows(sql)
yield({:kind=>'StiTestSub3'})
Please sign in to comment.
Something went wrong with that request. Please try again.