Class: ActiveData::Model::Associations::ReferencesMany
- Inherits:
-
Base
- Object
- Base
- ActiveData::Model::Associations::ReferencesMany
show all
- Defined in:
- lib/active_data/model/associations/references_many.rb
Instance Attribute Summary
Attributes inherited from Base
#owner, #reflection
Instance Method Summary
collapse
Methods inherited from Base
#apply_changes!, #evar_loaded?, #initialize, #inspect, #loaded!, #loaded?, #reload, #reset, #target, #transaction
Instance Method Details
#apply_changes ⇒ Object
5
6
7
8
9
|
# File 'lib/active_data/model/associations/references_many.rb', line 5
def apply_changes
present_keys = target.reject { |t| t.marked_for_destruction? }.map(&reflection.primary_key).compact
write_source(present_keys)
true
end
|
#clear ⇒ Object
61
62
63
64
65
66
|
# File 'lib/active_data/model/associations/references_many.rb', line 61
def clear
attribute.pollute do
write_source([])
end
reload.empty?
end
|
#concat(*objects) ⇒ Object
56
57
58
59
|
# File 'lib/active_data/model/associations/references_many.rb', line 56
def concat(*objects)
append objects.flatten
reader
end
|
#default ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/active_data/model/associations/references_many.rb', line 21
def default
unless evar_loaded?
default = Array.wrap(reflection.default(owner))
if default.all? { |object| object.is_a?(reflection.klass) }
default
elsif default.all? { |object| object.is_a?(Hash) }
default.map { |attributes| reflection.klass.new(attributes) }
else
scope(default).to_a
end if default.present?
end || []
end
|
#identify ⇒ Object
72
73
74
|
# File 'lib/active_data/model/associations/references_many.rb', line 72
def identify
target.map(&reflection.primary_key)
end
|
#load_target ⇒ Object
16
17
18
19
|
# File 'lib/active_data/model/associations/references_many.rb', line 16
def load_target
source = read_source
source.present? ? scope(source).to_a : default
end
|
#read_source ⇒ Object
34
35
36
|
# File 'lib/active_data/model/associations/references_many.rb', line 34
def read_source
attribute.read_before_type_cast
end
|
#reader(force_reload = false) ⇒ Object
42
43
44
45
|
# File 'lib/active_data/model/associations/references_many.rb', line 42
def reader force_reload = false
reload if force_reload
@proxy ||= Collection::Referenced.new self
end
|
#replace(objects) ⇒ Object
Also known as:
writer
47
48
49
50
51
52
53
|
# File 'lib/active_data/model/associations/references_many.rb', line 47
def replace objects
loaded!
transaction do
clear
append objects
end
end
|
#scope(source = read_source) ⇒ Object
68
69
70
|
# File 'lib/active_data/model/associations/references_many.rb', line 68
def scope source = read_source
reflection.scope.where(reflection.primary_key => source)
end
|
#target=(object) ⇒ Object
11
12
13
14
|
# File 'lib/active_data/model/associations/references_many.rb', line 11
def target= object
loaded!
@target = object.to_a
end
|
#write_source(value) ⇒ Object
38
39
40
|
# File 'lib/active_data/model/associations/references_many.rb', line 38
def write_source value
attribute.write_value value
end
|