Class: NinjaModel::Associations::Association
- Inherits:
-
Object
- Object
- NinjaModel::Associations::Association
show all
- Defined in:
- lib/ninja_model/associations/association.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(owner, reflection) ⇒ Association
Returns a new instance of Association.
9
10
11
12
13
14
|
# File 'lib/ninja_model/associations/association.rb', line 9
def initialize(owner, reflection)
@target = nil
@owner, @reflection = owner, reflection
reset
end
|
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
5
6
7
|
# File 'lib/ninja_model/associations/association.rb', line 5
def owner
@owner
end
|
#reflection ⇒ Object
Returns the value of attribute reflection.
5
6
7
|
# File 'lib/ninja_model/associations/association.rb', line 5
def reflection
@reflection
end
|
#target ⇒ Object
Returns the value of attribute target.
5
6
7
|
# File 'lib/ninja_model/associations/association.rb', line 5
def target
@target
end
|
Instance Method Details
#association_scope ⇒ Object
53
54
55
56
57
|
# File 'lib/ninja_model/associations/association.rb', line 53
def association_scope
if klass
@association_scope ||= AssociationScope.new(self).scope
end
end
|
#klass ⇒ Object
70
71
72
|
# File 'lib/ninja_model/associations/association.rb', line 70
def klass
reflection.klass
end
|
#load_target ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/ninja_model/associations/association.rb', line 78
def load_target
if find_target?
@target ||= find_target
end
loaded! unless loaded?
target
rescue NinjaModel::RecordNotFound
reset
end
|
#loaded! ⇒ Object
32
33
34
|
# File 'lib/ninja_model/associations/association.rb', line 32
def loaded!
@loaded = true
end
|
#loaded? ⇒ Boolean
28
29
30
|
# File 'lib/ninja_model/associations/association.rb', line 28
def loaded?
@loaded
end
|
#reload ⇒ Object
21
22
23
24
25
26
|
# File 'lib/ninja_model/associations/association.rb', line 21
def reload
reset
reset_scope
load_target
self unless target.nil?
end
|
#reset ⇒ Object
16
17
18
19
|
# File 'lib/ninja_model/associations/association.rb', line 16
def reset
@loaded = false
@target = nil
end
|
#reset_scope ⇒ Object
59
60
61
|
# File 'lib/ninja_model/associations/association.rb', line 59
def reset_scope
@association_scope = nil
end
|
#scoped ⇒ Object
45
46
47
|
# File 'lib/ninja_model/associations/association.rb', line 45
def scoped
target_scope.merge(association_scope)
end
|
#set_inverse_instance(record) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/ninja_model/associations/association.rb', line 63
def set_inverse_instance(record)
if record && invertible_for?(record)
inverse = record.association(inverse_reflection_for(record).name)
inverse.target = owner
end
end
|
#stale_target? ⇒ Boolean
36
37
38
|
# File 'lib/ninja_model/associations/association.rb', line 36
def stale_target?
false
end
|
#target_scope ⇒ Object
49
50
51
|
# File 'lib/ninja_model/associations/association.rb', line 49
def target_scope
klass.scoped
end
|