Class: Reform::Form::Validate::Populator::PopulateIfEmpty
- Inherits:
-
Object
- Object
- Reform::Form::Validate::Populator::PopulateIfEmpty
- Includes:
- Uber::Callable
- Defined in:
- lib/reform/form/validate.rb
Overview
This might change soon (e.g. moved into disposable).
Instance Method Summary collapse
Instance Method Details
#call(fields, fragment, *args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/reform/form/validate.rb', line 41 def call(fields, fragment, *args) index = args.first = args.last binding = .binding form = binding.get parent_form = .[:parent_form] # FIXME: test those cases!!! return form[index] if binding.array? and form and form[index] # TODO: this should be handled by the Binding. return form if !binding.array? and form # only get here when above form is nil. if binding[:populate_if_empty].is_a?(Proc) model = parent_form.instance_exec(fragment, ., &binding[:populate_if_empty]) # call user block. else model = binding[:populate_if_empty].new end form = binding[:form].new(model) # free service: wrap model with Form. this usually happens in #setup. if binding.array? # TODO: please extract this into Disposable. fields = fields.send(:fields) fields.send("#{binding.setter}", []) unless fields.send("#{binding.getter}") # DISCUSS: why do I have to initialize this here? fields.send("#{binding.getter}")[index] = form else fields.send("#{binding.setter}", form) # :setter is currently overwritten by :parse_strategy. end end |