Module: Uchi::Pagy::Linkable::QueryUtils

Defined in:
lib/uchi/pagy/modules/abilities/linkable.rb

Class Method Summary collapse

Class Method Details

.build_nested_query(value, prefix = nil, unescaped = []) ⇒ Object

Extracted from Rack::Utils and reformatted for rubocop Add the ‘unescaped’ param, and use it for simple and safe url-templating. All string keyed hashes



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/uchi/pagy/modules/abilities/linkable.rb', line 15

def build_nested_query(value, prefix = nil, unescaped = [])
  case value
  when Array
    value.map { |v| build_nested_query(v, "#{prefix}[]", unescaped) }.join('&')
  when Hash
    value.map do |k, v|
      new_k = prefix ? "#{prefix}[#{escape(k)}]" : escape(k)
      unescaped[unescaped.find_index(k)] = new_k if unescaped.size.positive? && new_k != k && unescaped.include?(k)
      build_nested_query(v, new_k, unescaped)
    end.delete_if(&:empty?).join('&')
  when nil
    escape(prefix)
  else
    raise ArgumentError, 'value must be a Hash' if prefix.nil?
    return "#{escape(prefix)}=#{value}" if unescaped.include?(prefix)

    "#{escape(prefix)}=#{escape(value)}"
  end
end

.escape(str) ⇒ Object



35
36
37
# File 'lib/uchi/pagy/modules/abilities/linkable.rb', line 35

def escape(str)
  URI.encode_www_form_component(str)
end