Module | Sequel::Plugins::ClassTableInheritance::ClassMethods |
In: |
lib/sequel/plugins/class_table_inheritance.rb
|
cti_base_model | [R] | The parent/root/base model for this class table inheritance hierarchy. This is the only model in the hierarchy that load the class_table_inheritance plugin. |
cti_columns | [R] | Hash with table name symbol keys and arrays of column symbol values, giving the columns to update in each backing database table. |
cti_key | [R] | The column containing the class name as a string. Used to return instances of subclasses when calling the superclass‘s load method. |
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. |
Add the appropriate data structures to the subclass. Does not allow anonymous subclasses to be created, since they would not be mappable to a table.
# File lib/sequel/plugins/class_table_inheritance.rb, line 118 118: def inherited(subclass) 119: cc = cti_columns 120: ck = cti_key 121: ct = cti_tables.dup 122: ctm = cti_table_map.dup 123: cbm = cti_base_model 124: pk = primary_key 125: ds = dataset 126: subclass.instance_eval do 127: raise(Error, "cannot create anonymous subclass for model class using class_table_inheritance") if !(n = name) || n.empty? 128: table = ctm[n.to_sym] || implicit_table_name 129: columns = db.from(table).columns 130: @cti_key = ck 131: @cti_tables = ct + [table] 132: @cti_columns = cc.merge(table=>columns) 133: @cti_table_map = ctm 134: @cti_base_model = cbm 135: # Need to set dataset and columns before calling super so that 136: # the main column accessor module is included in the class before any 137: # plugin accessor modules (such as the lazy attributes accessor module). 138: set_dataset(ds.join(table, [pk])) 139: set_columns(self.columns) 140: end 141: super 142: subclass.instance_eval do 143: m = method(:constantize) 144: dataset.row_proc = lambda{|r| (m.call(r[ck]) rescue subclass).load(r)} 145: (columns - [cbm.primary_key]).each{|a| define_lazy_attribute_getter(a)} 146: cti_tables.reverse.each do |table| 147: db.schema(table).each{|k,v| db_schema[k] = v} 148: end 149: end 150: end
The primary key in the parent/base/root model, which should have a foreign key with the same name referencing it in each model subclass.
# File lib/sequel/plugins/class_table_inheritance.rb, line 154 154: def primary_key 155: return super if self == cti_base_model 156: cti_base_model.primary_key 157: end