Module: Mongoid::Document::ClassMethods

Defined in:
lib/mongoid/document.rb

Instance Method Summary collapse

Instance Method Details

#_typesObject

Returns all types to query for when using this class as the base.



122
123
124
# File 'lib/mongoid/document.rb', line 122

def _types
  @_type ||= (self.subclasses + [ self.name ])
end

#add_indexesObject

Add the default indexes to the root document if they do not already exist. Currently this is only _type.



42
43
44
45
46
47
# File 'lib/mongoid/document.rb', line 42

def add_indexes
  unless indexed
    self._collection.create_index(:_type, false)
    self.indexed = true
  end
end

#collectionObject

Returns the collection associated with this Document. If the document is embedded, there will be no collection associated with it.

Returns: Mongo::Collection



54
55
56
57
58
# File 'lib/mongoid/document.rb', line 54

def collection
  raise Errors::InvalidCollection.new(self) if embedded?
  self._collection ||= Mongoid.database.collection(self.collection_name)
  add_indexes; self._collection
end

#embedded?Boolean

return true if the Document is embedded in another Documnet.

Returns:



61
62
63
# File 'lib/mongoid/document.rb', line 61

def embedded?
  self.embedded == true
end

#field(name, options = {}) ⇒ Object

Defines all the fields that are accessable on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.

Options:

name: The name of the field, as a Symbol. options: A Hash of options to supply to the Field.

Example:

field :score, :default => 0



77
78
79
80
# File 'lib/mongoid/document.rb', line 77

def field(name, options = {})
  set_field(name, options)
  set_default(name, options)
end

#human_nameObject

Returns a human readable version of the class.



83
84
85
# File 'lib/mongoid/document.rb', line 83

def human_name
  name.underscore.humanize
end

#index(name, options = { :unique => false }) ⇒ Object

Adds an index on the field specified. Options can be :unique => true or :unique => false. It will default to the latter.



89
90
91
# File 'lib/mongoid/document.rb', line 89

def index(name, options = { :unique => false })
  collection.create_index(name, options[:unique])
end

#instantiate(attrs = {}, allocating = false) ⇒ Object

Instantiate a new object, only when loaded from the database.



94
95
96
97
98
99
100
101
102
103
# File 'lib/mongoid/document.rb', line 94

def instantiate(attrs = {}, allocating = false)
  attributes = attrs.with_indifferent_access
  if attributes[:_id] || allocating
    document = allocate
    document.instance_variable_set(:@attributes, attributes)
    return document
  else
    return new(attributes)
  end
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 and MUST be defined on documents that are embedded in order for proper updates in has_may associations.



110
111
112
113
# File 'lib/mongoid/document.rb', line 110

def key(*fields)
  self.primary_key = fields
  before_save :generate_key
end

#store_in(name) ⇒ Object

Macro for setting the collection name to store in.



116
117
118
119
# File 'lib/mongoid/document.rb', line 116

def store_in(name)
  self.collection_name = name.to_s
  self._collection = Mongoid.database.collection(name.to_s)
end