Sha256: 070d96daaaed15cc2ecde909088ecfbd430a0f412918ccc2202709c27284932c
Contents?: true
Size: 1.42 KB
Versions: 25
Compression:
Stored size: 1.42 KB
Contents
# frozen_string_literal: true module Blacklight # Writes out zero or more <input type="hidden"> elements, completely # representing a hash passed in using Rails-style request parameters # for hashes nested with arrays and other hashes. class HiddenSearchStateComponent < ::ViewComponent::Base # @param [Hash] hash def initialize(params:) Deprecation.warn(self, "Passing page as a parameter to HiddenSearchStateComponent is deprecated and will not be supported in Blacklight 8") if params.key?(:page) @params = params.except(:utf8) end def call hidden_fields = [] flatten_hash(@params).each do |name, value| value = Array.wrap(value) value.each do |v| hidden_fields << hidden_field_tag(name, v.to_s, id: nil) end end safe_join(hidden_fields, "\n") end private 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 end end
Version data entries
25 entries across 25 versions & 1 rubygems