Skip to content

Commit

Permalink
Add serialize_persistent_params helper method (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz authored Sep 18, 2024
1 parent db5009f commit ed160e4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
21 changes: 21 additions & 0 deletions app/helpers/trestle/params_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,26 @@ def persistent_params

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

# 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.
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
end
end
66 changes: 64 additions & 2 deletions spec/trestle/helpers/params_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'

describe Trestle::ParamsHelper, type: :helper do
let(:params) { ActionController::Parameters.new(sort: :field, order: "asc", ignore: "me") }

describe "#persistent_params" do
let(:params) { ActionController::Parameters.new(sort: :field, order: "asc", ignore: "me") }

it "only returns params that should be persistent" do
expect(persistent_params).to eq(ActionController::Parameters.new(sort: :field, order: "asc").permit!)
end
Expand Down Expand Up @@ -48,4 +48,66 @@
end
end
end

describe "#serialize_persistent_params" do
before(:each) do
allow(Trestle.config).to receive(:persistent_params).and_return([:sort, { array: [] }, :order, { hash: {} }])
end

let(:params) { ActionController::Parameters.new(sort: :field, order: "asc", ignore: "me", array: ["foo", "bar", "baz"], hash: { key1: "value1", key2: "value2" },) }

it "returns hidden input tags for each parameter" do
result = serialize_persistent_params

expect(result).to have_tag("input", with: { type: "hidden", name: "sort", value: "field" })
expect(result).to have_tag("input", with: { type: "hidden", name: "order", value: "asc" })
expect(result).not_to have_tag("input", with: { type: "hidden", name: "ignore", value: "me" })
end

it "returns a hidden input tag for each individual array parameter" do
result = serialize_persistent_params

expect(result).to have_tag("input", with: { type: "hidden", name: "array[]", value: "foo" })
expect(result).to have_tag("input", with: { type: "hidden", name: "array[]", value: "bar" })
expect(result).to have_tag("input", with: { type: "hidden", name: "array[]", value: "baz" })
end

it "returns a hidden input tag for each individual hash parameter" do
result = serialize_persistent_params

expect(result).to have_tag("input", with: { type: "hidden", name: "hash[key1]", value: "value1" })
expect(result).to have_tag("input", with: { type: "hidden", name: "hash[key2]", value: "value2" })
end

it "does not include specific params matching the :except option (singular)" do
result = serialize_persistent_params(except: :order)

expect(result).not_to have_tag("input", with: { type: "hidden", name: "order", value: "asc" })
end

it "does not include specific params matching the :except option (array)" do
result = serialize_persistent_params(except: [:order, :array])

expect(result).not_to have_tag("input", with: { type: "hidden", name: "order", value: "asc" })
expect(result).not_to have_tag("input", with: { type: "hidden", name: "array[]", value: "foo" })
expect(result).not_to have_tag("input", with: { type: "hidden", name: "array[]", value: "bar" })
expect(result).not_to have_tag("input", with: { type: "hidden", name: "array[]", value: "baz" })
end

it "only includes params matching the :only option (singular)" do
result = serialize_persistent_params(only: :sort)

expect(result).to have_tag("input", count: 1)
expect(result).to have_tag("input", with: { type: "hidden", name: "sort", value: "field" })
end

it "only includes params matching the :only option (array)" do
result = serialize_persistent_params(only: [:sort, :hash])

expect(result).to have_tag("input", count: 3)
expect(result).to have_tag("input", with: { type: "hidden", name: "sort", value: "field" })
expect(result).to have_tag("input", with: { type: "hidden", name: "hash[key1]", value: "value1" })
expect(result).to have_tag("input", with: { type: "hidden", name: "hash[key2]", value: "value2" })
end
end
end

0 comments on commit ed160e4

Please sign in to comment.