module Mongoid::Relations::Accessors::ClassMethods
Public Instance Methods
Adds the existence check for relations.
@example Add the existence check.
Person.existence_check(:name, meta)
@example Check if a relation exists.
person = Person.new person.has_game? person.game?
@param [ String, Symbol ] name The name of the relation. @param [ Metadata ] The metadata.
@return [ Class ] The model being set up.
@since 3.0.0
# File lib/mongoid/relations/accessors.rb, line 197 def existence_check(name, metadata) module_eval <<-END def #{name}? without_autobuild { !__send__(:#{name}).blank? } end alias :has_#{name}? :#{name}? END self end
Defines the getter for the relation. Nothing too special here: just return the instance variable for the relation if it exists or build the thing.
@example Set up the getter for the relation.
Person.getter("addresses", metadata)
@param [ String, Symbol ] name The name of the relation. @param [ Metadata ] metadata The metadata for the relation.
@return [ Class ] The class being set up.
@since 2.0.0.rc.1
# File lib/mongoid/relations/accessors.rb, line 220 def getter(name, metadata) re_define_method(name) do |reload = false| get_relation(name, metadata, reload) end self end
Defines the getter for the ids of documents in the relation. Should be specify only for referenced many relations.
@example Set up the ids getter for the relation.
Person.ids_getter("addresses", metadata)
@param [ String, Symbol ] name The name of the relation. @param [ Metadata] metadata The metadata for the relation.
@return [ Class ] The class being set up.
# File lib/mongoid/relations/accessors.rb, line 237 def ids_getter(name, metadata) ids_method = "#{name.to_s.singularize}_ids" re_define_method(ids_method) do send(name).only(:id).map(&:id) end self end
Defines the setter method that allows you to set documents in this relation by their ids. The defined setter, finds documents with given ids and invokes regular relation setter with found documents. Ids setters should be defined only for referenced many relations.
@example Set up the id_setter for the relation.
Person.ids_setter("addesses", metadata) @param [ String, Symbol ] name The name of the relation. @param [ Metadata ] metadata The metadata for the relation. @return [ Class ] The class being set up.
# File lib/mongoid/relations/accessors.rb, line 288 def ids_setter(name, metadata) ids_method = "#{name.to_s.singularize}_ids=" re_define_method(ids_method) do |ids| send(metadata.setter, metadata.klass.find(ids.reject(&:blank?))) end self end
Defines the setter for the relation. This does a few things based on some conditions. If there is an existing association, a target substitution will take place, otherwise a new relation will be created with the supplied target.
@example Set up the setter for the relation.
Person.setter("addresses", metadata)
@param [ String, Symbol ] name The name of the relation. @param [ Metadata ] metadata The metadata for the relation.
@return [ Class ] The class being set up.
@since 2.0.0.rc.1
# File lib/mongoid/relations/accessors.rb, line 260 def setter(name, metadata) re_define_method("#{name}=") do |object| without_autobuild do if metadata.many? set_relation(name, get_relation(name, metadata).substitute(object.substitutable)) elsif value = get_relation_for_set(name, metadata, object) set_relation(name, value.substitute(object.substitutable)) else __build__(name, object.substitutable, metadata) end end end self end