Class: Blacklight::Parameters

Inherits:
Object
  • Object
show all
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.



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

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



47
48
49
# File 'lib/blacklight/parameters.rb', line 47

def params
  @params
end

#search_stateObject (readonly)

rubocop:enable Naming/MethodParameterName



47
48
49
# File 'lib/blacklight/parameters.rb', line 47

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>)


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/blacklight/parameters.rb', line 20

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)]).compact_blank.uniq
end

.sanitize(params) ⇒ Object

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

Parameters:

  • params (Hash)

    parameters



9
10
11
12
# File 'lib/blacklight/parameters.rb', line 9

def self.sanitize params
  params.reject { |_k, v| v.nil? } # not available in Rails 6.0
        .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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/blacklight/parameters.rb', line 57

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
    params.deep_dup.permit!
  end
end