Sha256: 5c40dc606b3dbe677ed27c3b5b346bfe7486beb6f5f382bb43b5d370656369e8

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

module Reform::Form::Dry
  module InputHash
    private

    # if dry_error is a hash rather than an array then it contains
    # the messages for a nested property
    # these messages need to be added to the correct collection
    # objects.

    # collections:
    # {0=>{:name=>["must be filled"]}, 1=>{:name=>["must be filled"]}}

    # Objects:
    # {:name=>["must be filled"]}
    # simply load up the object and attach the message to it

    # we can't use to_nested_hash as it get's messed up by composition.
    def input_hash(form)
      hash = form.class.nested_hash_representer.new(form).to_hash
      symbolize_hash(hash)
    end

    # dry-v needs symbolized keys
    # TODO: Don't do this here... Representers??
    def symbolize_hash(old_hash)
      old_hash.each_with_object({}) do |(k, v), new_hash|
        new_hash[k.to_sym] = if v.is_a?(Hash)
                               symbolize_hash(v)
                             elsif v.is_a?(Array)
                               v.map { |h| h.is_a?(Hash) ? symbolize_hash(h) : h }
                             else
                               v
                             end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reform-2.6.2 lib/reform/form/dry/input_hash.rb
reform-2.6.1 lib/reform/form/dry/input_hash.rb
reform-2.6.0 lib/reform/form/dry/input_hash.rb
reform-2.5.0 lib/reform/form/dry/input_hash.rb
reform-2.3.3 lib/reform/form/dry/input_hash.rb
reform-2.3.2 lib/reform/form/dry/input_hash.rb
reform-2.3.1 lib/reform/form/dry/input_hash.rb
reform-2.3.0.rc2 lib/reform/form/dry/input_hash.rb