Module: Sequel::Plugins::ClassTableInheritance::ClassMethods
- Defined in:
- lib/sequel/plugins/class_table_inheritance.rb
Instance Attribute Summary collapse
-
#cti_ignore_subclass_columns ⇒ Object
readonly
An array of columns that may be duplicated in sub-classes.
-
#cti_instance_dataset ⇒ Object
readonly
The dataset that table instance datasets are based on.
-
#cti_models ⇒ Object
readonly
An array of each model in the inheritance hierarchy that is backed by a new table.
-
#cti_qualify_tables ⇒ Object
readonly
A boolean indicating whether or not to automatically qualify tables backing subclasses with the same qualifier as their superclass, if the superclass is qualified.
-
#cti_table_columns ⇒ Object
readonly
An array of column symbols for the backing database table, giving the columns to update in each backing database table.
-
#cti_table_map ⇒ Object
readonly
A hash with class name symbol keys and table name symbol values.
-
#cti_tables ⇒ Object
readonly
An array of table symbols that back this model.
Instance Method Summary collapse
-
#cti_table_name ⇒ Object
The name of the most recently joined table.
-
#freeze ⇒ Object
Freeze CTI information when freezing model class.
- #inherited(subclass) ⇒ Object
-
#sti_class_from_key(key) ⇒ Object
The model class for the given key value.
-
#table_name ⇒ Object
The table name for the current model class’s main table.
Instance Attribute Details
#cti_ignore_subclass_columns ⇒ Object (readonly)
An array of columns that may be duplicated in sub-classes. The primary key column is always allowed to be duplicated
249 250 251 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 249 def cti_ignore_subclass_columns @cti_ignore_subclass_columns end |
#cti_instance_dataset ⇒ Object (readonly)
The dataset that table instance datasets are based on. Used for database modifications
235 236 237 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 235 def cti_instance_dataset @cti_instance_dataset end |
#cti_models ⇒ Object (readonly)
An array of each model in the inheritance hierarchy that is backed by a new table.
227 228 229 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 227 def cti_models @cti_models end |
#cti_qualify_tables ⇒ Object (readonly)
A boolean indicating whether or not to automatically qualify tables backing subclasses with the same qualifier as their superclass, if the superclass is qualified. Specified with the :qualify_tables option to the plugin and only applied to automatically determined table names (not to the :table_map option).
256 257 258 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 256 def cti_qualify_tables @cti_qualify_tables end |
#cti_table_columns ⇒ Object (readonly)
An array of column symbols for the backing database table, giving the columns to update in each backing database table.
231 232 233 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 231 def cti_table_columns @cti_table_columns end |
#cti_table_map ⇒ Object (readonly)
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.
245 246 247 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 245 def cti_table_map @cti_table_map end |
#cti_tables ⇒ Object (readonly)
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.
240 241 242 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 240 def cti_tables @cti_tables end |
Instance Method Details
#cti_table_name ⇒ Object
The name of the most recently joined table.
343 344 345 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 343 def cti_table_name cti_tables ? cti_tables.last : dataset.first_source_alias end |
#freeze ⇒ Object
Freeze CTI information when freezing model class.
259 260 261 262 263 264 265 266 267 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 259 def freeze @cti_models.freeze @cti_tables.freeze @cti_table_columns.freeze @cti_table_map.freeze @cti_ignore_subclass_columns.freeze super end |
#inherited(subclass) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 271 def inherited(subclass) ds = sti_dataset # Prevent inherited in model/base.rb from setting the dataset subclass.instance_exec { @dataset = nil } super # Set table if this is a class table inheritance table = nil columns = nil if (n = subclass.name) && !n.empty? if table = cti_table_map[n.to_sym] columns = db.schema(table).map(&:first) else table = if cti_qualify_tables && (schema = dataset.schema_and_table(cti_table_name).first) SQL::QualifiedIdentifier.new(schema, subclass.implicit_table_name) else subclass.implicit_table_name end columns = check_non_connection_error(false){db.schema(table) && db.schema(table).map(&:first)} table = nil if !columns || columns.empty? end end table = nil if table && (table == cti_table_name) return unless table pk = primary_key subclass.instance_exec do if cti_tables.length == 1 ds = ds.select(*self.columns.map{|cc| Sequel.qualify(cti_table_name, Sequel.identifier(cc))}) end ds.send(:columns=, self.columns) cols = (columns - [pk]) - cti_ignore_subclass_columns dup_cols = cols & ds.columns unless dup_cols.empty? 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" end sel_app = cols.map{|cc| Sequel.qualify(table, Sequel.identifier(cc))} @sti_dataset = ds = ds.join(table, pk=>pk).select_append(*sel_app) ds = ds.from_self(:alias=>@cti_alias) ds.send(:columns=, self.columns + cols) set_dataset(ds) set_columns(self.columns) @dataset = @dataset.with_row_proc(lambda{|r| subclass.sti_load(r)}) cols.each{|a| define_lazy_attribute_getter(a, :dataset=>dataset, :table=>@cti_alias)} @cti_models += [self] @cti_tables += [table] @cti_table_columns = columns @cti_instance_dataset = db.from(table) cti_tables.reverse_each do |ct| db.schema(ct).each{|sk,v| db_schema[sk] = v} end setup_auto_validations if respond_to?(:setup_auto_validations, true) end end |
#sti_class_from_key(key) ⇒ Object
The model class for the given key value.
348 349 350 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 348 def sti_class_from_key(key) sti_class(sti_model_map[key]) end |
#table_name ⇒ Object
The table name for the current model class’s main table.
334 335 336 337 338 339 340 |
# File 'lib/sequel/plugins/class_table_inheritance.rb', line 334 def table_name if cti_tables && cti_tables.length > 1 @cti_alias else super end end |