Module: Neo4j::ActiveNode::HasN::ClassMethods

Defined in:
lib/neo4j/active_node/has_n.rb

Instance Method Summary collapse

Instance Method Details

#association?(name) ⇒ Boolean

rubocop:enable Style/PredicateName

Returns:



228
229
230
# File 'lib/neo4j/active_node/has_n.rb', line 228

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

#associationsObject



232
233
234
# File 'lib/neo4j/active_node/has_n.rb', line 232

def associations
  @associations ||= {}
end

#associations_keysObject



236
237
238
# File 'lib/neo4j/active_node/has_n.rb', line 236

def associations_keys
  @associations_keys ||= associations.keys
end

#has_association?(name) ⇒ Boolean

:nocov:

Returns:



219
220
221
222
223
# File 'lib/neo4j/active_node/has_n.rb', line 219

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 `ActiveNode` class,
    `false` to specify any model, or nil to specify that it should be guessed.

  *rel_class*: The ``ActiveRel`` class to use for this association.  Can be either a
    model object ``include`` ing ``ActiveRel`` 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)


330
331
332
333
334
335
# File 'lib/neo4j/active_node/has_n.rb', line 330

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

  define_has_many_methods(name)
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 <Neo4j/ActiveNode/HasN/ClassMethods#has_many>` for anything not specified here



347
348
349
350
351
352
# File 'lib/neo4j/active_node/has_n.rb', line 347

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

  define_has_one_methods(name)
end

#inherited(klass) ⇒ Object

make sure the inherited classes inherit the _decl_rels hash



241
242
243
244
245
# File 'lib/neo4j/active_node/has_n.rb', line 241

def inherited(klass)
  klass.instance_variable_set(:@associations, associations.clone)
  @associations_keys = klass.associations_keys.clone
  super
end