Class: Granite::Form::Model::Associations::ReferencesMany

Inherits:
ReferencesAny show all
Defined in:
lib/granite/form/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

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

Constructor Details

This class inherits a constructor from Granite::Form::Model::Associations::Base

Instance Method Details

#clearObject



52
53
54
55
56
57
# File 'lib/granite/form/model/associations/references_many.rb', line 52

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

#concat(*objects) ⇒ Object



47
48
49
50
# File 'lib/granite/form/model/associations/references_many.rb', line 47

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

#defaultObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/granite/form/model/associations/references_many.rb', line 16

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



59
60
61
# File 'lib/granite/form/model/associations/references_many.rb', line 59

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

#load_targetObject



11
12
13
14
# File 'lib/granite/form/model/associations/references_many.rb', line 11

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

#reader(force_reload = false) ⇒ Object



32
33
34
35
# File 'lib/granite/form/model/associations/references_many.rb', line 32

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

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



37
38
39
40
41
42
43
# File 'lib/granite/form/model/associations/references_many.rb', line 37

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

#target=(object) ⇒ Object



6
7
8
9
# File 'lib/granite/form/model/associations/references_many.rb', line 6

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