module Sequel::Plugins::SingleTableInheritance::ClassMethods
def dataset_extend(mod, opts=OPTS)
Extend the sti dataset with the module when extending the main
def dataset_extend(mod, opts=OPTS) @sti_dataset = @sti_dataset.with_extend(mod) super end
def freeze
because of how STI works, you should not freeze an STI subclass
Freeze STI information when freezing model class. Note that
def freeze @sti_key_array.freeze if @sti_key_array @sti_key_map.freeze if @sti_key_map.is_a?(Hash) @sti_model_map.freeze if @sti_model_map.is_a?(Hash) super end
def inherited(subclass)
Copy the necessary attributes to the subclasses, and filter the
def inherited(subclass) super key = Array(sti_key_map[subclass]).dup sti_subclass_added(key) rp = dataset.row_proc subclass.set_dataset(sti_subclass_dataset(key), :inherited=>true) subclass.instance_exec do @dataset = @dataset.with_row_proc(rp) @sti_key_array = key self.simple_table = nil end end
def set_dataset_row_proc(ds)
If calling set_dataset manually, make sure to set the dataset
def set_dataset_row_proc(ds) if @dataset ds.with_row_proc(@dataset.row_proc) else super end end
def sti_class(v)
an invalid class name string or symbol is used, return self.
Treat strings and symbols as class names. If nil is given or
Return a class object. If a class is given, return it directly.
def sti_class(v) case v when String, Symbol constantize(v) rescue self when nil self when Class v else raise(Error, "Invalid class type used: #{v.inspect}") end end
def sti_class_from_sti_key(key)
def sti_class_from_sti_key(key) sti_class(sti_model_map[key]) end
def sti_load(r)
Return an instance of the class specified by sti_key,
def sti_load(r) sti_class_from_sti_key(r[sti_key]).call(r) end
def sti_subclass_added(key)
Make sure that all subclasses of the parent class correctly include
def sti_subclass_added(key) if sti_key_array key_array = Array(key) Sequel.synchronize{sti_key_array.push(*key_array)} superclass.sti_subclass_added(key) end end
def sti_subclass_dataset(key)
Use the given dataset for the subclass, with key being the allowed
def sti_subclass_dataset(key) sti_dataset.where(SQL::QualifiedIdentifier.new(sti_dataset.first_source_alias, sti_key)=>Sequel.delay{Sequel.synchronize{key}}) end