module Sequel::Plugins::HybridTableInheritance::ClassMethods

Attributes

cti_instance_dataset[R]

The dataset that table instance datasets are based on. Used for database modifications

cti_models[R]

An array of each model in the inheritance hierarchy that uses an backed by a new table.

cti_subclass_datasets[R]

A hash with subclass models keys and datasets to load that subclass assuming the current model has already been loaded. Used for eager loading

cti_subclass_load[R]

Eager loading option

cti_table_columns[R]

An array of column symbols for the backing database table, giving the columns to update in each backing database table.

cti_table_map[R]

A hash with class name symbol keys and table name symbol values. Specified with the :table_map option to the plugin, and used if the implicit naming is incorrect.

cti_tables[R]

An array of table symbols that back this model. The first is cti_base_model table symbol, and the last is the current model table symbol.

Public Instance Methods

cti_base_model() click to toggle source

The parent/root/base model for this class table inheritance hierarchy. This is the only model in the hierarchy that loads the class_table_inheritance plugin. Only needed to be compatible with class_table_inheritance plugin

# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 215
def cti_base_model
  @cti_models.first
end
cti_columns() click to toggle source

Hash with table name symbol keys and arrays of column symbol values, giving the columns to update in each backing database table. Only needed to be compatible with class_table_inheritance plugin

# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 253
def cti_columns
  h = {}
  cti_models.each { |m| h[m.table_name] = m.cti_table_columns }
  h
end
cti_key() click to toggle source

Alias to single_table_inheritance methods to be compatible with class_table_inheritance plugin

# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 261
def cti_key; sti_key; end
cti_model_map() click to toggle source
# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 262
def cti_model_map; sti_model_map; end
cti_table_model() click to toggle source

Last model in the inheritance hierarchy to use a new table

# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 220
def cti_table_model
  @cti_models.last
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 266
def inherited(subclass)
  ds = sti_dataset

  # Prevent inherited in model/base.rb from setting the dataset
  subclass.instance_eval { @dataset = nil }

  super

  # Set table if this is a class table inheritance
  table = nil
  columns = nil
  if (n = subclass.name) && !n.empty?
    if table = cti_table_map[n.to_sym]
      columns = db.from(table).columns
    else
      table = subclass.implicit_table_name
      columns = db.from(table).columns rescue nil
      table = nil if !columns || columns.empty?
    end
  end
  table = nil if table && (table == table_name)

  return unless table

  pk = primary_key
  subclass.instance_eval do
    if cti_tables.length == 1
      ds = ds.select(*self.columns.map{|cc| Sequel.qualify(table_name, Sequel.identifier(cc))})
    end
    sel_app = (columns - [pk]).map{|cc| Sequel.qualify(table, Sequel.identifier(cc))}
    @sti_dataset = ds.join(table, pk=>pk).select_append(*sel_app)
    set_dataset(@sti_dataset)
    set_columns(self.columns)
    dataset.row_proc = lambda{|r| subclass.sti_load(r)}

    unless cti_subclass_load == :lazy_only
      cti_models.each do |model|
        sd = model.instance_variable_get(:@cti_subclass_datasets)
        unless d = sd[cti_table_model]
          sd[self] = db.from(table).select(*columns.map{|cc| Sequel.qualify(table, Sequel.identifier(cc))})
        else
          sd[self] = d.join(table, pk=>pk).select_append(*sel_app)
        end
      end
    end
    unless cti_subclass_load == :eager_only
      (columns - [pk]).each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>table)}
    end

    @cti_models += [self]
    @cti_tables += [table]
    @cti_table_columns = columns
    @cti_instance_dataset = db.from(table)

    cti_tables.reverse.each do |ct|
      db.schema(ct).each{|sk,v| db_schema[sk] = v}
    end
  end
end
sti_class_from_key(key) click to toggle source
# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 331
def sti_class_from_key(key)
  sti_class(sti_model_map[key])
end
table_name() click to toggle source

The table name for the current model class’s main table.

Calls superclass method
# File lib/sequel/plugins/hybrid_table_inheritance.rb, line 327
def table_name
  cti_tables ? cti_tables.last : super
end