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 uses an 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 used if the implicit naming is incorrect. |
cti_tables | [R] | 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. |
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.
# File lib/sequel/plugins/class_table_inheritance.rb, line 205 205: def cti_base_model 206: @cti_models.first 207: end
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.
# File lib/sequel/plugins/class_table_inheritance.rb, line 230 230: def cti_columns 231: h = {} 232: cti_models.each { |m| h[m.table_name] = m.cti_table_columns } 233: h 234: end
Alias to sti_key, for backwards compatibility.
# File lib/sequel/plugins/class_table_inheritance.rb, line 237 237: def cti_key; sti_key; end
Alias to sti_model_map, for backwards compatibility.
# File lib/sequel/plugins/class_table_inheritance.rb, line 240 240: def cti_model_map; sti_model_map; end
# File lib/sequel/plugins/class_table_inheritance.rb, line 244 244: def inherited(subclass) 245: ds = sti_dataset 246: 247: # Prevent inherited in model/base.rb from setting the dataset 248: subclass.instance_eval { @dataset = nil } 249: 250: super 251: 252: # Set table if this is a class table inheritance 253: table = nil 254: columns = nil 255: if (n = subclass.name) && !n.empty? 256: if table = cti_table_map[n.to_sym] 257: columns = db.from(table).columns 258: else 259: table = subclass.implicit_table_name 260: columns = check_non_connection_error{db.from(table).columns} 261: table = nil if !columns || columns.empty? 262: end 263: end 264: table = nil if table && (table == table_name) 265: 266: return unless table 267: 268: pk = primary_key 269: subclass.instance_eval do 270: if cti_tables.length == 1 271: ds = ds.select(*self.columns.map{|cc| Sequel.qualify(table_name, Sequel.identifier(cc))}) 272: end 273: sel_app = (columns - [pk]).map{|cc| Sequel.qualify(table, Sequel.identifier(cc))} 274: @sti_dataset = ds.join(table, pk=>pk).select_append(*sel_app) 275: set_dataset(@sti_dataset) 276: set_columns(self.columns) 277: dataset.row_proc = lambda{|r| subclass.sti_load(r)} 278: (columns - [pk]).each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>table)} 279: 280: @cti_models += [self] 281: @cti_tables += [table] 282: @cti_table_columns = columns 283: @cti_instance_dataset = db.from(table) 284: 285: cti_tables.reverse_each do |ct| 286: db.schema(ct).each{|sk,v| db_schema[sk] = v} 287: end 288: end 289: end
# File lib/sequel/plugins/class_table_inheritance.rb, line 296 296: def sti_class_from_key(key) 297: sti_class(sti_model_map[key]) 298: end