Module: Trestle::ParamsHelper

Defined in:
app/helpers/trestle/params_helper.rb

Instance Method Summary collapse

Instance Method Details

#persistent_paramsObject

Returns a subset of the params “hash” (an instance of ActionController::Parameters) limited to only those keys that should be considered persistent throughout reordering and pagination.

This could be a scope or the current order, but this may be extended by Trestle plugins and the application itself in ‘Trestle.config.persistent_params` to include search queries, filters, etc.

By default this list is set to: [:sort, :order, :scope]

Returns an instance of ActionController::Parameters.



14
15
16
17
18
19
# File 'app/helpers/trestle/params_helper.rb', line 14

def persistent_params
  flat, nested = Trestle.config.persistent_params.partition { |p| !p.is_a?(Hash) }
  nested = nested.inject({}) { |result, param| result.merge(param) }

  params.slice(*(flat + nested.keys)).permit(*(flat << nested))
end

#serialize_persistent_params(except: [], only: []) ⇒ Object

Converts the current set of persistent params into hidden form fields, suitable for inclusion within a form tag (e.g for search or filtering).

except - List of param keys to exclude only - List of param keys to only include

Returns a HTML-safe String.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/trestle/params_helper.rb', line 28

def serialize_persistent_params(except: [], only: [])
  params = persistent_params

  if Array(only).any?
    params = params.slice(*only)
  elsif Array(except).any?
    params = params.except(*except)
  end

  safe_join(to_form_params(params).map { |param|
    tag.input(type: "hidden", name: param[:name], value: param[:value], autocomplete: "off")
  }, "\n")
end