Sha256: cea1c9e2a3c1d73b3d94a4214be79e08bac983d0100d6c2984b95c766ddda331

Contents?: true

Size: 1.68 KB

Versions: 31

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true
# Rails Helper methods to take a hash and turn it to form <input type="hidden">
# fields, works with hash nested with other hashes and arrays, standard rails
# serialization style.  Oddly while Hash#to_query will do this for a URL
# query parameters, there seems to be no built in way to do it to create
# hidden form fields instead. 
#
# Code taken from http://marklunds.com/articles/one/314
#
# This is used to serialize a complete current query from current params
# to form fields used for sort and change per-page
module Blacklight::HashAsHiddenFieldsHelperBehavior
  ##
  # 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.
  # @param [Hash] hash
  # @return [String] 
  def render_hash_as_hidden_fields(hash)
    
    hidden_fields = []
    flatten_hash(hash).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

  protected
  
  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

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
blacklight-6.25.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.24.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.23.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.22.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.21.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.20.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.19.2 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.19.1 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.19.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.18.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.17.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.16.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.15.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.14.1 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.14.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.13.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.12.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.11.2 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.11.1 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb
blacklight-6.11.0 app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb