Module: Mongoid::Traversable::DiscriminatorAssignment Private

Defined in:
build/mongoid-8.1/lib/mongoid/traversable.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Module used for prepending to the various discriminator_*= methods

Instance Method Summary collapse

Instance Method Details

#discriminator_key=(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'build/mongoid-8.1/lib/mongoid/traversable.rb', line 23

def discriminator_key=(value)
  if hereditary?
    raise Errors::InvalidDiscriminatorKeyTarget.new(self, self.superclass)
  end

  _mongoid_clear_types

  if value
    Mongoid::Fields::Validators::Macro.validate_field_name(self, value)
    value = value.to_s
    super
  else
    # When discriminator key is set to nil, replace the class's definition
    # of the discriminator key reader (provided by class_attribute earlier)
    # and re-delegate to Mongoid.
    class << self
      delegate :discriminator_key, to: ::Mongoid
    end
  end

  # This condition checks if the new discriminator key would overwrite
  # an existing field.
  # This condition also checks if the class has any descendants, because
  # if it doesn't then it doesn't need a discriminator key.
  if !fields.has_key?(self.discriminator_key) && !descendants.empty?
    default_proc = lambda { self.class.discriminator_value }
    field(self.discriminator_key, default: default_proc, type: String)
  end
end

#discriminator_value=(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
56
57
58
# File 'build/mongoid-8.1/lib/mongoid/traversable.rb', line 53

def discriminator_value=(value)
  value ||= self.name
  _mongoid_clear_types
  add_discriminator_mapping(value)
  @discriminator_value = value
end