module Mti

Constants

VERSION

Public Class Methods

has_subclass(table) click to toggle source
# File lib/mti.rb, line 6
def self.has_subclass table
  mattr_accessor :mti_models unless defined? self.mti_models
  self.mti_models ||= []
  
  table_columns = table.to_s.camelize.constantize.column_names
                  .reject {|col| ["id", "#{self.to_s.underscore}_id"].include? col }
                  .compact

  self.mti_models << table

  has_one table, autosave: true, dependent: :destroy

  table_columns.each do |col|
    delegate col.to_sym, "#{col}=".to_sym, to: table, allow_nil: true
  end
end
new(attributes = nil) click to toggle source
Calls superclass method
# File lib/mti.rb, line 23
def initialize(attributes = nil)
  super
  self.mti_models.map {|model|
    mti_model_column = model.to_s.camelize.constantize.column_names 
    self.send("build_#{model.to_s}", attributes.to_h.reject {|key, val| val unless mti_model_column.include? key.to_s}) 
  } if defined? self.mti_models
end