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_model_map | [R] | A hash with keys being values of the cti_key column, and values being class name strings or symbols. Used if you don‘t want to store class names in the database. |
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 160 160: def inherited(subclass) 161: cc = cti_columns 162: ck = cti_key 163: ct = cti_tables.dup 164: ctm = cti_table_map.dup 165: cbm = cti_base_model 166: cmm = cti_model_map 167: pk = primary_key 168: ds = dataset 169: table = nil 170: columns = nil 171: subclass.instance_eval do 172: raise(Error, "cannot create anonymous subclass for model class using class_table_inheritance") if !(n = name) || n.empty? 173: table = ctm[n.to_sym] || implicit_table_name 174: columns = db.from(table).columns 175: @cti_key = ck 176: @cti_tables = ct + [table] 177: @cti_columns = cc.merge(table=>columns) 178: @cti_table_map = ctm 179: @cti_base_model = cbm 180: @cti_model_map = cmm 181: # Need to set dataset and columns before calling super so that 182: # the main column accessor module is included in the class before any 183: # plugin accessor modules (such as the lazy attributes accessor module). 184: set_dataset(ds.join(table, pk=>pk).select_append(*(columns - [primary_key]).map{|c| Sequel.qualify(table, Sequel.identifier(c))})) 185: set_columns(self.columns) 186: end 187: super 188: subclass.instance_eval do 189: set_dataset_cti_row_proc 190: (columns - [cbm.primary_key]).each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>table)} 191: cti_tables.reverse.each do |t| 192: db.schema(t).each{|k,v| db_schema[k] = v} 193: end 194: end 195: 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 199 199: def primary_key 200: return super if self == cti_base_model 201: cti_base_model.primary_key 202: end