Sha256: dc3e169123547aef086d1ad2d3dbc2dce25af5698a2da57546ce4c054588b39e

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Fields #:nodoc:
    module Serializable #:nodoc:
      module ForeignKeys #:nodoc:

        # Defines the behaviour for integer foreign key fields.
        class Object
          include Serializable

          # Is the field a BSON::ObjectId?
          #
          # @example Is the field a BSON::ObjectId?
          #   field.object_id_field?
          #
          # @return [ true, false ] If the field is a BSON::ObjectId.
          #
          # @since 2.2.0
          def object_id_field?
            @object_id_field ||=
              metadata.polymorphic? ? true : metadata.klass.using_object_ids?
          end

          # Serialize the object from the type defined in the model to a MongoDB
          # compatible object to store.
          #
          # @example Serialize the field.
          #   field.serialize(object)
          #
          # @param [ Object ] object The object to cast.
          #
          # @return [ Array ] The converted object.
          #
          # @since 2.1.0
          def serialize(object)
            return nil if object.blank?
            if object_id_field?
              constraint.convert(object)
            else
              metadata.klass.fields["_id"].serialize(object)
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mongoid-multi-db-3.0.0 lib/mongoid/fields/serializable/foreign_keys/object.rb
mongoid-2.3.5 lib/mongoid/fields/serializable/foreign_keys/object.rb
mongoid-2.3.4 lib/mongoid/fields/serializable/foreign_keys/object.rb