Class: Granite::Form::Model::Associations::EmbedsMany

Inherits:
EmbedsAny
  • Object
show all
Defined in:
lib/granite/form/model/associations/embeds_many.rb

Instance Attribute Summary

Attributes inherited from Base

#owner, #reflection

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

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

Instance Method Details

#build(attributes = {}) ⇒ Object



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

def build(attributes = {})
  push_object(build_object(attributes))
end

#clearObject



49
50
51
52
53
# File 'lib/granite/form/model/associations/embeds_many.rb', line 49

def clear
  target
  @target = []
  true
end

#concat(*objects) ⇒ Object



69
70
71
# File 'lib/granite/form/model/associations/embeds_many.rb', line 69

def concat(*objects)
  append objects.flatten
end

#defaultObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/granite/form/model/associations/embeds_many.rb', line 21

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

#load_targetObject



16
17
18
19
# File 'lib/granite/form/model/associations/embeds_many.rb', line 16

def load_target
  source = read_source
  source.present? ? reflection.klass.instantiate_collection(source) : default
end

#reader(force_reload = false) ⇒ Object



55
56
57
58
# File 'lib/granite/form/model/associations/embeds_many.rb', line 55

def reader(force_reload = false)
  reload if force_reload
  @proxy ||= Collection::Embedded.new self
end

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



60
61
62
63
64
65
# File 'lib/granite/form/model/associations/embeds_many.rb', line 60

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

#resetObject



40
41
42
43
# File 'lib/granite/form/model/associations/embeds_many.rb', line 40

def reset
  super
  @target = []
end

#syncObject



45
46
47
# File 'lib/granite/form/model/associations/embeds_many.rb', line 45

def sync
  write_source(target.map { |model| model_data(model) })
end

#target=(objects) ⇒ Object



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

def target=(objects)
  objects.each { |object| setup_performers! object }
  loaded!
  @target = objects
end