Module: Mongoid::Relations::Accessors::ClassMethods

Defined in:
lib/mongoid/relations/accessors.rb

Instance Method Summary collapse

Instance Method Details

#existence_check(name, metadata) ⇒ Class

Adds the existence check for relations.

Examples:

Add the existence check.

Person.existence_check(:name, meta)

Check if a relation exists.

person = Person.new
person.has_game?
person.game?

Parameters:

  • name (String, Symbol)

    The name of the relation.

  • The (Metadata)

    metadata.

Returns:

  • (Class)

    The model being set up.

Since:

  • 3.0.0



126
127
128
129
130
131
132
133
134
# File 'lib/mongoid/relations/accessors.rb', line 126

def existence_check(name, )
  module_eval <<-END
    def #{name}?
      without_autobuild { !#{name}.blank? }
    end
    alias :has_#{name}? :#{name}?
  END
  self
end

#getter(name, metadata) ⇒ Class

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.

Examples:

Set up the getter for the relation.

Person.getter("addresses", )

Parameters:

  • name (String, Symbol)

    The name of the relation.

  • metadata (Metadata)

    The metadata for the relation.

Returns:

  • (Class)

    The class being set up.

Since:

  • 2.0.0.rc.1



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/mongoid/relations/accessors.rb', line 149

def getter(name, )
  re_define_method(name) do |*args|
    reload, variable = args.first, "@#{name}"
    value = if instance_variable_defined?(variable) && !reload
      instance_variable_get(variable)
    else
      _building do
        _loading { __build__(name, attributes[.key], ) }
      end
    end
    if value.nil? && .autobuilding? && !without_autobuild?
      send("build_#{name}")
    else
      value
    end
  end
  self
end

#ids_getter(name, metadata) ⇒ Class

Defines the getter for the ids of documents in the relation. Should be specify only for referenced many relations.

Examples:

Set up the ids getter for the relation.

Person.ids_getter("addresses", )

Parameters:

  • name (String, Symbol)

    The name of the relation.

  • metadata (Metadata)

    The metadata for the relation.

Returns:

  • (Class)

    The class being set up.



178
179
180
181
182
183
184
# File 'lib/mongoid/relations/accessors.rb', line 178

def ids_getter(name, )
  ids_method = "#{name.to_s.singularize}_ids"
  re_define_method(ids_method) do
    send(name).only(:id).map(&:id)
  end
  self
end

#ids_setter(name, metadata) ⇒ Object

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.

@param [ String, Symbol ] name The name of the relation.
@param [ Metadata ] metadata The metadata for the relation.

@return [ Class ] The class being set up.

Examples:

Set up the id_setter for the relation.

Person.ids_setter("addesses", )


228
229
230
231
232
233
234
# File 'lib/mongoid/relations/accessors.rb', line 228

def ids_setter(name, )
  ids_method = "#{name.to_s.singularize}_ids="
  re_define_method(ids_method) do |ids|
    send(.setter, .klass.find(ids))
  end
  self
end

#setter(name, metadata) ⇒ Class

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.

Examples:

Set up the setter for the relation.

Person.setter("addresses", )

Parameters:

  • name (String, Symbol)

    The name of the relation.

  • metadata (Metadata)

    The metadata for the relation.

Returns:

  • (Class)

    The class being set up.

Since:

  • 2.0.0.rc.1



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/mongoid/relations/accessors.rb', line 201

def setter(name, )
  re_define_method("#{name}=") do |object|
    without_autobuild do
      if relation_exists?(name) || .many? ||
        (object.blank? && send(name))
        set_relation(name, send(name).substitute(object.substitutable))
      else
        __build__(name, object.substitutable, )
      end
    end
  end
  self
end