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

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

Instance Method Summary collapse

Instance Method Details

#fallback_descendantObject



32
33
34
# File 'lib/bsm/model/sti_convertable.rb', line 32

def fallback_descendant
  self
end

#kindObject



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

def kind
  name.demodulize.underscore
end

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

@Override: Allow to specify a kind



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bsm/model/sti_convertable.rb', line 16

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

  klass.with_scope(respond_to?(:current_scope) ? current_scope : current_scoped_methods) do # AR 3.0 compatibility
    klass.new(attributes, *args, &block)
  end
end

#real_descendantsObject



28
29
30
# File 'lib/bsm/model/sti_convertable.rb', line 28

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