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



229
230
231
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 229

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.



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

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.



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

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



239
240
241
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 239

def cti_table_map
  @cti_table_map
end

#cti_tablesObject (readonly)

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.



234
235
236
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 234

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.



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

def cti_base_model
  Sequel::Deprecation.deprecate("#{self}.cti_base_model", "Use #{self}.cti_models.first instead")
  @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.



244
245
246
247
248
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 244

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

#cti_keyObject

Alias to sti_key, for backwards compatibility.



251
252
253
254
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 251

def cti_key
  Sequel::Deprecation.deprecate("#{self}.cti_key", "Use #{self}.sti_key instead")
  sti_key
end

#cti_model_mapObject

Alias to sti_model_map, for backwards compatibility.



257
258
259
260
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 257

def cti_model_map
  Sequel::Deprecation.deprecate("#{self}.cti_model_map", "Use #{self}.sti_model_map instead")
  sti_model_map
end

#cti_table_nameObject

The name of the most recently joined table.



347
348
349
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 347

def cti_table_name
  cti_tables ? cti_tables.last : dataset.first_source_alias
end

#freezeObject

Freeze CTI information when freezing model class.



263
264
265
266
267
268
269
270
271
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 263

def freeze
  @cti_models.freeze
  @cti_tables.freeze
  @cti_instance_dataset.freeze
  @cti_table_columns.freeze
  @cti_table_map.freeze

  super
end

#inherited(subclass) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 275

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 = 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_eval 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]
    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"
      Sequel::Deprecation.deprecate("Using class_table_inheritance with duplicate column names (#{n} => #{dup_cols}) in subclass tables (other than the primary key column)', 'Make sure all tables used have unique column names, or implement support for handling duplicate column names in the class_table_inheritance 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)

    if @cti_alias # SEQUEL5: remove if
      ds = ds.from_self(:alias=>@cti_alias)
    end

    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||table)} # SEQUEL5: remove ||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

The model class for the given key value.



352
353
354
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 352

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.



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 334

def table_name
  if cti_tables
    if @cti_alias # SEQUEL5: remove if
      @cti_alias
    else
      cti_tables.last
    end
  else
    super
  end
end