Class: NoBrainer::Document::Association::HasMany
- Inherits:
-
Object
- Object
- NoBrainer::Document::Association::HasMany
show all
- Includes:
- Core
- Defined in:
- lib/no_brainer/document/association/has_many.rb
Direct Known Subclasses
HasOne
Defined Under Namespace
Classes: Metadata
Instance Method Summary
collapse
Methods included from Core
#assert_target_type, #initialize
Instance Method Details
#before_destroy_callback ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/no_brainer/document/association/has_many.rb', line 122
def before_destroy_callback
case metadata.options[:dependent]
when :destroy then dependent_criteria.destroy_all
when :delete then dependent_criteria.delete_all
when :nullify then dependent_criteria.update_all(foreign_key => nil)
when :restrict then raise NoBrainer::Error::ChildrenExist unless dependent_criteria.empty?
end
end
|
#dependent_criteria ⇒ Object
118
119
120
|
# File 'lib/no_brainer/document/association/has_many.rb', line 118
def dependent_criteria
target_criteria.unscoped
end
|
92
93
94
|
# File 'lib/no_brainer/document/association/has_many.rb', line 92
def loaded?
target_criteria.cached?
end
|
#preload(new_targets) ⇒ Object
96
97
98
99
|
# File 'lib/no_brainer/document/association/has_many.rb', line 96
def preload(new_targets)
set_inverses_of(new_targets)
target_criteria._override_cache(new_targets)
end
|
#read ⇒ Object
83
84
85
|
# File 'lib/no_brainer/document/association/has_many.rb', line 83
def read
target_criteria
end
|
#set_inverse_proc ⇒ Object
114
115
116
|
# File 'lib/no_brainer/document/association/has_many.rb', line 114
def set_inverse_proc
->(target){ set_inverses_of([target]) if target.is_a?(NoBrainer::Document) }
end
|
#set_inverses_of(new_targets) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/no_brainer/document/association/has_many.rb', line 101
def set_inverses_of(new_targets)
@inverses ||= metadata.inverses
return if @inverses.blank?
new_targets.each do |target|
@inverses.each { |inverse| target.associations[inverse].preload(self.owner) }
end
end
|
#target_criteria ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/no_brainer/document/association/has_many.rb', line 69
def target_criteria
@target_criteria ||= begin
query_criteria = { foreign_key => owner.__send__(primary_key) }
if metadata.options[:as]
query_criteria = query_criteria.merge(
foreign_type => owner.root_class.name
)
end
base_criteria.where(query_criteria).after_find(set_inverse_proc)
end
end
|
#write(new_children) ⇒ Object
87
88
89
90
|
# File 'lib/no_brainer/document/association/has_many.rb', line 87
def write(new_children)
raise "You can't assign `#{target_name}'. " \
"Instead, you must modify delete and create `#{target_model}' manually."
end
|