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