Class: ActiveMongoid::Associations::Many
- Inherits:
-
Proxy
- Object
- Proxy
- ActiveMongoid::Associations::Many
show all
- Includes:
- Mongoid::Threaded::Lifecycle
- Defined in:
- lib/active_mongoid/associations/many.rb
Instance Attribute Summary
Attributes inherited from Proxy
#__metadata__, #base, #target
Instance Method Summary
collapse
-
#<<(*args) ⇒ Object
(also: #push)
-
#blank? ⇒ Boolean
-
#build(attributes = {}, type = nil) {|obj| ... } ⇒ Object
(also: #new)
-
#concat(objects) ⇒ Object
-
#create(attributes = nil, type = nil, &block) ⇒ Object
-
#create!(attributes = nil, type = nil, &block) ⇒ Object
-
#delete(object) ⇒ Object
-
#delete_all(conditions = nil) ⇒ Object
-
#destroy_all(conditions = nil) ⇒ Object
-
#each ⇒ Object
-
#exists? ⇒ Boolean
-
#find(*args) ⇒ Object
-
#find_or_create_by(attrs = {}, type = nil, &block) ⇒ Object
-
#find_or_initialize_by(attrs = {}, type = nil, &block) ⇒ Object
-
#initialize(base, target, metadata) ⇒ Many
constructor
-
#nil? ⇒ Boolean
-
#nullify ⇒ Object
(also: #nullify_all)
-
#purge ⇒ Object
(also: #clear)
-
#raise_unsaved(obj) ⇒ Object
-
#respond_to?(name, include_private = false) ⇒ Boolean
-
#scoped ⇒ Object
-
#substitute(replacement) ⇒ Object
-
#unscoped ⇒ Object
Methods inherited from Proxy
#==, #init, #klass, #method_missing
Constructor Details
#initialize(base, target, metadata) ⇒ Many
Returns a new instance of Many.
Instance Method Details
#<<(*args) ⇒ Object
Also known as:
push
16
17
18
19
20
21
22
23
24
|
# File 'lib/active_mongoid/associations/many.rb', line 16
def <<(*args)
objs = args.flatten
return concat(objs) if objs.size > 1
if objs = objs.first
append(objs)
objs.save if base.persisted? && !_binding?
end
self
end
|
#blank? ⇒ Boolean
106
107
108
|
# File 'lib/active_mongoid/associations/many.rb', line 106
def blank?
size == 0
end
|
#build(attributes = {}, type = nil) {|obj| ... } ⇒ Object
Also known as:
new
27
28
29
30
31
32
|
# File 'lib/active_mongoid/associations/many.rb', line 27
def build(attributes = {}, type = nil)
obj = (type || klass).new(attributes)
append(obj)
yield(obj) if block_given?
obj
end
|
#concat(objects) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/active_mongoid/associations/many.rb', line 35
def concat(objects)
objs, inserts = [], []
objects.each do |obj|
next unless obj
append(obj)
save_or_delay(obj, objs, inserts) if base.persisted?
end
persist_delayed(objs, inserts)
self
end
|
#create(attributes = nil, type = nil, &block) ⇒ Object
110
111
112
113
114
115
116
117
118
|
# File 'lib/active_mongoid/associations/many.rb', line 110
def create(attributes = nil, type = nil, &block)
if attributes.is_a?(::Array)
attributes.map { |attrs| create(attrs, type, &block) }
else
obj = build(attributes, type, &block)
base.persisted? ? obj.save : raise_unsaved(obj)
obj
end
end
|
#create!(attributes = nil, type = nil, &block) ⇒ Object
120
121
122
123
124
125
126
127
128
|
# File 'lib/active_mongoid/associations/many.rb', line 120
def create!(attributes = nil, type = nil, &block)
if attributes.is_a?(::Array)
attributes.map { |attrs| create!(attrs, type, &block) }
else
obj = build(attributes, type, &block)
base.persisted? ? obj.save! : raise_unsaved(obj)
obj
end
end
|
#delete(object) ⇒ Object
61
62
63
64
65
66
|
# File 'lib/active_mongoid/associations/many.rb', line 61
def delete(object)
target.delete(object) do |obj|
unbind_one(obj) if obj
cascade!(obj) if obj
end
end
|
#delete_all(conditions = nil) ⇒ Object
68
69
70
|
# File 'lib/active_mongoid/associations/many.rb', line 68
def delete_all(conditions = nil)
remove_all(conditions, :delete_all)
end
|
#destroy_all(conditions = nil) ⇒ Object
72
73
74
|
# File 'lib/active_mongoid/associations/many.rb', line 72
def destroy_all(conditions = nil)
remove_all(conditions, :destroy_all)
end
|
#each ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/active_mongoid/associations/many.rb', line 76
def each
if block_given?
target.each { |obj| yield(obj) }
else
to_enum
end
end
|
#exists? ⇒ Boolean
84
85
86
|
# File 'lib/active_mongoid/associations/many.rb', line 84
def exists?
criteria.exists?
end
|
#find(*args) ⇒ Object
88
89
90
91
92
93
94
95
|
# File 'lib/active_mongoid/associations/many.rb', line 88
def find(*args)
begin
matching = criteria.find(*args)
rescue Exception => e
end
Array(matching).each { |obj| target.push(obj) }
matching
end
|
#find_or_create_by(attrs = {}, type = nil, &block) ⇒ Object
130
131
132
|
# File 'lib/active_mongoid/associations/many.rb', line 130
def find_or_create_by(attrs = {}, type = nil, &block)
find_or(:create, attrs, type, &block)
end
|
#find_or_initialize_by(attrs = {}, type = nil, &block) ⇒ Object
134
135
136
|
# File 'lib/active_mongoid/associations/many.rb', line 134
def find_or_initialize_by(attrs = {}, type = nil, &block)
find_or(:build, attrs, type, &block)
end
|
#nil? ⇒ Boolean
138
139
140
|
# File 'lib/active_mongoid/associations/many.rb', line 138
def nil?
false
end
|
#nullify ⇒ Object
Also known as:
nullify_all
97
98
99
100
101
102
103
|
# File 'lib/active_mongoid/associations/many.rb', line 97
def nullify
criteria.update_all(__metadata__.foreign_key => nil)
target.clear do |obj|
unbind_one(obj)
obj.changed_attributes.delete(__metadata__.foreign_key)
end
end
|
#purge ⇒ Object
Also known as:
clear
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/active_mongoid/associations/many.rb', line 46
def purge
unless __metadata__.destructive?
nullify
else
after_remove_error = nil
criteria.delete_all
many = target.clear do |obj|
unbind_one(obj)
obj.destroyed = true if obj.respond_to?(:destroyed=)
end
many
end
end
|
#raise_unsaved(obj) ⇒ Object
155
156
157
|
# File 'lib/active_mongoid/associations/many.rb', line 155
def raise_unsaved(obj)
end
|
#respond_to?(name, include_private = false) ⇒ Boolean
142
143
144
145
|
# File 'lib/active_mongoid/associations/many.rb', line 142
def respond_to?(name, include_private = false)
[].respond_to?(name, include_private) ||
klass.respond_to?(name, include_private) || super
end
|
#scoped ⇒ Object
147
148
149
|
# File 'lib/active_mongoid/associations/many.rb', line 147
def scoped
criteria
end
|
#substitute(replacement) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/active_mongoid/associations/many.rb', line 159
def substitute(replacement)
if replacement
new_objs, objs = replacement.compact, []
new_ids = new_objs.map { |obj| obj.id }
remove_not_in(new_ids)
new_objs.each do |obj|
objs.push(obj) if obj.send(__metadata__.foreign_key) != base.id
end
concat(objs)
else
purge
end
self
end
|
#unscoped ⇒ Object
151
152
153
|
# File 'lib/active_mongoid/associations/many.rb', line 151
def unscoped
criteria.unscoped
end
|