Sha256: 5713ccb02e19cb70ec5281ae6388ff375996a5201ccc55f265e9ede5600f7303

Contents?: true

Size: 1.27 KB

Versions: 21

Compression:

Stored size: 1.27 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:)
      @params = params.except(:page, :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

21 entries across 21 versions & 2 rubygems

Version Path
blacklight-8.6.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.6.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.5.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.5.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.4.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-8.3.0/app/components/blacklight/hidden_search_state_component.rb
blacklight-8.3.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.2.2 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.2.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.2.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.1.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.1 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0.beta8 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0.beta7 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0.beta6 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0.beta5 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0.beta4 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0.beta3 app/components/blacklight/hidden_search_state_component.rb
blacklight-8.0.0.beta2 app/components/blacklight/hidden_search_state_component.rb