Class: TypedFormModel::ParamsLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_form_model/params_loader.rb

Overview

Converts raw controller params (Hash or ActionController::Parameters) into a kwargs Hash suitable for FormClass.new(...). Filters to declared props, symbolises keys. Routes Rails FormBuilder-style *_attributes keys back to their underlying nested-form prop, converting indexed hashes for array-of-form props into arrays.

Constant Summary collapse

ATTRIBUTES_SUFFIX =
"_attributes"

Instance Method Summary collapse

Constructor Details

#initialize(form_class) ⇒ ParamsLoader



12
13
14
# File 'lib/typed_form_model/params_loader.rb', line 12

def initialize(form_class)
  @form_class = form_class
end

Instance Method Details

#call(raw, persisted: false) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/typed_form_model/params_loader.rb', line 16

def call(raw, persisted: false)
  return {persisted: persisted} if raw.nil?
  hash = to_hash(raw)
  attrs = hash.each_with_object({}) do |(key, value), acc|
    attach_entry(acc, key, value)
  end
  attrs[:persisted] = persisted
  attrs
end