Class: ActiveData::Model::Associations::EmbedsMany
- Inherits:
-
EmbedsAny
- Object
- Base
- EmbedsAny
- ActiveData::Model::Associations::EmbedsMany
show all
- Defined in:
- lib/active_data/model/associations/embeds_many.rb
Instance Attribute Summary
Attributes inherited from Base
#owner, #reflection
Instance Method Summary
collapse
Methods inherited from Base
#apply_changes!, #callback, #evar_loaded?, #initialize, #inspect, #loaded!, #loaded?, #reload, #target, #transaction
Instance Method Details
#apply_changes ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 21
def apply_changes
result = target.map do |object|
object.destroyed? || object.marked_for_destruction? ? object.destroy : object.save
end.all?
@destroyed = target.select(&:destroyed?)
target.delete_if(&:destroyed?)
result
end
|
#build(attributes = {}) ⇒ Object
5
6
7
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 5
def build(attributes = {})
push_object(build_object(attributes))
end
|
#clear ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 65
def clear
begin
transaction { target.all?(&:destroy!) }
rescue ActiveData::ObjectNotDestroyed
nil
end
reload.empty?
end
|
#concat(*objects) ⇒ Object
87
88
89
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 87
def concat(*objects)
append objects.flatten
end
|
#create(attributes = {}) ⇒ Object
9
10
11
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 9
def create(attributes = {})
build(attributes).tap(&:save)
end
|
#create!(attributes = {}) ⇒ Object
13
14
15
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 13
def create!(attributes = {})
build(attributes).tap(&:save!)
end
|
#default ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 41
def default
unless evar_loaded?
default = Array.wrap(reflection.default(owner))
if default.present?
collection = if default.all? { |object| object.is_a?(reflection.klass) }
default
else
default.map do |attributes|
reflection.klass.with_sanitize(false) do
build_object(attributes)
end
end
end
collection.map { |object| object.send(:clear_changes_information) } if reflection.klass.dirty?
collection
end
end || []
end
|
#destroyed ⇒ Object
17
18
19
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 17
def destroyed
@destroyed ||= []
end
|
#load_target ⇒ Object
36
37
38
39
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 36
def load_target
source = read_source
source.present? ? reflection.klass.instantiate_collection(source) : default
end
|
#reader(force_reload = false) ⇒ Object
74
75
76
77
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 74
def reader(force_reload = false)
reload if force_reload
@proxy ||= Collection::Embedded.new self
end
|
#replace(objects) ⇒ Object
Also known as:
writer
79
80
81
82
83
84
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 79
def replace(objects)
transaction do
clear
append(objects) or raise ActiveData::AssociationChangesNotApplied
end
end
|
#reset ⇒ Object
60
61
62
63
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 60
def reset
super
@target = []
end
|
#target=(objects) ⇒ Object
30
31
32
33
34
|
# File 'lib/active_data/model/associations/embeds_many.rb', line 30
def target=(objects)
objects.each { |object| setup_performers! object }
loaded!
@target = objects
end
|