Class: Blacklight::Parameters

Inherits:
Object
  • Object
show all
Extended by:
Deprecation
Defined in:
lib/blacklight/parameters.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, search_state) ⇒ Parameters

Returns a new instance of Parameters.



55
56
57
58
# File 'lib/blacklight/parameters.rb', line 55

def initialize(params, search_state)
  @params = params.is_a?(Hash) ? params.with_indifferent_access : params
  @search_state = search_state
end

Instance Attribute Details

#paramsObject (readonly)

rubocop:enable Naming/MethodParameterName



51
52
53
# File 'lib/blacklight/parameters.rb', line 51

def params
  @params
end

#search_stateObject (readonly)

rubocop:enable Naming/MethodParameterName



51
52
53
# File 'lib/blacklight/parameters.rb', line 51

def search_state
  @search_state
end

Class Method Details

.deep_merge_permitted_params(a, b) ⇒ Array<Symbol, Hash>

rubocop:disable Naming/MethodParameterName Merge two Rails strong_params-style permissions into a single list of permitted parameters, deep-merging complex values as needed.

Parameters:

  • a (Array<Symbol, Hash>)
  • b (Array<Symbol, Hash>)

Returns:

  • (Array<Symbol, Hash>)


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/blacklight/parameters.rb', line 24

def self.deep_merge_permitted_params(a, b)
  a = [a] if a.is_a? Hash
  b = [b] if b.is_a? Hash

  complex_params_from_a, scalar_params_from_a = a.flatten.uniq.partition { |x| x.is_a? Hash }
  complex_params_from_a = complex_params_from_a.inject({}) { |tmp, h| _deep_merge_permitted_param_hashes(h, tmp) }
  complex_params_from_b, scalar_params_from_b = b.flatten.uniq.partition { |x| x.is_a? Hash }
  complex_params_from_b = complex_params_from_b.inject({}) { |tmp, h| _deep_merge_permitted_param_hashes(h, tmp) }

  (scalar_params_from_a + scalar_params_from_b + [_deep_merge_permitted_param_hashes(complex_params_from_a, complex_params_from_b)]).reject(&:blank?).uniq
end

.sanitize(params) ⇒ Object

Sanitize the search parameters by removing unnecessary parameters from the provided parameters.

Parameters:

  • params (Hash)

    parameters



13
14
15
16
# File 'lib/blacklight/parameters.rb', line 13

def self.sanitize params
  params.reject { |_k, v| v.nil? }
        .except(:action, :controller, :id, :commit, :utf8)
end

Instance Method Details

#permit_search_paramsObject

Parameters:

  • params (Hash)

    with unknown structure (not declared in the blacklight config or filters) stripped out



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/blacklight/parameters.rb', line 61

def permit_search_params
  # if the parameters were generated internally, we can (probably) trust that they're fine
  return params unless params.is_a?(ActionController::Parameters)

  # if the parameters were permitted already, we should be able to trust them
  return params if params.permitted?

  permitted_params = filter_fields.inject(blacklight_config.search_state_fields) do |allowlist, filter|
    Blacklight::Parameters.deep_merge_permitted_params(allowlist, filter.permitted_params)
  end

  deep_unmangle_params!(params, permitted_params)

  if blacklight_config.filter_search_state_fields
    params.permit(*permitted_params)
  else
    warn_about_deprecated_parameter_handling(params, permitted_params)
    params.deep_dup.permit!
  end
end