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.



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

def _types
  @_type ||= (self.subclasses + [ self.name ])
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



46
47
48
49
# File 'lib/mongoid/document.rb', line 46

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

#embedded?Boolean

return true if the Document is embedded in another Documnet.

Returns:



52
53
54
# File 'lib/mongoid/document.rb', line 52

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



68
69
70
71
# File 'lib/mongoid/document.rb', line 68

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

#human_nameObject

Returns a human readable version of the class.



74
75
76
# File 'lib/mongoid/document.rb', line 74

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.



80
81
82
# File 'lib/mongoid/document.rb', line 80

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.



85
86
87
88
89
90
91
92
93
94
# File 'lib/mongoid/document.rb', line 85

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.



101
102
103
104
# File 'lib/mongoid/document.rb', line 101

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.



107
108
109
# File 'lib/mongoid/document.rb', line 107

def store_in(name)
  self.collection_name = name.to_s
end