Module: ActiveGraph::Node::HasN::ClassMethods

Defined in:
lib/active_graph/node/has_n.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Instance Method Details

#association?(name) ⇒ Boolean

rubocop:enable Naming/PredicateName

Returns:


294
295
296
# File 'lib/active_graph/node/has_n.rb', line 294

def association?(name)
  !!associations[name.to_sym]
end

#associationsObject


302
303
304
# File 'lib/active_graph/node/has_n.rb', line 302

def associations
  (@associations ||= parent_associations.dup)
end

#associations_keysObject


306
307
308
# File 'lib/active_graph/node/has_n.rb', line 306

def associations_keys
  @associations_keys ||= associations.keys
end

#has_association?(name) ⇒ Boolean

:nocov:

Returns:


285
286
287
288
289
# File 'lib/active_graph/node/has_n.rb', line 285

def has_association?(name)
  ActiveSupport::Deprecation.warn 'has_association? is deprecated and may be removed from future releases, use association? instead.', caller

  association?(name)
end

#has_many(direction, name, options = {}) ⇒ Object

For defining an “has many” association on a model. This defines a set of methods on your model instances. For instance, if you define the association on a Person model:

.. code-block

ruby

has_many :out, :vehicles, type: :has_vehicle

This would define the following methods:

#vehicles

Returns a QueryProxy object.  This is an Enumerable object and thus can be iterated
over.  It also has the ability to accept class-level methods from the Vehicle model
(including calls to association methods)

**#vehicles=**

Takes an array of Vehicle objects and replaces all current ``:HAS_VEHICLE`` relationships
with new relationships refering to the specified objects

.vehicles

Returns a QueryProxy object.  This would represent all ``Vehicle`` objects associated with
either all ``Person`` nodes (if ``Person.vehicles`` is called), or all ``Vehicle`` objects
associated with the ``Person`` nodes thus far represented in the QueryProxy chain.
For example:

.. code-block:: ruby

  company.people.where(age: 40).vehicles

Arguments:

**direction:**
  **Available values:** ``:in``, ``:out``, or ``:both``.

  Refers to the relative to the model on which the association is being defined.

  Example:

  .. code-block:: ruby

    Person.has_many :out, :posts, type: :wrote

  means that a `WROTE` relationship goes from a `Person` node to a `Post` node

**name:**
  The name of the association.  The affects the methods which are created (see above).
  The name is also used to form default assumptions about the model which is being referred to

  Example:

  .. code-block:: ruby

    Person.has_many :out, :posts, type: :wrote

  will assume a `model_class` option of ``'Post'`` unless otherwise specified

**options:** A ``Hash`` of options.  Allowed keys are:
  *type*: The Neo4j relationship type.  This option is required unless either the
    `origin` or `rel_class` options are specified

  *origin*: The name of the association from another model which the `type` and `model_class`
    can be gathered.

    Example:

    .. code-block:: ruby

      # `model_class` of `Post` is assumed here
      Person.has_many :out, :posts, origin: :author

      Post.has_one :in, :author, type: :has_author, model_class: :Person

  *model_class*: The model class to which the association is referring.  Can be a
    Symbol/String (or an ``Array`` of same) with the name of the `Node` class,
    `false` to specify any model, or nil to specify that it should be guessed.

  *rel_class*: The ``Relationship`` class to use for this association.  Can be either a
    model object ``include`` ing ``Relationship`` or a Symbol/String (or an ``Array`` of same).
    **A Symbol or String is recommended** to avoid load-time issues

  *dependent*: Enables deletion cascading.
    **Available values:** ``:delete``, ``:delete_orphans``, ``:destroy``, ``:destroy_orphans``
    (note that the ``:destroy_orphans`` option is known to be "very metal".  Caution advised)

393
394
395
396
397
398
# File 'lib/active_graph/node/has_n.rb', line 393

def has_many(direction, name, options = {}) # rubocop:disable Naming/PredicateName
  name = name.to_sym
  build_association(:has_many, direction, name, options)

  define_has_many_methods(name, options)
end

#has_one(direction, name, options = {}) ⇒ Object

For defining an “has one” association on a model. This defines a set of methods on your model instances. For instance, if you define the association on a Person model:

has_one :out, :vehicle, type: :has_vehicle

This would define the methods: “#vehicle“, “#vehicle=“, and “.vehicle“.

See :ref:`#has_many <ActiveGraph/Node/HasN/ClassMethods#has_many>` for anything not specified here


410
411
412
413
414
415
# File 'lib/active_graph/node/has_n.rb', line 410

def has_one(direction, name, options = {}) # rubocop:disable Naming/PredicateName
  name = name.to_sym
  build_association(:has_one, direction, name, options)

  define_has_one_methods(name, options)
end

#parent_associationsObject


298
299
300
# File 'lib/active_graph/node/has_n.rb', line 298

def parent_associations
  superclass == Object ? {} : superclass.associations
end