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 128 128: def inherited(subclass) 129: cc = cti_columns 130: ck = cti_key 131: ct = cti_tables.dup 132: ctm = cti_table_map.dup 133: cbm = cti_base_model 134: pk = primary_key 135: ds = dataset 136: subclass.instance_eval do 137: raise(Error, "cannot create anonymous subclass for model class using class_table_inheritance") if !(n = name) || n.empty? 138: table = ctm[n.to_sym] || implicit_table_name 139: columns = db.from(table).columns 140: @cti_key = ck 141: @cti_tables = ct + [table] 142: @cti_columns = cc.merge(table=>columns) 143: @cti_table_map = ctm 144: @cti_base_model = cbm 145: # Need to set dataset and columns before calling super so that 146: # the main column accessor module is included in the class before any 147: # plugin accessor modules (such as the lazy attributes accessor module). 148: set_dataset(ds.join(table, [pk])) 149: set_columns(self.columns) 150: end 151: super 152: subclass.instance_eval do 153: m = method(:constantize) 154: dataset.row_proc = if cti_key 155: lambda{|r| (m.call(r[ck]) rescue subclass).load(r)} 156: else 157: lambda{|r| subclass.load(r)} 158: end 159: (columns - [cbm.primary_key]).each{|a| define_lazy_attribute_getter(a)} 160: cti_tables.reverse.each do |table| 161: db.schema(table).each{|k,v| db_schema[k] = v} 162: end 163: end 164: 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 168 168: def primary_key 169: return super if self == cti_base_model 170: cti_base_model.primary_key 171: end