Module | Sequel::Plugins::SingleTableInheritance::ClassMethods |
In: |
lib/sequel/plugins/single_table_inheritance.rb
|
sti_dataset | [R] | The base dataset for STI, to which filters are added to get only the models for the specific STI subclass. |
sti_key | [R] | The column name holding the STI key for this model |
sti_key_array | [R] | Array holding keys for all subclasses of this class, used for the dataset filter in subclasses. Nil in the main class. |
sti_key_chooser | [R] | A proc which returns the value to use for new instances. This defaults to a lookup in the key map. |
sti_key_map | [R] | A hash/proc with class keys and column value values, mapping the class to a particular value given to the sti_key column. Used to set the column value when creating objects, and for the filter when retrieving objects in subclasses. |
sti_model_map | [R] | A hash/proc with column value keys and class values, mapping the value of the sti_key column to the appropriate class to use. |
Copy the necessary attributes to the subclasses, and filter the subclass‘s dataset based on the sti_kep_map entry for the class.
# File lib/sequel/plugins/single_table_inheritance.rb, line 152 152: def inherited(subclass) 153: super 154: sk = sti_key 155: sd = sti_dataset 156: skm = sti_key_map 157: smm = sti_model_map 158: skc = sti_key_chooser 159: key = Array(skm[subclass]).dup 160: sti_subclass_added(key) 161: rp = dataset.row_proc 162: subclass.set_dataset(sd.filter(SQL::QualifiedIdentifier.new(table_name, sk)=>key), :inherited=>true) 163: subclass.instance_eval do 164: dataset.row_proc = rp 165: @sti_key = sk 166: @sti_key_array = key 167: @sti_dataset = sd 168: @sti_key_map = skm 169: @sti_model_map = smm 170: @sti_key_chooser = skc 171: self.simple_table = nil 172: end 173: end
Return an instance of the class specified by sti_key, used by the row_proc.
# File lib/sequel/plugins/single_table_inheritance.rb, line 177 177: def sti_load(r) 178: sti_class(sti_model_map[r[sti_key]]).call(r) 179: end
Make sure that all subclasses of the parent class correctly include keys for all of their descendant classes.
# File lib/sequel/plugins/single_table_inheritance.rb, line 183 183: def sti_subclass_added(key) 184: if sti_key_array 185: key_array = Array(key) 186: Sequel.synchronize{sti_key_array.push(*key_array)} 187: superclass.sti_subclass_added(key) 188: end 189: end