Module: Bsm::Model::StiConvertable::ClassMethods

Defined in:
lib/bsm/model/sti_convertable.rb

Instance Method Summary collapse

Instance Method Details

#fallback_descendantObject



39
40
41
# File 'lib/bsm/model/sti_convertable.rb', line 39

def fallback_descendant
  self
end

#kindObject



43
44
45
# File 'lib/bsm/model/sti_convertable.rb', line 43

def kind
  name.demodulize.underscore
end

#new(attributes = nil, *args, &block) ⇒ Object

@Override: Allow to specify a kind



25
26
27
28
29
30
31
32
33
# File 'lib/bsm/model/sti_convertable.rb', line 25

def new(attributes = nil, *args, &block)
  kind  = (attributes = attributes.with_indifferent_access).delete(:kind) if attributes.is_a?(Hash)
  return super if real_type?

  klass = real_descendants.find {|k| k.kind == kind } || fallback_descendant
  return super if klass == self

  Bsm::Model::StiConvertable.scoping(klass, current_scope) { klass.new(attributes, *args, &block) }
end

#real_descendantsObject



35
36
37
# File 'lib/bsm/model/sti_convertable.rb', line 35

def real_descendants
  descendants.select(&:real_type?)
end