Class: Reform::Form::Populator::IfEmpty

Inherits:
Reform::Form::Populator show all
Defined in:
lib/reform/form/populator.rb

Overview

Populator

Instance Method Summary collapse

Methods inherited from Reform::Form::Populator

#call, #initialize

Constructor Details

This class inherits a constructor from Reform::Form::Populator

Instance Method Details

#call!(options) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/reform/form/populator.rb', line 45

def call!(options)
  binding, twin, index, fragment = options[:binding], options[:model], options[:index], options[:fragment] # TODO: remove once we drop 2.0.
  form = options[:represented]

  if binding.array?
    item = twin.original[index] and return item

    new_index = [index, twin.count].min # prevents nil items with initially empty/smaller collections and :skip_if's.
    # this means the fragment index and populated nested form index might be different.

    twin.insert(new_index, run!(form, fragment, options)) # form.songs.insert(Song.new)
  else
    return if twin

    form.send(binding.setter, run!(form, fragment, options)) # form.artist=(Artist.new)
  end
end