Sha256: 646ab698a8359d2978b1f933b7d561c408451930f8b810fe8e07f5d1131c1124

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

module SolidusSearchkick
  module Helpers
    module FormHelper
      def flatten_hash(hash = params, ancestor_names = [])
        flat_hash = {}
        hash.each do |k, v|
          names = Array.new(ancestor_names)
          names << k
          if v.is_a?(Hash)
            flat_hash.merge!(flatten_hash(v, names))
          else
            key = flat_hash_key(names)
            key += '[]' if v.is_a?(Array)
            flat_hash[key] = v
          end
        end

        flat_hash
      end

      def flat_hash_key(names)
        names = Array.new(names)
        name = names.shift.to_s.dup
        names.each do |n|
          name << "[#{n}]"
        end
        name
      end

      def hash_as_hidden_fields(hash = params)
        hidden_fields = []
        flatten_hash(hash).each do |name, value|
          value = [value] if !value.is_a?(Array)
          value.each do |v|
            hidden_fields << hidden_field_tag(name, v.to_s, id: nil)
          end
        end

        hidden_fields.join("\n")
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
solidus_searchkick-0.3.4 lib/solidus_searchkick/helpers/form_helper.rb
solidus_searchkick-0.3.3 lib/solidus_searchkick/helpers/form_helper.rb
solidus_searchkick-0.3.2 lib/solidus_searchkick/helpers/form_helper.rb
solidus_searchkick-0.3.1 lib/solidus_searchkick/helpers/form_helper.rb
solidus_searchkick-0.3.0 lib/solidus_searchkick/helpers/form_helper.rb
solidus_searchkick-0.2.3 lib/solidus_searchkick/helpers/form_helper.rb
solidus_searchkick-0.2.2 lib/solidus_searchkick/helpers/form_helper.rb
solidus_searchkick-0.2.1 lib/solidus_searchkick/helpers/form_helper.rb