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:.
-
#rel_target ⇒ Object
readonly
:nodoc:.
-
#target ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#ids_reader ⇒ Object
Implements the ids reader method, e.g.
-
#ids_writer(ids) ⇒ Object
Implements the ids writer method, e.g.
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#klass ⇒ Object
Returns the class of the target.
- #reader(*args) ⇒ Object
- #rel(*associations) ⇒ Object
- #rels_reader(*args) ⇒ Object
- #rels_writer(rels) ⇒ Object
-
#reset ⇒ Object
Resets the loaded flag to
false
and sets the target tonil
. - #save(fresh = false) ⇒ Object
-
#writer(records) ⇒ Object
(also: #super_writer)
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 |
#rel_target ⇒ Object (readonly)
:nodoc:
20 21 22 |
# File 'lib/active_node/associations/association.rb', line 20 def rel_target @rel_target end |
#target ⇒ Object (readonly)
:nodoc:
20 21 22 |
# File 'lib/active_node/associations/association.rb', line 20 def target @target end |
Instance Method Details
#ids_reader ⇒ Object
Implements the ids reader method, e.g. foo.item_ids for Foo.has_many :items
68 69 70 71 |
# File 'lib/active_node/associations/association.rb', line 68 def ids_reader reader @target.map(&:id) end |
#ids_writer(ids) ⇒ Object
Implements the ids writer method, e.g. foo.item_ids= for Foo.has_many :items
58 59 60 61 |
# File 'lib/active_node/associations/association.rb', line 58 def ids_writer(ids) @rel_target = nil super_writer klass.find(ids.reject(&:blank?).map!(&:to_i)) end |
#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 |
#reader(*args) ⇒ Object
63 64 65 |
# File 'lib/active_node/associations/association.rb', line 63 def reader(*args) @target ||= rels_reader(*args).map &:other end |
#rel(*associations) ⇒ Object
44 45 46 |
# File 'lib/active_node/associations/association.rb', line 44 def rel(*associations) owner.relationships(reflection, *associations) end |
#rels_reader(*args) ⇒ Object
74 75 76 77 |
# File 'lib/active_node/associations/association.rb', line 74 def rels_reader(*args) rel(*args) unless @rel_target @rel_target ||= [] end |
#rels_writer(rels) ⇒ Object
79 80 81 82 83 |
# File 'lib/active_node/associations/association.rb', line 79 def rels_writer(rels) @target = nil @dirty = true @rel_target = rels 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(fresh = false) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/active_node/associations/association.rb', line 85 def save(fresh=false) #return unless @dirty #delete all relations missing in new target original_rels = fresh ? [] : ActiveNode::Graph::Builder.new(owner.class, reflection.name).build(owner.id).first.association(reflection.name).rels_reader original_rels.each do |r| unless ids_reader.include? r.other.id Neo.db.delete_relationship(r.id) original_rels.delete(r) end end #add relations missing in old target #if no rel_target proceed as before + set rel_target from db #if rel_target exists update persisted records and insert new records if @rel_target @rel_target.each { |r| r.save(self) } else @target.map do |n| original_rels.detect { |r| r.other.id == n.id }.tap { |o_r| o_r.try :other=, n } || ActiveNode::Relationship.create!(n, self) end end end |
#writer(records) ⇒ Object Also known as: super_writer
Implements the writer method, e.g. foo.items= for Foo.has_many :items
49 50 51 52 53 |
# File 'lib/active_node/associations/association.rb', line 49 def writer(records) @dirty = true @rel_target = nil @target = records end |