module Mongoid::Relations::Accessors
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.
Public Instance Methods
Builds the related document and creates the relation unless the document is nil, then sets the relation on this document.
@example Build the relation.
person.__build__(:addresses, { :id => 1 }, metadata)
@param [ String, Symbol ] name The name of the relation. @param [ Hash, Moped::BSON::ObjectId ] object The id or attributes to use. @param [ Metadata ] metadata The relation's metadata. @param [ true, false ] building If we are in a build operation.
@return [ Proxy ] The relation.
@since 2.0.0.rc.1
# File lib/mongoid/relations/accessors.rb, line 24 def __build__(name, object, metadata) relation = create_relation(object, metadata) set_relation(name, relation) end
Create a relation from an object and metadata.
@example Create the relation.
person.create_relation(document, metadata)
@param [ Document, Array<Document ] object The relation target. @param [ Metadata ] metadata The relation metadata.
@return [ Proxy ] The relation.
@since 2.0.0.rc.1
# File lib/mongoid/relations/accessors.rb, line 40 def create_relation(object, metadata) type = @attributes[metadata.inverse_type] target = metadata.builder(self, object).build(type) target ? metadata.relation.new(self, target, metadata) : nil end
Determines if the relation exists or not.
@example Does the relation exist?
person.relation_exists?(:people)
@param [ String ] name The name of the relation to check.
@return [ true, false ] True if set and not nil, false if not.
@since 2.0.0.rc.1
# File lib/mongoid/relations/accessors.rb, line 56 def relation_exists?(name) ivar(name) end
Resets the criteria inside the relation proxy. Used by many to many relations to keep the underlying ids array in sync.
@example Reset the relation criteria.
person.reset_relation_criteria(:preferences)
@param [ Symbol ] name The name of the relation.
@since 3.0.14
# File lib/mongoid/relations/accessors.rb, line 69 def reset_relation_criteria(name) if instance_variable_defined?("@#{name}") send(name).reset_unloaded end end
Set the supplied relation to an instance variable on the class with the provided name. Used as a helper just for code cleanliness.
@example Set the proxy on the document.
person.set(:addresses, addresses)
@param [ String, Symbol ] name The name of the relation. @param [ Proxy ] relation The relation to set.
@return [ Proxy ] The relation.
@since 2.0.0.rc.1
# File lib/mongoid/relations/accessors.rb, line 87 def set_relation(name, relation) instance_variable_set("@#{name}", relation) end
Private Instance Methods
Get the relation. Extracted out from the getter method to avoid infinite recursion when overriding the getter.
@api private
@example Get the relation.
document.get_relation(:name, metadata)
@param [ Symbol ] name The name of the relation. @param [ Metadata ] metadata The relation metadata. @param [ true, false ] reload If the relation is to be reloaded.
@return [ Proxy ] The relation.
@since 3.0.16
# File lib/mongoid/relations/accessors.rb, line 108 def get_relation(name, metadata, reload = false) variable = "@#{name}" value = if instance_variable_defined?(variable) && !reload instance_variable_get(variable) else _building do _loading do __build__(name, attributes[metadata.key], metadata) end end end if value.nil? && metadata.autobuilding? && !without_autobuild? send("build_#{name}") else value end end
@todo: Durran: Refactor before release, but this fixes the issue with the extra queries.
# File lib/mongoid/relations/accessors.rb, line 128 def get_relation_for_set(name, metadata, object) variable = "@#{name}" value = if instance_variable_defined?(variable) instance_variable_get(variable) else _building do _loading do if needs_no_database_query?(object, metadata) __build__(name, object, metadata) else __build__(name, attributes[metadata.key], metadata) end end end end end
# File lib/mongoid/relations/accessors.rb, line 145 def needs_no_database_query?(object, metadata) object.is_a?(Document) && !object.embedded? && object.id == attributes[metadata.key] end
Yield to the block with autobuild functionality turned off.
@example Execute without autobuild.
document.without_autobuild do document.name end
@return [ Object ] The result of the yield.
@since 3.0.0
# File lib/mongoid/relations/accessors.rb, line 172 def without_autobuild Threaded.begin_execution("without_autobuild") yield ensure Threaded.exit_execution("without_autobuild") end
Is the current code executing without autobuild functionality?
@example Is autobuild disabled?
document.without_autobuild?
@return [ true, false ] If autobuild is disabled.
@since 3.0.0
# File lib/mongoid/relations/accessors.rb, line 158 def without_autobuild? Threaded.executing?(:without_autobuild) end