Module: Sequel::Plugins::ClassTableInheritance::ClassMethods

Defined in:
lib/sequel/plugins/class_table_inheritance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cti_instance_datasetObject (readonly)

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



203
204
205
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 203

def cti_instance_dataset
  @cti_instance_dataset
end

#cti_modelsObject (readonly)

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



188
189
190
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 188

def cti_models
  @cti_models
end

#cti_table_columnsObject (readonly)

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



199
200
201
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 199

def cti_table_columns
  @cti_table_columns
end

#cti_table_mapObject (readonly)

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.



213
214
215
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 213

def cti_table_map
  @cti_table_map
end

#cti_tablesObject (readonly)

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.



208
209
210
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 208

def cti_tables
  @cti_tables
end

Instance Method Details

#cti_base_modelObject

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. For backwards compatibility.



193
194
195
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 193

def cti_base_model
  @cti_models.first
end

#cti_columnsObject

Hash with table name symbol keys and arrays of column symbol values, giving the columns to update in each backing database table. For backwards compatibility.



218
219
220
221
222
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 218

def cti_columns
  h = {}
  cti_models.each { |m| h[m.table_name] = m.cti_table_columns }
  h
end

#cti_keyObject

Alias to sti_key, for backwards compatibility.



225
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 225

def cti_key; sti_key; end

#cti_model_mapObject

Alias to sti_model_map, for backwards compatibility.



228
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 228

def cti_model_map; sti_model_map; end

#inherited(subclass) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 232

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)}
    (columns - [pk]).each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>table)}

    @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) ⇒ Object



284
285
286
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 284

def sti_class_from_key(key)
  sti_class(sti_model_map[key])
end

#table_nameObject

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



280
281
282
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 280

def table_name
  cti_tables ? cti_tables.last : super
end