Module | Sequel::Plugins::ClassTableInheritance::ClassMethods |
In: |
lib/sequel/plugins/class_table_inheritance.rb
|
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. |
The name of the most recently joined table.
# File lib/sequel/plugins/class_table_inheritance.rb, line 312 312: def cti_table_name 313: cti_tables ? cti_tables.last : dataset.first_source_alias 314: end
Freeze CTI information when freezing model class.
# File lib/sequel/plugins/class_table_inheritance.rb, line 236 236: def freeze 237: @cti_models.freeze 238: @cti_tables.freeze 239: @cti_table_columns.freeze 240: @cti_table_map.freeze 241: 242: super 243: end
# File lib/sequel/plugins/class_table_inheritance.rb, line 247 247: def inherited(subclass) 248: ds = sti_dataset 249: 250: # Prevent inherited in model/base.rb from setting the dataset 251: subclass.instance_exec { @dataset = nil } 252: 253: super 254: 255: # Set table if this is a class table inheritance 256: table = nil 257: columns = nil 258: if (n = subclass.name) && !n.empty? 259: if table = cti_table_map[n.to_sym] 260: columns = db.from(table).columns 261: else 262: table = subclass.implicit_table_name 263: columns = check_non_connection_error(false){db.from(table).columns} 264: table = nil if !columns || columns.empty? 265: end 266: end 267: table = nil if table && (table == cti_table_name) 268: 269: return unless table 270: 271: pk = primary_key 272: subclass.instance_exec do 273: if cti_tables.length == 1 274: ds = ds.select(*self.columns.map{|cc| Sequel.qualify(cti_table_name, Sequel.identifier(cc))}) 275: end 276: cols = columns - [pk] 277: dup_cols = cols & ds.columns 278: unless dup_cols.empty? 279: 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" 280: end 281: sel_app = cols.map{|cc| Sequel.qualify(table, Sequel.identifier(cc))} 282: @sti_dataset = ds = ds.join(table, pk=>pk).select_append(*sel_app) 283: 284: ds = ds.from_self(:alias=>@cti_alias) 285: 286: set_dataset(ds) 287: set_columns(self.columns) 288: @dataset = @dataset.with_row_proc(lambda{|r| subclass.sti_load(r)}) 289: cols.each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>@cti_alias)} 290: 291: @cti_models += [self] 292: @cti_tables += [table] 293: @cti_table_columns = columns 294: @cti_instance_dataset = db.from(table) 295: 296: cti_tables.reverse_each do |ct| 297: db.schema(ct).each{|sk,v| db_schema[sk] = v} 298: end 299: end 300: end
The model class for the given key value.
# File lib/sequel/plugins/class_table_inheritance.rb, line 317 317: def sti_class_from_key(key) 318: sti_class(sti_model_map[key]) 319: end