Class: ActiveNode::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_node/associations/association.rb

Overview

Active Record Associations

This is the root class of all associations (‘+ Foo’ signifies an included module Foo):

Association
  SingularAssociation
    HasOneAssociation
      HasOneThroughAssociation + ThroughAssociation
    BelongsToAssociation
      BelongsToPolymorphicAssociation
  CollectionAssociation
    HasAndBelongsToManyAssociation
    HasManyAssociation
      HasManyThroughAssociation + ThroughAssociation

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Association

Returns a new instance of Association.



24
25
26
27
28
# File 'lib/active_node/associations/association.rb', line 24

def initialize(owner, reflection)
  @owner, @reflection = owner, reflection

  reset
end

Instance Attribute Details

#ownerObject (readonly)

:nodoc:



20
21
22
# File 'lib/active_node/associations/association.rb', line 20

def owner
  @owner
end

#reflectionObject (readonly)

:nodoc:



20
21
22
# File 'lib/active_node/associations/association.rb', line 20

def reflection
  @reflection
end

#targetObject (readonly)

:nodoc:



20
21
22
# File 'lib/active_node/associations/association.rb', line 20

def target
  @target
end

Instance Method Details

#klassObject

Returns the class of the target. belongs_to polymorphic overrides this to look at the polymorphic_type field on the owner.



39
40
41
# File 'lib/active_node/associations/association.rb', line 39

def klass
  reflection.klass
end

#load_targetObject



43
44
45
# File 'lib/active_node/associations/association.rb', line 43

def load_target
  owner.send(reflection.direction, reflection.type, reflection.klass)
end

#reader(force_reload = false) ⇒ Object

Implements the reader method, e.g. foo.items for Foo.has_many :items



48
49
50
# File 'lib/active_node/associations/association.rb', line 48

def reader(force_reload = false)
  @target ||= load_target
end

#resetObject

Resets the loaded flag to false and sets the target to nil.



31
32
33
34
35
# File 'lib/active_node/associations/association.rb', line 31

def reset
  @loaded = false
  @target = nil
  @stale_state = nil
end

#saveObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_node/associations/association.rb', line 58

def save
  return unless @dirty
  #delete all relations missing in new target
  owner.node.rels(reflection.type).send(reflection.direction).each do |rel|
    rel.del unless ids_reader.include? rel.other_node(owner.node).neo_id.to_i
  end
  original_target = owner.node.send(reflection.direction, reflection.type)
  original_target_ids = original_target.map(&:neo_id).map(&:to_i)
  #add relations missing in old target
  target_each { |n| original_target << n.node unless original_target_ids.include? n.id }
end

#writer(records) ⇒ Object

Implements the writer method, e.g. foo.items= for Foo.has_many :items



53
54
55
56
# File 'lib/active_node/associations/association.rb', line 53

def writer(records)
  @dirty = true
  @target = records
end