Class: ActiveData::Model::Associations::ReferencesMany

Inherits:
ReferencesAny 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 ReferencesAny

#scope

Methods inherited from Base

#apply_changes!, #callback, #evar_loaded?, #initialize, #inspect, #loaded!, #loaded?, #reload, #reset, #target, #transaction

Constructor Details

This class inherits a constructor from ActiveData::Model::Associations::Base

Instance Method Details

#apply_changesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_data/model/associations/references_many.rb', line 21

def apply_changes
  target.all? do |object|
    if object
      if object.marked_for_destruction? && reflection.autosave?
        object.destroy
      elsif object.new_record? || (reflection.autosave? && object.changed?)
        persist_object(object)
      else
        true
      end
    else
      true
    end
  end
end

#build(attributes = {}) ⇒ Object



5
6
7
# File 'lib/active_data/model/associations/references_many.rb', line 5

def build(attributes = {})
  append([build_object(attributes)]).last
end

#clearObject



82
83
84
85
86
87
# File 'lib/active_data/model/associations/references_many.rb', line 82

def clear
  attribute.pollute do
    write_source([])
  end
  reload.empty?
end

#concat(*objects) ⇒ Object



77
78
79
80
# File 'lib/active_data/model/associations/references_many.rb', line 77

def concat(*objects)
  append objects.flatten
  reader
end

#create(attributes = {}) ⇒ Object



9
10
11
12
13
# File 'lib/active_data/model/associations/references_many.rb', line 9

def create(attributes = {})
  object = build(attributes)
  persist_object(object)
  object
end

#create!(attributes = {}) ⇒ Object



15
16
17
18
19
# File 'lib/active_data/model/associations/references_many.rb', line 15

def create!(attributes = {})
  object = build(attributes)
  persist_object(object, raise_error: true)
  object
end

#defaultObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_data/model/associations/references_many.rb', line 47

def default
  return [] if evar_loaded?

  default = Array.wrap(reflection.default(owner))

  return [] unless default

  if default.all? { |object| object.is_a?(reflection.persistence_adapter.data_type) }
    default
  elsif default.all? { |object| object.is_a?(Hash) }
    default.map { |attributes| build_object(attributes) }
  else
    reflection.persistence_adapter.find_all(owner, default)
  end || []
end

#identifyObject



89
90
91
# File 'lib/active_data/model/associations/references_many.rb', line 89

def identify
  target.map { |obj| reflection.persistence_adapter.identify(obj) }
end

#load_targetObject



42
43
44
45
# File 'lib/active_data/model/associations/references_many.rb', line 42

def load_target
  source = read_source
  source.present? ? reflection.persistence_adapter.find_all(owner, source) : default
end

#reader(force_reload = false) ⇒ Object



63
64
65
66
# File 'lib/active_data/model/associations/references_many.rb', line 63

def reader(force_reload = false)
  reload if force_reload
  @proxy ||= reflection.persistence_adapter.referenced_proxy(self)
end

#replace(objects) ⇒ Object Also known as: writer



68
69
70
71
72
73
74
# File 'lib/active_data/model/associations/references_many.rb', line 68

def replace(objects)
  loaded!
  transaction do
    clear
    append objects
  end
end

#target=(object) ⇒ Object



37
38
39
40
# File 'lib/active_data/model/associations/references_many.rb', line 37

def target=(object)
  loaded!
  @target = object.to_a
end