Class: Reform::Form::Populator

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

Overview

Implements the :populator option.

populator: -> (fragment:, model:, :binding)
populator: -> (fragment:, collection:, index:, binding:)

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

Direct Known Subclasses

IfEmpty, Sync

Defined Under Namespace

Classes: External, IfEmpty, Sync

Instance Method Summary collapse

Constructor Details

#initialize(user_proc) ⇒ Populator

Returns a new instance of Populator.



8
9
10
11
# File 'lib/reform/form/populator.rb', line 8

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

Instance Method Details

#call(input, options) ⇒ Object



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

def call(input, options)
  model = get(options)
  twin  = call!(options.merge(model: model, collection: model))

  return twin if twin == Representable::Pipeline::Stop

  # 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 = get(options) unless options[:binding].array?

  # we always need to return a twin/form here so we can call nested.deserialize().
  handle_fail(twin, options)

  twin
end