Module: Mongoid::Keys::ClassMethods

Defined in:
lib/mongoid/keys.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#identity(options = {}) ⇒ Object

Used for telling Mongoid on a per model basis whether to override the default BSON::ObjectId and use a different type. This will be expanded in the future for requiring a PkFactory if the type is not a BSON::ObjectId or String.

Examples:

Change the documents key type.

class Person
  include Mongoid::Document
  identity :type => String
end

Parameters:

  • options (Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :type (Class)

    The type of the id.

Since:

  • 2.0.0.beta.1



78
79
80
81
# File 'lib/mongoid/keys.rb', line 78

def identity(options = {})
  fields["_id"].type = options[:type]
  @object_ids = id_is_object
end

#key(*fields) ⇒ Object

Defines the field that will be used for the id of this Document. This set the id of this Document before save to a parameterized version of the field that was supplied. This is good for use for readable URLS in web applications.

Examples:

Create a composite id.

class Person
  include Mongoid::Document
  key :first_name, :last_name
end

Parameters:

  • The (Array<Symbol>)

    fields the key is composed of.

Since:

  • 1.0.0



97
98
99
100
101
# File 'lib/mongoid/keys.rb', line 97

def key(*fields)
  self.primary_key = fields
  identity(:type => String)
  set_callback(:save, :around, :set_composite_key)
end

#using_object_ids?true, false

Convenience method for determining if we are using BSON::ObjectIds as our id.

Examples:

Does this class use object ids?

person.using_object_ids?

Returns:

  • (true, false)

    If the class uses BSON::ObjectIds for the id.

Since:

  • 1.0.0



112
113
114
# File 'lib/mongoid/keys.rb', line 112

def using_object_ids?
  @object_ids ||= id_is_object
end