Module: ErpBaseErpSvcs::Extensions::ActiveRecord::StiInstantiation::ClassMethods

Defined in:
lib/erp_base_erp_svcs/extensions/active_record/sti_instantiation.rb

Instance Method Summary collapse

Instance Method Details

#new(*args, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/erp_base_erp_svcs/extensions/active_record/sti_instantiation.rb', line 21

def new(*args, &block)
  options = args.first.is_a?(Hash) ? args.first : {}
  type = options[:type] || options['type']
  klass = type.is_a?(Class) ? type : type.constantize if type

  if type and klass != self
    raise "STI instantiation error: class #{klass.name} should be a descendant of #{self.name}" unless klass < self
    klass.new(*args, &block)
  else
    super
  end
end