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

Version Path
blacklight-7.24.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.23.0.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.23.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.22.2 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.22.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.22.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.21.2 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.21.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.21.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.20.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.20.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.19.2 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.19.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.19.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.18.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.18.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.17.2 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.17.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.17.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.16.0 app/components/blacklight/hidden_search_state_component.rb