Module: Foreman::STI::ClassMethods

Defined in:
app/models/concerns/foreman/sti.rb

Instance Method Summary collapse

Instance Method Details

#new(*attributes, &block) ⇒ Object

ensures that the correct STI object is created when :type is passed.


11
12
13
14
15
16
17
18
19
20
# File 'app/models/concerns/foreman/sti.rb', line 11

def new(*attributes, &block)
  if (h = attributes.first).is_a?(Hash) && (type = h.with_indifferent_access.delete(:type)) && !type.empty?
    if (klass = type.constantize) != self
      raise "Invalid type #{type}" unless klass <= self
      return klass.new(*attributes, &block)
    end
  end

  super
end