Module: ActiveGraph::Node::HasN::ClassMethods
- Defined in:
- lib/active_graph/node/has_n.rb
Overview
rubocop:disable Metrics/ModuleLength
Instance Method Summary collapse
-
#association?(name) ⇒ Boolean
rubocop:enable Naming/PredicateName.
- #associations ⇒ Object
- #associations_keys ⇒ Object
-
#has_association?(name) ⇒ Boolean
:nocov:.
-
#has_many(direction, name, options = {}) ⇒ Object
For defining an “has many” association on a model.
-
#has_one(direction, name, options = {}) ⇒ Object
For defining an “has one” association on a model.
- #parent_associations ⇒ Object
Instance Method Details
#association?(name) ⇒ Boolean
rubocop:enable Naming/PredicateName
306 307 308 |
# File 'lib/active_graph/node/has_n.rb', line 306 def association?(name) !!associations[name.to_sym] end |
#associations ⇒ Object
314 315 316 |
# File 'lib/active_graph/node/has_n.rb', line 314 def associations (@associations ||= parent_associations.dup) end |
#associations_keys ⇒ Object
318 319 320 |
# File 'lib/active_graph/node/has_n.rb', line 318 def associations_keys @associations_keys ||= associations.keys end |
#has_association?(name) ⇒ Boolean
:nocov:
297 298 299 300 301 |
# File 'lib/active_graph/node/has_n.rb', line 297 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)
405 406 407 408 409 410 |
# File 'lib/active_graph/node/has_n.rb', line 405 def has_many(direction, name, = {}) # rubocop:disable Naming/PredicateName name = name.to_sym build_association(:has_many, direction, name, ) 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 <ActiveGraph/Node/HasN/ClassMethods#has_many>` for anything not specified here
422 423 424 425 426 427 |
# File 'lib/active_graph/node/has_n.rb', line 422 def has_one(direction, name, = {}) # rubocop:disable Naming/PredicateName name = name.to_sym build_association(:has_one, direction, name, ) define_has_one_methods(name, ) end |
#parent_associations ⇒ Object
310 311 312 |
# File 'lib/active_graph/node/has_n.rb', line 310 def parent_associations superclass == Object ? {} : superclass.associations end |