Module: Bsm::Model::StiConvertable::ClassMethods
- Defined in:
- lib/bsm/model/sti_convertable.rb
Instance Method Summary collapse
- #fallback_descendant ⇒ Object
- #kind ⇒ Object
-
#new(attributes = nil, *args, &block) ⇒ Object
@Override: Allow to specify a kind.
- #real_descendants ⇒ Object
Instance Method Details
#fallback_descendant ⇒ Object
34 35 36 |
# File 'lib/bsm/model/sti_convertable.rb', line 34 def fallback_descendant self end |
#kind ⇒ Object
38 39 40 |
# File 'lib/bsm/model/sti_convertable.rb', line 38 def kind name.demodulize.underscore end |
#new(attributes = nil, *args, &block) ⇒ Object
@Override: Allow to specify a kind
20 21 22 23 24 25 26 27 28 |
# File 'lib/bsm/model/sti_convertable.rb', line 20 def new(attributes = nil, *args, &block) kind = attributes.delete(:kind) { attributes.delete('kind') } if attributes.respond_to?(:delete) 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_descendants ⇒ Object
30 31 32 |
# File 'lib/bsm/model/sti_convertable.rb', line 30 def real_descendants descendants.select(&:real_type?) end |