Module: ActiveRecordEx::PolymorphicBuild::ClassMethods

Defined in:
lib/active_record_ex/polymorphic_build.rb

Instance Method Summary collapse

Instance Method Details

#new_with_typing(attrs = {}, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/active_record_ex/polymorphic_build.rb', line 19

def new_with_typing(attrs = {}, options = {})
  if attrs[:type] && (klass = attrs[:type].constantize) < self
    klass.new(attrs, options)
  elsif attrs[:type] && !((klass = attrs[:type].constantize) <= self)
    raise ArgumentError.new("Attempting to instantiate #{klass}, which is not a subclass of #{self}")
  else
    new_without_typing(attrs, options)
  end
end