module Sequel::Plugins::ClassTableInheritance::ClassMethods

Attributes

cti_ignore_subclass_columns[R]

An array of columns that may be duplicated in sub-classes. The primary key column is always allowed to be duplicated

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 is backed by a new table.

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 should be used if the implicit naming is incorrect.

cti_tables[R]

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

Public Instance Methods

cti_table_name() click to toggle source

The name of the most recently joined table.

# File lib/sequel/plugins/class_table_inheritance.rb, line 319
def cti_table_name
  cti_tables ? cti_tables.last : dataset.first_source_alias
end
freeze() click to toggle source

Freeze CTI information when freezing model class.

Calls superclass method
# File lib/sequel/plugins/class_table_inheritance.rb, line 242
def freeze
  @cti_models.freeze
  @cti_tables.freeze
  @cti_table_columns.freeze
  @cti_table_map.freeze
  @cti_ignore_subclass_columns.freeze

  super
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/sequel/plugins/class_table_inheritance.rb, line 254
def inherited(subclass)
  ds = sti_dataset

  # Prevent inherited in model/base.rb from setting the dataset
  subclass.instance_exec { @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 = check_non_connection_error(false){db.from(table).columns}
      table = nil if !columns || columns.empty?
    end
  end
  table = nil if table && (table == cti_table_name)

  return unless table

  pk = primary_key
  subclass.instance_exec do
    if cti_tables.length == 1
      ds = ds.select(*self.columns.map{|cc| Sequel.qualify(cti_table_name, Sequel.identifier(cc))})
    end
    cols = (columns - [pk]) - cti_ignore_subclass_columns
    dup_cols = cols & ds.columns
    unless dup_cols.empty?
      raise Error, "class_table_inheritance with duplicate column names (other than the primary key column) is not supported, make sure tables have unique column names (duplicate columns: #{dup_cols}). If this is desired, specify these columns in the :ignore_subclass_columns option when initializing the plugin"
    end
    sel_app = cols.map{|cc| Sequel.qualify(table, Sequel.identifier(cc))}
    @sti_dataset = ds = ds.join(table, pk=>pk).select_append(*sel_app)

    ds = ds.from_self(:alias=>@cti_alias)

    set_dataset(ds)
    set_columns(self.columns)
    @dataset = @dataset.with_row_proc(lambda{|r| subclass.sti_load(r)})
    cols.each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>@cti_alias)}

    @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

The model class for the given key value.

# File lib/sequel/plugins/class_table_inheritance.rb, line 324
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/class_table_inheritance.rb, line 310
def table_name
  if cti_tables && cti_tables.length > 1
    @cti_alias
  else
    super
  end
end

Private Instance Methods

sti_subclass_dataset(key) click to toggle source

If using a subquery for class table inheritance, also use a subquery when setting subclass dataset.

Calls superclass method
# File lib/sequel/plugins/class_table_inheritance.rb, line 332
def sti_subclass_dataset(key)
  ds = super
  if cti_models[0] != self
    ds = ds.from_self(:alias=>@cti_alias)
  end
  ds
end