123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Traversable::DiscriminatorAssignment Private

Do not use. This module is for internal use only.
Relationships & Source Files
Defined in: lib/mongoid/traversable.rb

Overview

Module used for prepending to the various discriminator_*= methods

Instance Attribute Summary

Instance Attribute Details

#discriminator_key=(value) (writeonly)

Sets the discriminator key.

Parameters:

  • value (String)

    The discriminator key to set.

Raises:

[ GitHub ]

  
# File 'lib/mongoid/traversable.rb', line 123

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

  _mongoid_clear_types

  if value
    Mongoid::Fields::Validators::Macro.validate_field_name(self, value)
    value = value.to_s
    ::Mongoid::Traversable.__redefine(self, 'discriminator_key', value)
  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.
  return if fields.key?(discriminator_key) || descendants.empty?

  default_proc = -> { self.class.discriminator_value }
  field(discriminator_key, default: default_proc, type: String)
end

#discriminator_value=(value) ⇒ String (writeonly)

Returns the discriminator key.

Returns:

  • (String)

    The discriminator key.

[ GitHub ]

  
# File 'lib/mongoid/traversable.rb', line 157

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