Class: ActiveNode::Associations::Association
- Inherits:
-
Object
- Object
- ActiveNode::Associations::Association
- 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
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
:nodoc:.
-
#reflection ⇒ Object
readonly
:nodoc:.
-
#target ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#klass ⇒ Object
Returns the class of the target.
- #load_target ⇒ Object
-
#reader(force_reload = false) ⇒ Object
Implements the reader method, e.g.
-
#reset ⇒ Object
Resets the loaded flag to
false
and sets the target tonil
. - #save ⇒ Object
-
#writer(records) ⇒ Object
Implements the writer method, e.g.
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
#owner ⇒ Object (readonly)
:nodoc:
20 21 22 |
# File 'lib/active_node/associations/association.rb', line 20 def owner @owner end |
#reflection ⇒ Object (readonly)
:nodoc:
20 21 22 |
# File 'lib/active_node/associations/association.rb', line 20 def reflection @reflection end |
#target ⇒ Object (readonly)
:nodoc:
20 21 22 |
# File 'lib/active_node/associations/association.rb', line 20 def target @target end |
Instance Method Details
#klass ⇒ Object
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_target ⇒ Object
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 |
#reset ⇒ Object
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 |
#save ⇒ Object
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 |