Module: Mongoid::Relations::Accessors

Extended by:
ActiveSupport::Concern
Included in:
Mongoid::Relations
Defined in:
lib/mongoid/relations/accessors.rb

Overview

This module contains all the behaviour related to accessing relations through the getters and setters, and how to delegate to builders to create new ones.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#build(name, object, metadata, options = {}) ⇒ Proxy

Builds the related document and creates the relation unless the document is nil, then sets the relation on this document.

Examples:

Build the relation.

person.build(:addresses, { :id => 1 }, )

Parameters:

  • name (String, Symbol)

    The name of the relation.

  • object (Hash, BSON::ObjectId)

    The id or attributes to use.

  • metadata (Metadata)

    The relation’s metadata.

  • building (true, false)

    If we are in a build operation.

Returns:

  • (Proxy)

    The relation.

Since:

  • 2.0.0.rc.1



25
26
27
28
29
30
# File 'lib/mongoid/relations/accessors.rb', line 25

def build(name, object, , options = {})
  relation = create_relation(object, )
  set(name, relation).tap do |relation|
    relation.load!(options) if relation && options[:eager]
  end
end

#create_relation(object, metadata) ⇒ Proxy

Create a relation from an object and metadata.

Examples:

Create the relation.

person.create_relation(document, )

Parameters:

  • Document, (Document, Array<Document ] object The relation target.)

    Array<Document ] object The relation target.

  • metadata (Metadata)

    The relation metadata.

Returns:

  • (Proxy)

    The relation.

Since:

  • 2.0.0.rc.1



57
58
59
60
61
# File 'lib/mongoid/relations/accessors.rb', line 57

def create_relation(object, )
  type = @attributes[.inverse_type]
  target = .builder(object).build(type)
  target ? .relation.new(self, target, ) : nil
end

#options(args) ⇒ Hash

Return the options passed to the builders.

Examples:

Get the options.

person.configurables(document, :continue => true)

Parameters:

  • args (Array)

    The arguments to check.

Returns:

  • (Hash)

    The options.

Since:

  • 2.0.0.rc.1



42
43
44
# File 'lib/mongoid/relations/accessors.rb', line 42

def options(args)
  Mongoid.binding_defaults.merge(args.extract_options!)
end

#relation_exists?(name) ⇒ true, false

Determines if the relation exists or not.

Examples:

Does the relation exist?

person.relation_exists?(:people)

Parameters:

  • name (String)

    The name of the relation to check.

Returns:

  • (true, false)

    True if set and not nil, false if not.

Since:

  • 2.0.0.rc.1



73
74
75
# File 'lib/mongoid/relations/accessors.rb', line 73

def relation_exists?(name)
  ivar(name)
end

#set(name, relation) ⇒ Proxy

Set the supplied relation to an instance variable on the class with the provided name. Used as a helper just for code cleanliness.

Examples:

Set the proxy on the document.

person.set(:addresses, addresses)

Parameters:

  • name (String, Symbol)

    The name of the relation.

  • relation (Proxy)

    The relation to set.

Returns:

  • (Proxy)

    The relation.

Since:

  • 2.0.0.rc.1



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

def set(name, relation)
  instance_variable_set("@#{name}", relation)
end

#substitute(name, object, options) ⇒ Object

Replace an existing relation with a new one.

Examples:

Replace the relation.

document.substitute("addresses", Address.new)

Parameters:

  • name (String)

    The name of the relation.

  • object (Document)

    The document to replace with.

Since:

  • 2.0.0



103
104
105
# File 'lib/mongoid/relations/accessors.rb', line 103

def substitute(name, object, options)
  set(name, ivar(name).substitute(object, options))
end