Class: FormObj::ModelMapper::Array

Inherits:
Form::Array show all
Defined in:
lib/form_obj/model_mapper/array.rb

Instance Method Summary collapse

Methods inherited from Form::Array

#mark_as_persisted, #mark_for_destruction, #marked_for_destruction, #persisted?

Methods inherited from Struct::Array

#build, #create, #to_hash, #update_attributes

Constructor Details

#initialize(item_class, model_attribute, *args) ⇒ Array

Returns a new instance of Array.



4
5
6
7
# File 'lib/form_obj/model_mapper/array.rb', line 4

def initialize(item_class, model_attribute, *args)
  @model_attribute = model_attribute
  super(item_class, *args)
end

Instance Method Details

#load_from_models(models, *args) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/form_obj/model_mapper/array.rb', line 9

def load_from_models(models, *args)
  clear
  iterate_through_models_to_load_them(models[:default] || [], *args) do |model|
    build.load_from_models(models.merge(default: model), *args)
  end
  self
end

#model_primary_keyObject



25
26
27
# File 'lib/form_obj/model_mapper/array.rb', line 25

def model_primary_key
  self.item_class.model_primary_key
end

#sync_to_models(models) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/form_obj/model_mapper/array.rb', line 17

def sync_to_models(models)
  items = define_models_for_CUD(models)

  sync_destruction_to_models(models, items[:destroy])
  sync_update_to_models(models, items[:update])
  sync_creation_to_models(models, items[:create])
end

#to_models_hash(models) ⇒ Object



29
30
31
32
# File 'lib/form_obj/model_mapper/array.rb', line 29

def to_models_hash(models)
  self.each { |item| models[:default] << item.to_models_hash(models.merge(default: {}))[:default] }
  models
end