Class: DataMapper::Types::Discriminator

Inherits:
DataMapper::Type show all
Defined in:
lib/gems/dm-core-0.9.9/lib/dm-core/types/discriminator.rb

Constant Summary

Constants inherited from DataMapper::Type

DataMapper::Type::PROPERTY_OPTIONS, DataMapper::Type::PROPERTY_OPTION_ALIASES

Class Method Summary collapse

Methods inherited from DataMapper::Type

configure, dump, inherited, load, options, primitive

Class Method Details

.bind(property) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/types/discriminator.rb', line 9

def self.bind(property)
  model = property.model

  model.class_eval <<-EOS, __FILE__, __LINE__
    def self.descendants
      (@descendants ||= []).uniq!
      @descendants
    end

    after_class_method :inherited, :add_scope_for_discriminator

    def self.add_scope_for_discriminator(retval, target)
      target.descendants << target
      target.default_scope.update(#{property.name.inspect} => target.descendants)
      propagate_descendants(target)
    end

    def self.propagate_descendants(target)
      descendants << target
      superclass.propagate_descendants(target) if superclass.respond_to?(:propagate_descendants)
    end
  EOS
end