Sha256: 023802491b5909aa058a23c2526690c62543261f8bbfe20810ff13fcb61eed1f

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 < Blacklight::Component
    # @param [Hash] params
    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 & 2 rubygems

Version Path
blacklight-7.40.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.39.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.38.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-7.37.0/app/components/blacklight/hidden_search_state_component.rb
blacklight-7.37.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.36.2 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.36.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.36.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.35.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.34.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.33.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.33.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.32.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.31.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.30.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.29.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.28.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.27.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.27.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-7.26.1 app/components/blacklight/hidden_search_state_component.rb