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.
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 320 def cti_table_name 321 cti_tables ? cti_tables.last : dataset.first_source_alias 322 end
Freeze CTI information when freezing model class.
# File lib/sequel/plugins/class_table_inheritance.rb 243 def freeze 244 @cti_models.freeze 245 @cti_tables.freeze 246 @cti_table_columns.freeze 247 @cti_table_map.freeze 248 @cti_ignore_subclass_columns.freeze 249 250 super 251 end
# File lib/sequel/plugins/class_table_inheritance.rb 255 def inherited(subclass) 256 ds = sti_dataset 257 258 # Prevent inherited in model/base.rb from setting the dataset 259 subclass.instance_exec { @dataset = nil } 260 261 super 262 263 # Set table if this is a class table inheritance 264 table = nil 265 columns = nil 266 if (n = subclass.name) && !n.empty? 267 if table = cti_table_map[n.to_sym] 268 columns = db.from(table).columns 269 else 270 table = subclass.implicit_table_name 271 columns = check_non_connection_error(false){db.from(table).columns} 272 table = nil if !columns || columns.empty? 273 end 274 end 275 table = nil if table && (table == cti_table_name) 276 277 return unless table 278 279 pk = primary_key 280 subclass.instance_exec do 281 if cti_tables.length == 1 282 ds = ds.select(*self.columns.map{|cc| Sequel.qualify(cti_table_name, Sequel.identifier(cc))}) 283 end 284 cols = (columns - [pk]) - cti_ignore_subclass_columns 285 dup_cols = cols & ds.columns 286 unless dup_cols.empty? 287 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" 288 end 289 sel_app = cols.map{|cc| Sequel.qualify(table, Sequel.identifier(cc))} 290 @sti_dataset = ds = ds.join(table, pk=>pk).select_append(*sel_app) 291 292 ds = ds.from_self(:alias=>@cti_alias) 293 294 set_dataset(ds) 295 set_columns(self.columns) 296 @dataset = @dataset.with_row_proc(lambda{|r| subclass.sti_load(r)}) 297 cols.each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>@cti_alias)} 298 299 @cti_models += [self] 300 @cti_tables += [table] 301 @cti_table_columns = columns 302 @cti_instance_dataset = db.from(table) 303 304 cti_tables.reverse_each do |ct| 305 db.schema(ct).each{|sk,v| db_schema[sk] = v} 306 end 307 end 308 end
The model class for the given key value.
# File lib/sequel/plugins/class_table_inheritance.rb 325 def sti_class_from_key(key) 326 sti_class(sti_model_map[key]) 327 end
The table name for the current model class's main table.
# File lib/sequel/plugins/class_table_inheritance.rb 311 def table_name 312 if cti_tables && cti_tables.length > 1 313 @cti_alias 314 else 315 super 316 end 317 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 333 def sti_subclass_dataset(key) 334 ds = super 335 if cti_models[0] != self 336 ds = ds.from_self(:alias=>@cti_alias) 337 end 338 ds 339 end