Class: Mongoid::Relations::Referenced::In

Inherits:
One
  • Object
show all
Includes:
Evolvable
Defined in:
lib/mongoid/relations/referenced/in.rb

Overview

This class handles all behaviour for relations that are either one-to-many or one-to-one, where the foreign key is store on this side of the relation and the reference is to document(s) in another collection.

Instance Attribute Summary

Attributes inherited from Proxy

#__metadata, #base, #target

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Evolvable

#__evolve_object_id__

Methods inherited from One

#__evolve_object_id__, #clear, #in_memory, #respond_to?

Methods inherited from Proxy

apply_ordering, #extend_proxies, #init, #klass, #reset_unloaded, #substitutable, #with

Methods included from Marshalable

#marshal_dump, #marshal_load

Constructor Details

- (In) initialize(base, target, metadata)

Instantiate a new referenced_in relation.

Examples:

Create the new relation.

Referenced::In.new(game, person, )


22
23
24
25
26
27
# File 'lib/mongoid/relations/referenced/in.rb', line 22

def initialize(base, target, )
  init(base, target, ) do
    characterize_one(target)
    bind_one
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Relations::Proxy

Class Method Details

+ (Builder) builder(base, meta, object)

Return the builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the builder.

Referenced::In.builder(meta, object)

Since:

  • 2.0.0.rc.1



122
123
124
# File 'lib/mongoid/relations/referenced/in.rb', line 122

def builder(base, meta, object)
  Builders::Referenced::In.new(base, meta, object)
end

+ (Criteria) criteria(metadata, object, type = nil)

Get the standard criteria used for querying this relation.

Examples:

Get the criteria.

Proxy.criteria(meta, id, Model)

Since:

  • 2.1.0



138
139
140
# File 'lib/mongoid/relations/referenced/in.rb', line 138

def criteria(, object, type = nil)
  type.where(.primary_key => object)
end

+ (Object) eager_load_klass



142
143
144
# File 'lib/mongoid/relations/referenced/in.rb', line 142

def eager_load_klass
  Relations::Eager::BelongsTo
end

+ (false) embedded?

Returns true if the relation is an embedded one. In this case always false.

Examples:

Is this relation embedded?

Referenced::In.embedded?

Since:

  • 2.0.0.rc.1



155
156
157
# File 'lib/mongoid/relations/referenced/in.rb', line 155

def embedded?
  false
end

+ (String) foreign_key(name)

Get the foreign key for the provided name.

Examples:

Get the foreign key.

Referenced::In.foreign_key(:person)

Since:

  • 3.0.0



169
170
171
# File 'lib/mongoid/relations/referenced/in.rb', line 169

def foreign_key(name)
  "#{name}#{foreign_key_suffix}"
end

+ (nil) foreign_key_default

Get the default value for the foreign key.

Examples:

Get the default.

Referenced::In.foreign_key_default

Since:

  • 2.0.0.rc.1



181
182
183
# File 'lib/mongoid/relations/referenced/in.rb', line 181

def foreign_key_default
  nil
end

+ (String) foreign_key_suffix

Returns the suffix of the foreign key field, either “_id” or “_ids”.

Examples:

Get the suffix for the foreign key.

Referenced::In.foreign_key_suffix

Since:

  • 2.0.0.rc.1



193
194
195
# File 'lib/mongoid/relations/referenced/in.rb', line 193

def foreign_key_suffix
  "_id"
end

+ (Symbol) macro

Returns the macro for this relation. Used mostly as a helper in reflection.

Examples:

Get the macro.

Referenced::In.macro


204
205
206
# File 'lib/mongoid/relations/referenced/in.rb', line 204

def macro
  :belongs_to
end

+ (NestedBuilder) nested_builder(metadata, attributes, options)

Return the nested builder that is responsible for generating the documents that will be used by this relation.

Examples:

Get the nested builder.

Referenced::In.builder(attributes, options)

Options Hash (options):

  • :allow_destroy (true, false)

    Can documents be deleted?

  • :limit (Integer)

    Max number of documents to create at once.

  • :reject_if (Proc, Symbol)

    If documents match this option then they are ignored.

  • :update_only (true, false)

    Only existing documents can be modified.

Since:

  • 2.0.0.rc.1



230
231
232
# File 'lib/mongoid/relations/referenced/in.rb', line 230

def nested_builder(, attributes, options)
  Builders::NestedAttributes::One.new(, attributes, options)
end

+ (Root) path(document)

Get the path calculator for the supplied document.

Examples:

Get the path calculator.

Proxy.path(document)

Since:

  • 2.1.0



244
245
246
# File 'lib/mongoid/relations/referenced/in.rb', line 244

def path(document)
  Mongoid::Atomic::Paths::Root.new(document)
end

+ (true) stores_foreign_key?

Tells the caller if this relation is one that stores the foreign key on its own objects.

Examples:

Does this relation store a foreign key?

Referenced::In.stores_foreign_key?

Since:

  • 2.0.0.rc.1



257
258
259
# File 'lib/mongoid/relations/referenced/in.rb', line 257

def stores_foreign_key?
  true
end

+ (Array<Symbol>) valid_options

Get the valid options allowed with this relation.

Examples:

Get the valid options.

Relation.valid_options

Since:

  • 2.1.0



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/mongoid/relations/referenced/in.rb', line 269

def valid_options
  [
    :autobuild,
    :autosave,
    :dependent,
    :foreign_key,
    :index,
    :polymorphic,
    :primary_key,
    :touch
  ]
end

+ (true, false) validation_default

Get the default validation setting for the relation. Determines if by default a validates associated will occur.

Examples:

Get the validation default.

Proxy.validation_default

Since:

  • 2.1.9



291
292
293
# File 'lib/mongoid/relations/referenced/in.rb', line 291

def validation_default
  false
end

Instance Method Details

- (Object) nullify

Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.

Examples:

Nullify the relation.

person.game.nullify


36
37
38
39
# File 'lib/mongoid/relations/referenced/in.rb', line 36

def nullify
  unbind_one
  target.save
end

- (In?) substitute(replacement)

Substitutes the supplied target documents for the existing document in the relation.

Examples:

Substitute the relation.

name.substitute(new_name)

Since:

  • 2.0.0.rc.1



53
54
55
56
57
58
59
# File 'lib/mongoid/relations/referenced/in.rb', line 53

def substitute(replacement)
  unbind_one
  return nil unless replacement
  self.target = normalize(replacement)
  bind_one
  self
end