module Sequel::Plugins::ClassTableInheritance::ClassMethods
Attributes
An array of columns that may be duplicated in sub-classes. The primary key column is always allowed to be duplicated
The dataset that table instance datasets are based on. Used for database modifications
An array of each model in the inheritance hierarchy that is backed by a new table.
A boolean indicating whether or not to automatically qualify tables backing subclasses with the same qualifier as their superclass, if the superclass is qualified. Specified with the :qualify_tables option to the plugin and only applied to automatically determined table names (not to the :table_map option).
An array of column symbols for the backing database table, giving the columns to update in each backing database table.
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.
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
The name of the most recently joined table.
# File lib/sequel/plugins/class_table_inheritance.rb 343 def cti_table_name 344 cti_tables ? cti_tables.last : dataset.first_source_alias 345 end
Freeze CTI information when freezing model class.
# File lib/sequel/plugins/class_table_inheritance.rb 259 def freeze 260 @cti_models.freeze 261 @cti_tables.freeze 262 @cti_table_columns.freeze 263 @cti_table_map.freeze 264 @cti_ignore_subclass_columns.freeze 265 266 super 267 end
# File lib/sequel/plugins/class_table_inheritance.rb 271 def inherited(subclass) 272 ds = sti_dataset 273 274 # Prevent inherited in model/base.rb from setting the dataset 275 subclass.instance_exec { @dataset = nil } 276 277 super 278 279 # Set table if this is a class table inheritance 280 table = nil 281 columns = nil 282 if (n = subclass.name) && !n.empty? 283 if table = cti_table_map[n.to_sym] 284 columns = db.schema(table).map(&:first) 285 else 286 table = if cti_qualify_tables && (schema = dataset.schema_and_table(cti_table_name).first) 287 SQL::QualifiedIdentifier.new(schema, subclass.implicit_table_name) 288 else 289 subclass.implicit_table_name 290 end 291 columns = check_non_connection_error(false){db.schema(table) && db.schema(table).map(&:first)} 292 table = nil if !columns || columns.empty? 293 end 294 end 295 table = nil if table && (table == cti_table_name) 296 297 return unless table 298 299 pk = primary_key 300 subclass.instance_exec do 301 if cti_tables.length == 1 302 ds = ds.select(*self.columns.map{|cc| Sequel.qualify(cti_table_name, Sequel.identifier(cc))}) 303 end 304 ds.send(:columns=, self.columns) 305 cols = (columns - [pk]) - cti_ignore_subclass_columns 306 dup_cols = cols & ds.columns 307 unless dup_cols.empty? 308 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" 309 end 310 sel_app = cols.map{|cc| Sequel.qualify(table, Sequel.identifier(cc))} 311 @sti_dataset = ds = ds.join(table, pk=>pk).select_append(*sel_app) 312 313 ds = ds.from_self(:alias=>@cti_alias) 314 ds.send(:columns=, self.columns + cols) 315 316 set_dataset(ds) 317 set_columns(self.columns) 318 @dataset = @dataset.with_row_proc(lambda{|r| subclass.sti_load(r)}) 319 cols.each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>@cti_alias)} 320 321 @cti_models += [self] 322 @cti_tables += [table] 323 @cti_table_columns = columns 324 @cti_instance_dataset = db.from(table) 325 326 cti_tables.reverse_each do |ct| 327 db.schema(ct).each{|sk,v| db_schema[sk] = v} 328 end 329 setup_auto_validations if respond_to?(:setup_auto_validations, true) 330 end 331 end
The model class for the given key value.
# File lib/sequel/plugins/class_table_inheritance.rb 348 def sti_class_from_key(key) 349 sti_class(sti_model_map[key]) 350 end
The table name for the current model class's main table.
# File lib/sequel/plugins/class_table_inheritance.rb 334 def table_name 335 if cti_tables && cti_tables.length > 1 336 @cti_alias 337 else 338 super 339 end 340 end
Private Instance Methods
If using a subquery for class table inheritance, also use a subquery when setting subclass dataset.
# File lib/sequel/plugins/class_table_inheritance.rb 356 def sti_subclass_dataset(key) 357 ds = super 358 if cti_models[0] != self 359 ds = ds.from_self(:alias=>@cti_alias) 360 end 361 ds 362 end