| Module | Sequel::Plugins::ClassTableInheritance::ClassMethods |
| In: |
lib/sequel/plugins/class_table_inheritance.rb
|
| cti_ignore_subclass_columns | [R] | An array of columns that may be duplicated in sub-classes. The primary key column is always allowed to be duplicated |
| 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 is 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 should be used if the implicit naming is incorrect. |
| cti_tables | [R] | An array of table symbols that back this model. The first is table symbol for the base model, and the last is the current model table symbol. |
The name of the most recently joined table.
# File lib/sequel/plugins/class_table_inheritance.rb, line 320
320: def cti_table_name
321: cti_tables ? cti_tables.last : dataset.first_source_alias
322: end
Freeze CTI information when freezing model class.
# File lib/sequel/plugins/class_table_inheritance.rb, line 243
243: def freeze
244: @cti_models.freeze
245: @cti_tables.freeze
246: @cti_table_columns.freeze
247: @cti_table_map.freeze
248: @cti_ignore_subclass_columns.freeze
249:
250: super
251: end
# File lib/sequel/plugins/class_table_inheritance.rb, line 255
255: def inherited(subclass)
256: ds = sti_dataset
257:
258: # Prevent inherited in model/base.rb from setting the dataset
259: subclass.instance_exec { @dataset = nil }
260:
261: super
262:
263: # Set table if this is a class table inheritance
264: table = nil
265: columns = nil
266: if (n = subclass.name) && !n.empty?
267: if table = cti_table_map[n.to_sym]
268: columns = db.from(table).columns
269: else
270: table = subclass.implicit_table_name
271: columns = check_non_connection_error(false){db.from(table).columns}
272: table = nil if !columns || columns.empty?
273: end
274: end
275: table = nil if table && (table == cti_table_name)
276:
277: return unless table
278:
279: pk = primary_key
280: subclass.instance_exec do
281: if cti_tables.length == 1
282: ds = ds.select(*self.columns.map{|cc| Sequel.qualify(cti_table_name, Sequel.identifier(cc))})
283: end
284: cols = (columns - [pk]) - cti_ignore_subclass_columns
285: dup_cols = cols & ds.columns
286: unless dup_cols.empty?
287: raise Error, "class_table_inheritance with duplicate column names (other than the primary key column) is not supported, make sure tables have unique column names (duplicate columns: #{dup_cols}). If this is desired, specify these columns in the :ignore_subclass_columns option when initializing the plugin"
288: end
289: sel_app = cols.map{|cc| Sequel.qualify(table, Sequel.identifier(cc))}
290: @sti_dataset = ds = ds.join(table, pk=>pk).select_append(*sel_app)
291:
292: ds = ds.from_self(:alias=>@cti_alias)
293:
294: set_dataset(ds)
295: set_columns(self.columns)
296: @dataset = @dataset.with_row_proc(lambda{|r| subclass.sti_load(r)})
297: cols.each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>@cti_alias)}
298:
299: @cti_models += [self]
300: @cti_tables += [table]
301: @cti_table_columns = columns
302: @cti_instance_dataset = db.from(table)
303:
304: cti_tables.reverse_each do |ct|
305: db.schema(ct).each{|sk,v| db_schema[sk] = v}
306: end
307: end
308: end
The model class for the given key value.
# File lib/sequel/plugins/class_table_inheritance.rb, line 325
325: def sti_class_from_key(key)
326: sti_class(sti_model_map[key])
327: end