Class: Reform::Form::Populator

Inherits:
Object
  • Object
show all
Includes:
Uber::Callable
Defined in:
lib/reform/form/populator.rb

Overview

Implements the :populator option.

populator: -> (fragment, twin, options)
populator: -> (fragment, collection[twin], index, options)

For collections, the entire collection and the currently deserialised index is passed in.

Direct Known Subclasses

IfEmpty, Sync

Defined Under Namespace

Classes: IfEmpty, Sync

Instance Method Summary collapse

Constructor Details

#initialize(user_proc) ⇒ Populator

Returns a new instance of Populator.



10
11
12
13
# File 'lib/reform/form/populator.rb', line 10

def initialize(user_proc)
  @user_proc = user_proc # the actual `populator: ->{}` block from the user, via ::property.
  @value     = Uber::Options::Value.new(user_proc) # we can now process Callable, procs, :symbol.
end

Instance Method Details

#call(form, fragment, *args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/reform/form/populator.rb', line 15

def call(form, fragment, *args)
  options = args.last

  # FIXME: the optional index parameter SUCKS.
  twin = call!(form, fragment, options.binding.get, *args)

  # this kinda sucks. the proc may call self.composer = Artist.new, but there's no way we can
  # return the twin instead of the model from the #composer= setter.
  twin = options.binding.get unless options.binding.array?

  # since Populator#call is invoked as :instance, we always need to return a twin/form here.
  handle_fail(twin, options)

  twin
end