class Reform::Form::Populator::IfEmpty

Public Instance Methods

call!(options) click to toggle source
# File lib/reform/form/populator.rb, line 55
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

Private Instance Methods

deprecate_positional_args(form, proc, options) { || ... } click to toggle source
# File lib/reform/form/populator.rb, line 83
def deprecate_positional_args(form, proc, options) # TODO: remove in 2.2.
  arity = proc.is_a?(Symbol) ? form.method(proc).arity : proc.arity
  return yield if arity == 1
  warn "[Reform] Positional arguments for :prepopulate and friends are deprecated. Please use ->(options) and enjoy the rest of your day. Learn more at http://trailblazerb.org/gems/reform/upgrading-guide.html#to-21"

  @value.(form, options[:fragment], options[:user_options])
end
run!(form, fragment, options) click to toggle source
# File lib/reform/form/populator.rb, line 75
def run!(form, fragment, options)
  return @user_proc.new if @user_proc.is_a?(Class) # handle populate_if_empty: Class. this excludes using Callables, though.

  deprecate_positional_args(form, @user_proc, options) do
    evaluate_option(form, options)
  end
end